Count ways to split array into K non-intersecting subsets
Given an array, arr[] of size N and an integer K, the task is to split the array into K non-intersecting subsets such that union of all the K subsets is equal to the given array.
Examples:
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
Approach: The problem can be solved based on the following observations:
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.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int K, int M)
{
int res = 1;
while (M > 0) {
if ((M & 1) == 1) {
res = (res * K);
}
M = M >> 1;
K = (K * K);
}
return res;
}
int cntWays( int arr[], int N,
int K)
{
int cntways = 0;
int M = 0;
unordered_set< int > st;
for ( int i = 0; i < N; i++) {
st.insert(arr[i]);
}
M = st.size();
cntways = power(K, M);
return cntways;
}
int main()
{
int arr[] = { 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 2;
cout << cntWays(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int power( int K, int M)
{
int res = 1 ;
while (M > 0 )
{
if ((M & 1 ) == 1 )
{
res = (res * K);
}
M = M >> 1 ;
K = (K * K);
}
return res;
}
static int cntWays( int arr[], int N,
int K)
{
int cntways = 0 ;
int M = 0 ;
Set<Integer> st = new HashSet<Integer>();
for ( int i = 0 ; i < N; i++)
{
st.add(arr[i]);
}
M = st.size();
cntways = power(K, M);
return cntways;
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 };
int N = arr.length;
int K = 2 ;
System.out.println(cntWays(arr, N, K));
}
}
|
Python3
def power(K, M):
res = 1
while (M > 0 ):
if ((M & 1 ) = = 1 ):
res = (res * K)
M = M >> 1
K = (K * K)
return res
def cntWays(arr, N, K):
cntways = 0
M = 0
st = set ()
for i in range (N):
st.add(arr[i])
M = len (st)
cntways = power(K, M)
return cntways
if __name__ = = '__main__' :
arr = [ 2 , 3 ]
N = len (arr)
K = 2
print (cntWays(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int power( int K, int M)
{
int res = 1;
while (M > 0)
{
if ((M & 1) == 1)
{
res = (res * K);
}
M = M >> 1;
K = (K * K);
}
return res;
}
static int cntWays( int [] arr, int N,
int K)
{
int cntways = 0;
int M = 0;
HashSet< int > st = new HashSet< int >();
for ( int i = 0; i < N; i++)
{
st.Add(arr[i]);
}
M = st.Count;
cntways = power(K, M);
return cntways;
}
public static void Main()
{
int [] arr = { 2, 3 };
int N = arr.Length;
int K = 2;
Console.WriteLine(cntWays(arr, N, K));
}
}
|
Javascript
<script>
function power(K, M)
{
var res = 1;
while (M > 0) {
if ((M & 1) == 1) {
res = (res * K);
}
M = M >> 1;
K = (K * K);
}
return res;
}
function cntWays( arr, N, K)
{
var cntways = 0;
var M = 0;
var st = new Set();
for ( var i = 0; i < N; i++) {
st.add(arr[i]);
}
M = st.size;
cntways = power(K, M);
return cntways;
}
var arr = [ 2, 3 ];
var N = arr.length;
var K = 2;
document.write( cntWays(arr, N, K));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
03 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...