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++
// 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>
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Similar Reads
Split the array into N subarrays where each subarray has size 1 or sum at least K Given an array arr[] of size N and integer K. Task for this problem is to check if given array can be split into N continuous subarrays by performing given operations. In one operation choose existing array which may be result of previous operation and split it into two subarrays with each of subarr
10 min read
Split array into maximum possible subsets having product of their length with the maximum element at least K Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets. Examples: Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: 3Explan
5 min read
Maximum number of subsets an array can be split into such that product of their minimums with size of subsets is at least K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of disjoint subsets that the given array can be split into such that the product of the minimum element of each subset with the size of the subset is at least K. Examples: Input: arr[] = {7, 11, 2,
8 min read
Split Array into Consecutive Subsequences of Size K or more. Given an integer array arr[] that is sorted in increasing order and an integer K. Check whether it is possible to split arr[] into one or more subsequences such that both of the following conditions are true: Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one mor
10 min read
Split array into minimum number of subsets having maximum pair sum at most K Given an array, arr[] of size n, and an integer k, the task is to partition the array into the minimum number of subsets such that the maximum pair sum in each subset is less than or equal to k.Examples:Input: arr[] = [1, 2, 3, 4, 5], k = 5Output: 3Explanation: Subset having maximum pair sum less th
4 min read