Split an array into equal length subsequences consisting of equal elements only
Given an array arr[] of size N, the task is to check if it is possible to split the array arr[] into different subsequences of equal size such that each element of the subsequence are equal. If found to be true, then print “YES”. Otherwise, print “NO”.
Examples:
Input: arr[] = {1, 2, 3, 4, 4, 3, 2, 1}
Output: YES
Explanation: Possible partition: {1, 1}, {2, 2}, {3, 3}, {4, 4}.
Input: arr[] = {1, 1, 1, 2, 2, 2, 3, 3}
Output: NO
Approach: The idea is based on the following observation: Let the frequency of arr[i] be Ci, then these elements must be broken down into subsequences of X such that Ci % X = 0. This must be YES for every index i. To satisfy this, the value of X should be equal to the greatest common divisor(GCD) of all Ci (1?i?N). If X is greater than 1, then print YES otherwise print NO.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void splitArray( int arr[], int N)
{
map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
int G = 0;
for ( auto i : mp) {
G = gcd(G, i.second);
}
if (G > 1)
cout << "YES" ;
else
cout << "NO" ;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
splitArray(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
void splitArray( int arr[], int N)
{
TreeMap<Integer, Integer> mp
= new TreeMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
int G = 0 ;
for (Map.Entry<Integer, Integer> m :
mp.entrySet())
{
Integer i = m.getValue();
G = gcd(G, i.intValue());
}
if (G > 1 )
System.out.print( "YES" );
else
System.out.print( "NO" );
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 };
int n = arr.length;
new GFG().splitArray(arr, n);
}
}
|
Python3
from collections import defaultdict
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def splitArray(arr, N):
mp = defaultdict( int )
for i in range (N):
mp[arr[i]] + = 1
G = 0
for i in mp:
G = gcd(G, mp[i])
if (G > 1 ):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ]
n = len (arr)
splitArray(arr, n)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void splitArray( int [] arr, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; ++i)
{
if (mp.ContainsKey(arr[i]) == true )
mp[arr[i]] += 1;
else
mp[arr[i]] = 1;
}
int G = 0;
foreach (KeyValuePair< int , int > i in mp)
{
G = gcd(G, i.Value);
}
if (G > 1)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 4, 3, 2, 1 };
int n = arr.Length;
splitArray(arr, n);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function splitArray(arr, N)
{
var mp = new Map();
for ( var i = 0; i < N; i++)
{
if (mp.has(arr[i]))
{
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.set(arr[i], 1);
}
}
var G = 0;
mp.forEach((value, key) => {
G = gcd(G, value);
});
if (G > 1)
document.write( "YES" );
else
document.write( "NO" );
}
var arr = [ 1, 2, 3, 4, 4, 3, 2, 1 ];
var n = arr.length;
splitArray(arr, n);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Last Updated :
14 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...