Skip to content
Related Articles
Split an array into equal length subsequences consisting of equal elements only
• Last Updated : 01 Feb, 2021

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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// 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` `    ``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 Code` `int` `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);`   `    ``splitArray(arr, n);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `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 mp` `            ``= ``new` `TreeMap();`   `        ``// 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 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`

## Python3

 `# Python3 program for the above approach` `from` `collections ``import` `defaultdict`   `# Function to find the GCD` `# of two numbers a and b` `def` `gcd(a, 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` `def` `splitArray(arr, N):` `    `  `    ``# Store frequencies of` `    ``# array elements` `    ``mp ``=` `defaultdict(``int``)` `    `  `    ``# Traverse the array` `    ``for` `i ``in` `range``(N):` `        `  `        ``# Update frequency of arr[i]` `        ``mp[arr[i]] ``+``=` `1`   `    ``# Store the GCD of frequencies` `    ``# of all array elements` `    ``G ``=` `0`   `    ``# Traverse the map` `    ``for` `i ``in` `mp:`   `        ``# Update GCD` `        ``G ``=` `gcd(G, mp[i])`   `    ``# If the GCD is greater than 1,` `    ``# print YES otherwise print NO` `    ``if` `(G > ``1``):` `        ``print``(``"YES"``)` `    ``else``:` `        ``print``(``"NO"``)`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `    `  `    ``# Given array` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``4``, ``4``, ``3``, ``2``, ``1` `]`   `    ``# Store the size of the array` `    ``n ``=` `len``(arr)`   `    ``splitArray(arr, n)`   `# This code is contributed by chitranayal`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic; ` `using` `System.Linq;` `class` `GFG{` `    `  `// Function to find the GCD` `// of two numbers a and b` `static` `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` `static` `void` `splitArray(``int``[] arr, ``int` `n)` `{`   `    ``// Store frequencies of` `    ``// array elements` `    ``Dictionary<``int``, ` `             ``int``> mp = ``new` `Dictionary<``int``, ` `                                      ``int``>();`   `    ``// Traverse the array` `    ``for``(``int` `i = 0; i < n; ++i) ` `    ``{` `          `  `        ``// Update frequency of` `        ``// each array element` `        ``if` `(mp.ContainsKey(arr[i]) == ``true``)` `        ``mp[arr[i]] += 1;` `      ``else` `        ``mp[arr[i]] = 1;` `    ``}`   `    ``// Store the GCD of frequencies` `    ``// of all array elements` `    ``int` `G = 0;`   `    ``// Traverse the map` `    ``foreach` `(KeyValuePair<``int``, ``int``> i ``in` `mp)` `    ``{`   `        ``// Update GCD` `        ``G = gcd(G, i.Value);` `    ``}`   `    ``// If the GCD is greater than 1,` `    ``// print YES otherwise print NO` `    ``if` `(G > 1)` `        ``Console.Write(``"YES"``);` `    ``else` `        ``Console.Write(``"NO"``);` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `  `  `    ``// Given array` `    ``int``[] arr = { 1, 2, 3, 4, 4, 3, 2, 1 };`   `    ``// Store the size of the array` `    ``int` `n = arr.Length;` `    ``splitArray(arr, n);` `}` `}`   `// This code is contributed by sanjoy_62.`

Output:

`YES`

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :