Check if it’s possible to split the Array into strictly increasing subsets of size at least K
Given an array arr[] of size N and an integer K, the task is to check whether it’s possible to split the array into strictly increasing subsets of size at least K. If it is possible then print “Yes“. Otherwise, print “No“.
Examples:
Input: arr[] = {5, 6, 4, 9, 12}, K = 2
Output: Yes
Explanation:
One possible way to split the array into subsets of at least size 2 is, {arr[2](=4), arr[0](=5)} and {arr[1](=6), arr[3](=9), arr[4](=12)}Input: arr[] = {5, 7, 7, 7}, K = 2
Output: No
Approach: The problem can be solved by using Map to store the frequency of every element and dividing the array into X subsets where X is the frequency of the element that occurs maximum number of times in the array. Follow the steps below to solve the problem:
- Initialize a Map say m to store the frequency of elements and also initialize a variable mx as 0 to store the frequency of maximum occurring element in the array arr[].
- Traverse the array arr[] using the variable i, and increment m[arr[i]] by 1 and update the value of mx to max(mx, m[arr[i]]).
- Now if N/mx>= K then prints “Yes” as it the maximum number of elements a subset can have.
- Otherwise, print “No“.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if it is possible // to split the array into strictly // increasing subsets of size atleast K string ifPossible( int arr[], int N, int K) { // Map to store frequency of elements map< int , int > m; // Stores the frequency of the maximum // occurring element in the array int mx = 0; // Traverse the array for ( int i = 0; i < N; i++) { m[arr[i]] += 1; mx = max(mx, m[arr[i]]); } // Stores the minimum count of elements // in a subset int sz = N / mx; // If sz is greater than k-1 if (sz >= K) { return "Yes" ; } // Otherwise else { return "No" ; } } // Driver Code int main() { // Given Input int arr[] = { 5, 6, 4, 9, 12 }; int K = 2; int N = sizeof (arr) / sizeof (arr[0]); // Function Call cout << ifPossible(arr, N, K); return 0; } |
Java
// Java approach for the above program import java.util.HashMap; public class GFG { // Function to check if it is possible // to split the array into strictly // increasing subsets of size atleast K static String ifPossible( int arr[], int N, int K) { // Map to store frequency of elements HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); // Stores the frequency of the maximum // occurring element in the array int mx = 0 ; // Traverse the array for ( int i = 0 ; i < N; i++) { m.put(arr[i], m.getOrDefault(arr[i], 0 ) + 1 ); mx = Math.max(mx, m.get(arr[i])); } // Stores the minimum count of elements // in a subset int sz = N / mx; // If sz is greater than k-1 if (sz >= K) { return "Yes" ; } // Otherwise else { return "No" ; } } // Driver code public static void main(String[] args) { // Given Input int arr[] = { 5 , 6 , 4 , 9 , 12 }; int K = 2 ; int N = arr.length; // Function Call System.out.println(ifPossible(arr, N, K)); } } // This code is contributed by abhinavjain194 |
Python3
# Python3 program for the above approach # Function to check if it is possible # to split the array into strictly # increasing subsets of size atleast K def ifPossible(arr, N, K): # Map to store frequency of elements m = {} # Stores the frequency of the maximum # occurring element in the array mx = 0 # Traverse the array for i in range (N): if arr[i] in m: m[arr[i]] + = 1 else : m[arr[i]] = 1 mx = max (mx, m[arr[i]]) # Stores the minimum count of elements # in a subset sz = N / / mx # If sz is greater than k-1 if (sz > = K): return "Yes" # Otherwise else : return "No" # Driver Code if __name__ = = '__main__' : # Given Input arr = [ 5 , 6 , 4 , 9 , 12 ] K = 2 N = len (arr) # Function Call print (ifPossible(arr, N, K)) # This code is contributed by bgangwar59 |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to check if it is possible // to split the array into strictly // increasing subsets of size atleast K static string ifPossible( int []arr, int N, int K) { // Map to store frequency of elements Dictionary< int , int > m = new Dictionary< int , int >(); // Stores the frequency of the maximum // occurring element in the array int mx = 0; // Traverse the array for ( int i = 0; i < N; i++) { if (m.ContainsKey(arr[i])) m[arr[i]] += 1; else m.Add(arr[i],1); mx = Math.Max(mx, m[arr[i]]); } // Stores the minimum count of elements // in a subset int sz = N / mx; // If sz is greater than k-1 if (sz >= K) { return "Yes" ; } // Otherwise else { return "No" ; } } // Driver Code public static void Main() { // Given Input int []arr = { 5, 6, 4, 9, 12 }; int K = 2; int N = arr.Length; // Function Call Console.Write(ifPossible(arr, N, K)); } } // This code is contributed by SURENDRA_GANGWAR. |
Javascript
<script> // JavaScript program for the above approach // Function to check if it is possible // to split the array into strictly // increasing subsets of size atleast K function ifPossible(arr, N, K) { // Map to store frequency of elements let m = new Map(); // Stores the frequency of the maximum // occurring element in the array let mx = 0; // Traverse the array for (let i = 0; i < N; i++) { m[arr[i]] += 1; if (m.has(arr[i])){ m.set(arr[i], m.get([arr[i]]) + 1) } else { m.set(arr[i], 1) } mx = Math.max(mx, m.get(arr[i])); } // Stores the minimum count of elements // in a subset let sz = Math.floor(N / mx); // If sz is greater than k-1 if (sz >= K) { return "Yes" ; } // Otherwise else { return "No" ; } } // Driver Code // Given Input let arr = [ 5, 6, 4, 9, 12 ]; let K = 2; let N = arr.length; // Function Call document.write(ifPossible(arr, N, K)); </script> |
Yes
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Please Login to comment...