Check if it’s possible to split the Array into strictly increasing subsets of size at least K
Last Updated :
18 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
string ifPossible( int arr[], int N, int K)
{
map< int , int > m;
int mx = 0;
for ( int i = 0; i < N; i++) {
m[arr[i]] += 1;
mx = max(mx, m[arr[i]]);
}
int sz = N / mx;
if (sz >= K) {
return "Yes" ;
}
else {
return "No" ;
}
}
int main()
{
int arr[] = { 5, 6, 4, 9, 12 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
cout << ifPossible(arr, N, K);
return 0;
}
|
Java
import java.util.HashMap;
public class GFG
{
static String ifPossible( int arr[], int N, int K)
{
HashMap<Integer, Integer> m
= new HashMap<Integer, Integer>();
int mx = 0 ;
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]));
}
int sz = N / mx;
if (sz >= K) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 4 , 9 , 12 };
int K = 2 ;
int N = arr.length;
System.out.println(ifPossible(arr, N, K));
}
}
|
Python3
def ifPossible(arr, N, K):
m = {}
mx = 0
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]])
sz = N / / mx
if (sz > = K):
return "Yes"
else :
return "No"
if __name__ = = '__main__' :
arr = [ 5 , 6 , 4 , 9 , 12 ]
K = 2
N = len (arr)
print (ifPossible(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static string ifPossible( int []arr, int N, int K)
{
Dictionary< int , int > m = new Dictionary< int , int >();
int mx = 0;
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]]);
}
int sz = N / mx;
if (sz >= K) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main()
{
int []arr = { 5, 6, 4, 9, 12 };
int K = 2;
int N = arr.Length;
Console.Write(ifPossible(arr, N, K));
}
}
|
Javascript
<script>
function ifPossible(arr, N, K)
{
let m = new Map();
let mx = 0;
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]));
}
let sz = Math.floor(N / mx);
if (sz >= K) {
return "Yes" ;
}
else {
return "No" ;
}
}
let arr = [ 5, 6, 4, 9, 12 ];
let K = 2;
let N = arr.length;
document.write(ifPossible(arr, N, K));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...