给定一个大小为N的数组arr[]和一个整数K ,任务是将数组拆分为K 个不相交的子集,使得所有K个子集的并集等于给定的数组。
例子:
Input : arr[]= {2, 3}, K=2
Output: 4
Explanations:
Possible ways to partition the array into K(=2) subsets are:{ {{}, {2, 3}}, {{2}, {3}}, {{3}, {2}}, {{2, 3}, {}} }.
Therefore, the required output is 4.
Input: arr[] = {2, 2, 3, 3}, K = 3
Output: 9
方法:可以根据以下观察解决问题:
The total number of ways to place an element into any one of the K subsets = K.
Therefore, the total number of ways to place all distinct elements of the given array into K subsets = K × K × …..× K(M times) = KM
Where M = total number of distinct elements in the given array.
请按照以下步骤解决问题:
- 计算不同的元素,比如给定数组中存在的 M。
- 打印 power(K, M) 的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to get
// the value of pow(K, M)
int power(int K, int M)
{
// Stores value of pow(K, M)
int res = 1;
// Calculate value of pow(K, N)
while (M > 0) {
// If N is odd, update
// res
if ((M & 1) == 1) {
res = (res * K);
}
// Update M to M / 2
M = M >> 1;
// Update K
K = (K * K);
}
return res;
}
// Function to print total ways
// to split the array that
// satisfies the given condition
int cntWays(int arr[], int N,
int K)
{
// Stores total ways that
// satisfies the given
// condition
int cntways = 0;
// Stores count of distinct
// elements in the given arr
int M = 0;
// Store distinct elements
// of the given array
unordered_set st;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Insert current element
// into set st.
st.insert(arr[i]);
}
// Update M
M = st.size();
// Update cntways
cntways = power(K, M);
return cntways;
}
// Driver Code
int main()
{
int arr[] = { 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
cout << cntWays(arr, N, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to get
// the value of pow(K, M)
static int power(int K, int M)
{
// Stores value of pow(K, M)
int res = 1;
// Calculate value of pow(K, N)
while (M > 0)
{
// If N is odd, update
// res
if ((M & 1) == 1)
{
res = (res * K);
}
// Update M to M / 2
M = M >> 1;
// Update K
K = (K * K);
}
return res;
}
// Function to print total ways
// to split the array that
// satisfies the given condition
static int cntWays(int arr[], int N,
int K)
{
// Stores total ways that
// satisfies the given
// condition
int cntways = 0;
// Stores count of distinct
// elements in the given arr
int M = 0;
// Store distinct elements
// of the given array
Set st = new HashSet();
// Traverse the given array
for(int i = 0; i < N; i++)
{
// Insert current element
// into set st.
st.add(arr[i]);
}
// Update M
M = st.size();
// Update cntways
cntways = power(K, M);
return cntways;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 2, 3 };
int N = arr.length;
int K = 2;
System.out.println(cntWays(arr, N, K));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to get
# the value of pow(K, M)
def power(K, M):
# Stores value of pow(K, M)
res = 1
# Calculate value of pow(K, N)
while (M > 0):
# If N is odd, update
# res
if ((M & 1) == 1):
res = (res * K)
# Update M to M / 2
M = M >> 1
# Update K
K = (K * K)
return res
# Function to print total ways
# to split the array that
# satisfies the given condition
def cntWays(arr, N, K):
# Stores total ways that
# satisfies the given
# condition
cntways = 0
# Stores count of distinct
# elements in the given arr
M = 0
# Store distinct elements
# of the given array
st = set()
# Traverse the given array
for i in range(N):
# Insert current element
# into set st.
st.add(arr[i])
# Update M
M = len(st)
# Update cntways
cntways = power(K, M)
return cntways
# Driver Code
if __name__ == '__main__':
arr = [ 2, 3 ]
N = len(arr)
K = 2
print(cntWays(arr, N, K))
# This code is contributed by math_lover
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to get
// the value of pow(K, M)
static int power(int K, int M)
{
// Stores value of pow(K, M)
int res = 1;
// Calculate value of pow(K, N)
while (M > 0)
{
// If N is odd, update
// res
if ((M & 1) == 1)
{
res = (res * K);
}
// Update M to M / 2
M = M >> 1;
// Update K
K = (K * K);
}
return res;
}
// Function to print total ways
// to split the array that
// satisfies the given condition
static int cntWays(int[] arr, int N,
int K)
{
// Stores total ways that
// satisfies the given
// condition
int cntways = 0;
// Stores count of distinct
// elements in the given arr
int M = 0;
// Store distinct elements
// of the given array
HashSet st = new HashSet();
// Traverse the given array
for(int i = 0; i < N; i++)
{
// Insert current element
// into set st.
st.Add(arr[i]);
}
// Update M
M = st.Count;
// Update cntways
cntways = power(K, M);
return cntways;
}
// Driver code
public static void Main()
{
int[] arr = { 2, 3 };
int N = arr.Length;
int K = 2;
Console.WriteLine(cntWays(arr, N, K));
}
}
// This code is contributed by code_hunt
Javascript
4
时间复杂度: O(log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。