检查是否可以将数组拆分为大小至少为 K 的严格递增的子集
给定一个大小为N的数组arr[]和一个整数K ,任务是检查是否可以将数组拆分为大小至少为K的严格递增的子集。如果可能,则打印“是”。否则,打印“否”。
例子:
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
方法:可以通过使用 Map 存储每个元素的频率并将数组划分为 X 个子集来解决该问题,其中 X 是元素在数组中出现最大次数的频率。请按照以下步骤解决问题:
- 初始化一个 Map 说m以存储元素的频率,并将变量mx初始化为0以存储数组arr[]中最大出现元素的频率。
- 遍历数组 arr[]使用变量i ,并将m[arr[i]]增加1并将mx的值更新为max(mx, m[arr[i]]) 。
- 现在,如果N/mx>= K则打印“是”,因为它是子集可以具有的最大元素数。
- 否则,打印“否”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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 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 m
= new HashMap();
// 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 m = new Dictionary();
// 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
输出
Yes
时间复杂度: O(N*log(N))
辅助空间: O(N)