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:
- Create a hashmap, mp, to store the frequencies of all the elements of the array, arr[].
- Store the greatest common divisor of all the frequencies in mp in a variable X.
- If X is greater than 1, then the answer is YES.
- Otherwise, the answer is NO.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to find the GCD// of two numbers a and bint gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b);}// Function to check if it is possible to// split the array into equal length subsequences// such that all elements in the subsequence are equalvoid splitArray(int arr[], int N){ // Store frequencies of // array elements map<int, int> mp; // Traverse the array for (int i = 0; i < N; i++) { // Update frequency of arr[i] mp[arr[i]]++; } // Store the GCD of frequencies // of all array elements int G = 0; // Traverse the map for (auto i : mp) { // Update GCD G = gcd(G, i.second); } // If the GCD is greater than 1, // print YES otherwise print NO if (G > 1) cout << "YES"; else cout << "NO";}// Driver Codeint main(){ // Given array int arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 }; // Store the size of the array int n = sizeof(arr) / sizeof(arr[0]); splitArray(arr, n); return 0;} |
Java
// Java program to implement// the above approachimport java.io.*;import java.lang.*;import java.util.*;class GFG { // Function to find the GCD // of two numbers a and b int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // Function to check if it is possible to // split the array into equal length subsequences // such that all elements in the subsequence are equal void splitArray(int arr[], int N) { // Store frequencies of // array elements TreeMap<Integer, Integer> mp = new TreeMap<Integer, Integer>(); // Traverse the array for (int i = 0; i < N; i++) { // Update frequency of arr[i] if (mp.containsKey(arr[i])) { mp.put(arr[i], mp.get(arr[i]) + 1); } else { mp.put(arr[i], 1); } } // Store the GCD of frequencies // of all array elements int G = 0; // Traverse the map for (Map.Entry<Integer, Integer> m : mp.entrySet()) { // update gcd Integer i = m.getValue(); G = gcd(G, i.intValue()); } // If the GCD is greater than 1, // print YES otherwise print NO if (G > 1) System.out.print("YES"); else System.out.print("NO"); } // Driver Code public static void main(String[] args) { // Given array int[] arr = new int[] { 1, 2, 3, 4, 4, 3, 2, 1 }; // Store the size of the array int n = arr.length; new GFG().splitArray(arr, n); }}// This code is contributed by abhishekgiri1 |
YES
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

