Construction of multiple AP Arrays
Last Updated :
19 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int N, int A[])
{
bool flag = true ;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
if (A[i] < N) {
mp[A[i]]++;
}
else {
flag = false ;
}
}
for ( int i = 1; i <= N; i++) {
if (mp[i] > mp[i - 1]) {
flag = false ;
break ;
}
}
return flag;
}
int main()
{
int N = 9;
int A[] = { 0, 0, 0, 0, 1, 1, 1, 2, 2 };
int answer = isPossible(N, A);
if (answer == 1) {
cout << "YES";
}
else {
cout << "NO";
}
}
|
Java
import java.util.HashMap;
public class GFG {
static boolean isPossible( int N, int [] A) {
boolean flag = true ;
HashMap<Integer, Integer> map = new HashMap<>();
for ( int i = 0 ; i < N; i++) {
if (A[i] < N) {
map.put(A[i], map.getOrDefault(A[i], 0 ) + 1 );
}
else {
flag = false ;
}
}
for ( int i = 1 ; i <= N; i++) {
if (map.getOrDefault(i, 0 ) > map.getOrDefault(i - 1 , 0 )) {
flag = false ;
break ;
}
}
return flag;
}
public static void main(String[] args) {
int N = 9 ;
int [] A = { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 };
boolean answer = isPossible(N, A);
if (answer) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def isPossible(N, A):
flag = True
mp = {}
for i in range (N):
if A[i] < N:
if A[i] in mp:
mp[A[i]] + = 1
else :
mp[A[i]] = 1
else :
flag = False
for i in range ( 1 , N + 1 ):
if i in mp and mp[i] > mp.get(i - 1 , 0 ):
flag = False
break
return flag
N = 9
A = [ 0 , 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 ]
answer = isPossible(N, A)
if answer:
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG {
static bool IsPossible( int N, int [] A)
{
bool flag = true ;
Dictionary< int , int > map
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (A[i] < N) {
if (map.ContainsKey(A[i])) {
map[A[i]]++;
}
else {
map[A[i]] = 1;
}
}
else {
flag = false ;
}
}
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;
}
public static void Main( string [] args)
{
int N = 9;
int [] A = { 0, 0, 0, 0, 1, 1, 1, 2, 2 };
bool answer = IsPossible(N, A);
if (answer) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function isPossible(N, A) {
let flag = true ;
const map = new Map();
for (let i = 0; i < N; i++) {
if (A[i] < N) {
map.set(A[i], (map.get(A[i]) || 0) + 1);
} else {
flag = false ;
}
}
for (let i = 1; i <= N; i++) {
if ((map.get(i) || 0) > (map.get(i - 1) || 0)) {
flag = false ;
break ;
}
}
return flag;
}
const N = 9;
const A = [0, 0, 0, 0, 1, 1, 1, 2, 2];
const answer = isPossible(N, A);
if (answer) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N).
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...