Given an array A[] of N integers, the task is to check if it is possible to construct several arrays (at least one) using all the elements of the array A[] such that in each array, the value of each element is equal to the number of elements to its left.
Examples:
Input: N = 9, A[] = {0, 0, 0, 0, 1, 1, 1, 2, 2}
Output: YES
Explanation: The array A[] can be divided into 4 arrays as follows -
- {0,1,2} -> 1st element (0) has 0 elements to its left, 2nd element (1) has 1 element to its left and 3rd element (2) has 2 elements to its left
- {0,1} -> 1st element (0) has 0 elements to its left and 2nd element (1) has 1 element to its left
- {0,1,2} -> 1st element (0) has 0 elements to its left, 2nd element (1) has 1 element to its left and 3rd element (2) has 2 elements to its left
- {0} -> 1st element (0) has 0 elements to its left
Input: N = 3, A[] = {0, 0, 2}
Output: NO
Approach: To solve the problem follow the below observations:
Observation:
- The problem can be rephrased as " Is it possible to divide input array in Arithmetic Progressions each having the first digit 0 and a common difference equal to 1 "?
- For each A.P., if an integer X is a part of it, then X-1 must also be there. So, in the whole sequence if X occurs "count" times, then X-1 must also occur at least count times.
Following is the code based on the above approach:
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to Check if it is possible
// to construct several number
// of arrays from the given array
// satisfying the given condition
bool isPossible(int N, int A[])
{
// Initialize flag variable to
// store the answer
bool flag = true;
// Initializing an unordered_map to
// store frequency of each element
unordered_map<int, int> mp;
// Iterating through the array
for (int i = 0; i < N; i++) {
// If the current element is less
// than N, increment
// its frequency in map
if (A[i] < N) {
mp[A[i]]++;
}
// Else, make the value of flag = false
// (as there can never be more than
// N elements before any
// element in the array)
else {
flag = false;
}
}
// Iterate from 1 to N and check the
// required condition
for (int i = 1; i <= N; i++) {
if (mp[i] > mp[i - 1]) {
flag = false;
break;
}
}
return flag;
}
// Driver code
int main()
{
int N = 9;
int A[] = { 0, 0, 0, 0, 1, 1, 1, 2, 2 };
// Function Call
int answer = isPossible(N, A);
if (answer == 1) {
cout << "YES";
}
else {
cout << "NO";
}
}
import java.util.HashMap;
public class GFG {
// Function to Check if it is possible
// to construct several number
// of arrays from the given array
static boolean isPossible(int N, int[] A) {
// Initialize flag variable to
// store the answer
boolean flag = true;
// Initializing a HashMap to
// store frequency of each element
HashMap<Integer, Integer> map = new HashMap<>();
// Iterating through the array
for (int i = 0; i < N; i++) {
// If the current element is less
// than N, increment
// its frequency in the map
if (A[i] < N) {
map.put(A[i], map.getOrDefault(A[i], 0) + 1);
}
// Else, make the value of flag = false
// (as there can never be more than
// N elements before any
// element in the array)
else {
flag = false;
}
}
// Iterate from 1 to N and check the
// required condition
for (int i = 1; i <= N; i++) {
if (map.getOrDefault(i, 0) > map.getOrDefault(i - 1, 0)) {
flag = false;
break;
}
}
return flag;
}
// Driver code
public static void main(String[] args) {
int N = 9;
int[] A = { 0, 0, 0, 0, 1, 1, 1, 2, 2 };
// Function Call
boolean answer = isPossible(N, A);
if (answer) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
# Python code for the above approach
# Function to Check if it is possible
# to construct several number
# of arrays from the given array
# satisfying the given condition
def isPossible(N, A):
# Initialize flag variable to
# store the answer
flag = True
# Initializing a dictionary to
# store frequency of each element
mp = {}
# Iterating through the array
for i in range(N):
# If the current element is less
# than N, increment
# its frequency in the dictionary
if A[i] < N:
if A[i] in mp:
mp[A[i]] += 1
else:
mp[A[i]] = 1
# Else, make the value of flag = false
# (as there can never be more than
# N elements before any
# element in the array)
else:
flag = False
# Iterate from 1 to N and check the
# required condition
for i in range(1, N + 1):
if i in mp and mp[i] > mp.get(i - 1, 0):
flag = False
break
return flag
# Driver code
N = 9
A = [0, 0, 0, 0, 1, 1, 1, 2, 2]
# Function Call
answer = isPossible(N, A)
if answer:
print("YES")
else:
print("NO")
# this code is contributed by uttamdp_10
using System;
using System.Collections.Generic;
class GFG {
// Function to Check if it is possible
// to construct several number
// of arrays from the given array
static bool IsPossible(int N, int[] A)
{
// Initialize flag variable to
// store the answer
bool flag = true;
// Initializing a Dictionary to
// store frequency of each element
Dictionary<int, int> map
= new Dictionary<int, int>();
// Iterating through the array
for (int i = 0; i < N; i++) {
// If the current element is less
// than N, increment
// its frequency in the dictionary
if (A[i] < N) {
if (map.ContainsKey(A[i])) {
map[A[i]]++;
}
else {
map[A[i]] = 1;
}
}
// Else, make the value of flag = false
// (as there can never be more than
// N elements before any
// element in the array)
else {
flag = false;
}
}
// Iterate from 1 to N and check the
// required condition
for (int i = 1; i <= N; i++) {
if (map.TryGetValue(i, out int countI)
&& map.TryGetValue(i - 1,
out int countPrev)) {
if (countI > countPrev) {
flag = false;
break;
}
}
}
return flag;
}
// Driver code
public static void Main(string[] args)
{
int N = 9;
int[] A = { 0, 0, 0, 0, 1, 1, 1, 2, 2 };
// Function Call
bool answer = IsPossible(N, A);
if (answer) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// Added by: Nikunj Sonigara
// Function to check if it's possible to create an array of length N
// with values ranging from 0 to N-1 using the input array A.
function isPossible(N, A) {
// Initialize a flag as true to assume it's possible.
let flag = true;
// Create a Map to store counts of each element.
const map = new Map();
// Loop through the input array A.
for (let i = 0; i < N; i++) {
if (A[i] < N) {
// Update the count of A[i] in the map.
map.set(A[i], (map.get(A[i]) || 0) + 1);
} else {
// If any element in A is greater than or equal to N, set flag to false.
flag = false;
}
}
// Loop through numbers from 1 to N and check if their counts are non-decreasing.
for (let i = 1; i <= N; i++) {
if ((map.get(i) || 0) > (map.get(i - 1) || 0)) {
// If counts are not non-decreasing, set flag to false.
flag = false;
// Exit the loop.
break;
}
}
// Return the final flag indicating if it's possible.
return flag;
}
// Test input values
const N = 9;
const A = [0, 0, 0, 0, 1, 1, 1, 2, 2];
// Call the isPossible function with the test input and print "YES" or "NO" accordingly.
const answer = isPossible(N, A);
if (answer) {
console.log("YES");
} else {
console.log("NO");
}
Output
YES
Time Complexity: O(N).
Auxiliary Space: O(N)