Check if it is possible to construct an array with the given conditions
Last Updated :
18 Sep, 2023
Given integers N and K and an array A[] of M integers. The task is to check if it is possible to construct an array of size N such that-
- All the K consecutive elements of the array are distinct.
- Considering 1-based indexing, the integer i can be used A[i] times to construct the array.
Note that sum of all elements of array is A[] is guaranteed to be N.
Examples :
Input : N=12, M=6, K=2, A[]={2,2,2,2,2,2}
Output : YES
Explanation : Consider the array {1,2,1,2,3,4,3,4,5,6,5,6}. It can be seen that any 2 consecutive elements are different (as K=2). Also, as per the array A[], all the integers from 1 to 6 are used twice.
Input : N=12, M=6, K=2, A[]={1,1,1,1,1,7}
Output : NO
Approach :
Let num = N/K.
We divide the final array into contiguous subarrays of length num each, except the last subarray (which will have length say len = N%K).
Observation - 1:
Every element of given array A[] should be smaller than or equal to ceil(N/K).
This is because each A[i] has to be used A[i] times in construction of the resultant array. In case, any A[i] is greater than ceil(N/K), it has to be present twice in atleast one subarray of the resultant array to meet the required conditions.
Then, we will count the number of A[i] which are equal to ceil(N/K) (or basically num+1).
Observation - 2 :
The count of such elements must be smaller than or equal to len (which is the length of last subarray).
Steps involved in the implementation of code:
- For each element A[i] of A, follow the steps 4 and 5.
- If A[i] is equal to num+1, increment count by 1.
- Else if A[i] is greater than num+1, make flag equal to 0 as it won't be possible to construct the array.
- At the end of iteration, check if flag is 1 and count is less than or equal to len. If so, return 1, else return 0.
Below is the implementation of the above approach:
C++
// C++ code for the
//above approach
#include <bits/stdc++.h>
using namespace std;
// function to check if it is
// possible to construct an array
// with the given conditions
bool isPossible(int N, int M, int K, int A[])
{
// num stores the number
//of partitions of size K
// of the final array
int num = N / K;
// len stores the length of last
// partition whose length
//is less than K
int len = N % K;
// Count stores the Number of
// elements of array A whose
// value are equal to num+1
int count = 0;
// Flag variable to check if
// there is a valid
//answer or not
int flag = 1;
// Iterating through the given
// array A
for (int i = 0; i < M; i++) {
if (A[i] > num) {
// If ith element of A
// is equal to num+1,
// increment count
if (A[i] == num + 1) {
count++;
}
// else if ith element is greater
// than num+1, make flag=0
else {
flag = 0;
}
}
}
if (flag) {
// If flag = 1 and count is less
// than or equal to len, return 1
if (count <= len) {
return 1;
}
// else return 0
else {
return 0;
}
}
// If flag is 0, return 0
else {
return 0;
}
}
// Driver code
int main()
{
int N = 12, M = 6, K = 2;
int A[] = { 2, 2, 2, 2, 2, 2 };
int answer = isPossible(N, M, K, A);
if (answer == 1) {
cout << "YES" ;
}
else {
cout << "NO";
}
}
Java
import java.util.Arrays;
public class GFG {
// function to check if it is possible to construct an array
// with the given conditions
public static boolean isPossible(int N, int M, int K, int[] A) {
// num stores the number of partitions of size K
// of the final array
int num = N / K;
// len stores the length of the last partition
// whose length is less than K
int len = N % K;
// Count stores the number of elements of array A whose
// value is equal to num+1
int count = 0;
// Flag variable to check if there is a valid answer or not
boolean flag = true;
// Iterating through the given array A
for (int i = 0; i < M; i++) {
if (A[i] > num) {
// If the ith element of A is equal to num+1,
// increment count
if (A[i] == num + 1) {
count++;
}
// else if the ith element is greater than num+1,
// make flag false
else {
flag = false;
}
}
}
if (flag) {
// If flag is true and count is less than or equal to len,
// return true
if (count <= len) {
return true;
}
// else return false
else {
return false;
}
}
// If flag is false, return false
else {
return false;
}
}
// Driver code
public static void main(String[] args) {
int N = 12, M = 6, K = 2;
int[] A = {2, 2, 2, 2, 2, 2};
boolean answer = isPossible(N, M, K, A);
if (answer) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
Python3
# Python code for the above approach
# Function to check if it is
# possible to construct an array
# with the given conditions
def isPossible(N, M, K, A):
# num stores the number
# of partitions of size K
# of the final array
num = N // K
# len stores the length of last
# partition whose length
# is less than K
len = N % K
# Count stores the Number of
# elements of array A whose
# value are equal to num+1
count = 0
# Flag variable to check if
# there is a valid
# answer or not
flag = 1
# Iterating through the given
# array A
for i in range(M):
if A[i] > num:
# If ith element of A
# is equal to num+1,
# increment count
if A[i] == num + 1:
count += 1
# else if ith element is greater
# than num+1, make flag=0
else:
flag = 0
if flag:
# If flag = 1 and count is less
# than or equal to len, return 1
if count <= len:
return 1
# else return 0
else:
return 0
# If flag is 0, return 0
else:
return 0
# Driver code
N = 12
M = 6
K = 2
A = [2, 2, 2, 2, 2, 2]
answer = isPossible(N, M, K, A)
if answer == 1:
print("YES")
else:
print("NO")
# This code is contributed by Tapesh(tapeshdua420)
C#
// C# code for the above approach
using System;
public class GFG {
// function to check if it is possible to construct an
// array with the given conditions
static bool isPossible(int N, int M, int K, int[] A)
{
// num stores the number of partitions of size K
// of the final array
int num = N / K;
// len stores the length of the last partition
// whose length is less than K
int len = N % K;
// Count stores the number of elements of array A
// whose value is equal to num+1
int count = 0;
// Flag variable to check if there is a valid answer
// or not
bool flag = true;
// Iterating through the given array A
for (int i = 0; i < M; i++) {
if (A[i] > num) {
// If the ith element of A is equal to
// num+1,
// increment count
if (A[i] == num + 1) {
count++;
}
// else if the ith element is greater than
// num+1,
// make flag false
else {
flag = false;
}
}
}
if (flag) {
// If flag is true and count is less than or
// equal to len,
// return true
if (count <= len) {
return true;
}
// else return false
else {
return false;
}
}
// If flag is false, return false
else {
return false;
}
}
// Driver code
static void Main()
{
int N = 12, M = 6, K = 2;
int[] A = { 2, 2, 2, 2, 2, 2 };
bool answer = isPossible(N, M, K, A);
if (answer) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contributed by ragul21
JavaScript
// function to check if it is
// possible to construct an array
// with the given conditions
function isPossible(N, M, K, A) {
// num stores the number
//of partitions of size K
// of the final array
let num = Math.floor(N / K);
// len stores the length of last
// partition whose length
//is less than K
let len = N % K;
// Count stores the Number of
// elements of array A whose
// value are equal to num+1
let count = 0;
// Flag variable to check if
// there is a valid
//answer or not
let flag = 1;
// Iterating through the given
// array A
for (let i = 0; i < M; i++) {
if (A[i] > num) {
// If ith element of A
// is equal to num+1,
// increment count
if (A[i] === num + 1) {
count++;
}
// else if ith element is greater
// than num+1, make flag=0
else {
flag = 0;
}
}
}
if (flag) {
// If flag = 1 and count is less
// than or equal to len, return 1
if (count <= len) {
return 1;
}
// else return 0
else {
return 0;
}
}
// If flag is 0, return 0
else {
return 0;
}
}
// Test case
let N = 12, M = 6, K = 2;
let A = [2, 2, 2, 2, 2, 2];
let answer = isPossible(N, M, K, A);
if (answer === 1) {
console.log("YES");
} else {
console.log("NO");
}
Time Complexity : O(M)
Auxiliary Space : O(1)
Similar Reads
Check if there exist 4 indices in the array satisfying the given condition
Given an array A[] of N positive integers and 3 integers X, Y, and Z, the task is to check if there exist 4 indices (say p, q, r, s) such that the following conditions are satisfied: 0 < p < q < r < s < NSum of the subarray from A[p] to A[q - 1] is XSum of the subarray from A[q] to A[
8 min read
Generate an array of size K which satisfies the given conditions
Given two integers N and K, the task is to generate an array arr[] of length K such that: arr[0] + arr[1] + ... + arr[K - 1] = N.arr[i] > 0 for 0 ? i < K.arr[i] < arr[i + 1] ? 2 * arr[i] for 0 ? i < K - 1. If there are multiple answers find any one of them, otherwise, print -1. Examples:
8 min read
Check if there exists any subarray with the given conditions
Given two integers N and X. Then the task is to return YES or NO by checking whether there exists a subarray in any permutation of length N such that it contains a subarray, where A*B is equal to the X. Here A and B denote the number of elements in sub-array and the first element of sorted subarray
5 min read
Construct minimum length array with given conditions
Given an array, A[] of N integers and 2 integers P and Q, the task is to construct an array of minimum possible length with given P and Q and absolute difference of consecutive elements equal to 1 such that: P is the sum of all the elements A[i] of the array such that A[i] > A[(i+1) % N] and A[i]
6 min read
Check if the Array elements can be made 0 with given conditions
Given two arrays arr[] and arr2[], and a value K, the task is to check whether the array arr[] can be made to all zeros such that in each operation all elements less than K are made to 0 else they are subtracted by K then the K gets decayed by the smallest value of the array arr2[] whose arr[i] is n
10 min read
Count of triplets in an array that satisfy the given conditions
Given an array arr[] of N elements, the task is to find the count of triplets (arr[i], arr[j], arr[k]) such that (arr[i] + arr[j] + arr[k] = L) and (L % arr[i] = L % arr[j] = L % arr[k] = 0.Examples: Input: arr[] = {2, 4, 5, 6, 7} Output: 1 Only possible triplet is {2, 4, 6}Input: arr[] = {4, 4, 4,
13 min read
Find an array of size N that satisfies the given conditions
Given three integers N, S, and K, the task is to create an array of N positive integers such that the bitwise OR of any two consecutive elements from the array is odd and there are exactly K subarrays with a sum equal to S where 1 ? K ? N / 2. Examples: Input: N = 4, K = 2, S = 6 Output: 6 7 6 7 Her
8 min read
Check if it is possible to perform the given Grid Division
Given two integers N and M which represent the size of a grid. Also given an integer array arr[] of size P which represent that the given grid is divided into P parts each consisting of arr[i] cells from the grid. The task is to check whether it is possible to divide the grid in the given manner or
5 min read
Generate an array B[] from the given array A[] which satisfies the given conditions
Given an array A[] of N integers such that A[0] + A[1] + A[2] + ... A[N - 1] = 0. The task is to generate an array B[] such that B[i] is either ?A[i] / 2? or ?A[i] / 2? for all valid i and B[0] + B[1] + B[2] + ... + B[N - 1] = 0. Examples: Input: A[] = {1, 2, -5, 3, -1} Output: 0 1 -2 1 0Input: A[]
6 min read
Form an Array so that their Bitwise XOR sum satisfies given conditions
Given an array arr[] of size N ( N is even ), the task is to construct an array B[] such that the sum of B[i] XOR PrefixXor[i], where 1 ? i ? N is even and is divisible by the first N/2 elements of arr[]. PrefixXor[] array is an array where each element will indicate the XOR of all elements from 1st
7 min read