Product of all sorted subsets of size K using elements whose index divide K completely
Given an integer array arr[] of N distinct elements and a positive integer K ( K <= N ). The task is to calculate the product of all sorted subsets of size K, from the given array, using elements whose index divide K completely.
Note: As the answer can be very large, print it modulo 10^9+7.
Examples:
Input: arr[] = {4, 7, 5, 9, 3}, K = 4
Output: 808556639
Explanation:
In this case there are 5 possible sets:
{4, 7, 5, 9} -> 180 (Sorted order {4, 5, 7, 9}: Index 1, 2 and 4 divides M, so value of set is 4 * 5 * 9 = 180)
{4, 7, 9, 3} -> 108
{4, 5, 9, 3} -> 108
{4, 7, 5, 3} -> 84
{7, 5, 9, 3} -> 135
Total value = ( 180 * 108 * 108 * 84 * 135 ) % (109+7) = 808556639
Input: arr[] = {7, 8, 9}, K = 2
Output: 254016
Naive Approach:
We can find all the subsets of size K then sort the subsets and find the value of each subset and multiply it to get the final answer.
As there can be 2N subsets, this approach will not be very efficient for a large value of N.
Efficient Approach:
- The idea is not to create all the subsets to find the answer but to calculate the count of each element in all the subsets. If we find the count of each element in all subsets then the answer will be
- For finding count of the arr[i], we have to find total number of the different subsets that can be formed by placing arr[i] at every possible index that divides K completely.
- Number of sets formed by placing arr[i] at jth index( j divides K completely) will be:
( Total no. of elements smaller than arr[i] ) Cj-1 * ( Total no. of elements greater than arr[i] ) Ck-j
- As the count of any element may be very large, so for finding ( arr[i]( count of arr[i])) % ( 109 + 7 ) we have to use Fermat’s little theorem that is
{ a (p – 1) mod p = 1 } => { ( ax ) % p = ( a( x % p-1 )) % p }.
So, by using Fermat’s little theorem
( arr[i] (count of arr[i] )) % ( 109 + 7 ) => ( arr[i] (count of arr[i] % 109 + 6 )) % ( 109 + 7 ).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int p = 1000000007;
long long int power( long long int x,
long long int y,
long long int p)
{
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void nCr( long long int n, long long int p,
int f[][100], int m)
{
for ( long long int i = 0; i <= n; i++) {
for ( long long int j = 0; j <= m; j++) {
if (j > i) {
f[i][j] = 0;
}
else if (j == 0 || j == i) {
f[i][j] = 1;
}
else {
f[i][j] = (f[i - 1][j]
+ f[i - 1][j - 1])
% p;
}
}
}
}
void ProductOfSubsets( int arr[], int n,
int m)
{
int f[n + 1][100];
nCr(n, p - 1, f, m);
sort(arr, arr + n);
long long int ans = 1;
for ( long long int i = 0; i < n; i++) {
long long int x = 0;
for ( long long int j = 1; j <= m; j++) {
if (m % j == 0) {
x = (x
+ (f[n - i - 1][m - j]
* f[i][j - 1])
% (p - 1))
% (p - 1);
}
}
ans
= ((ans * power(arr[i], x, p)) % p);
}
cout << ans << endl;
}
int main()
{
int arr[] = { 4, 5, 7, 9, 3 };
int K = 4;
int N = sizeof (arr) / sizeof (arr[0]);
ProductOfSubsets(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int p = 1000000007 ;
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static void nCr( int n, int p,
int f[][], int m)
{
for ( int i = 0 ; i <= n; i++)
{
for ( int j = 0 ; j <= m; j++)
{
if (j > i)
{
f[i][j] = 0 ;
}
else if (j == 0 || j == i)
{
f[i][j] = 1 ;
}
else
{
f[i][j] = (f[i - 1 ][j] +
f[i - 1 ][j - 1 ]) % p;
}
}
}
}
static void ProductOfSubsets( int arr[], int n,
int m)
{
int [][]f = new int [n + 1 ][ 100 ];
nCr(n, p - 1 , f, m);
Arrays.sort(arr);
long ans = 1 ;
for ( int i = 0 ; i < n; i++)
{
int x = 0 ;
for ( int j = 1 ; j <= m; j++)
{
if (m % j == 0 )
{
x = (x + (f[n - i - 1 ][m - j] *
f[i][j - 1 ]) %
(p - 1 )) %
(p - 1 );
}
}
ans = ((ans * power(arr[i], x, p)) % p);
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 7 , 9 , 3 };
int K = 4 ;
int N = arr.length;
ProductOfSubsets(arr, N, K);
}
}
|
Python3
p = 1000000007
def power(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def nCr(n, p, f, m):
for i in range (n + 1 ):
for j in range (m + 1 ):
if (j > i):
f[i][j] = 0
elif (j = = 0 or j = = i):
f[i][j] = 1
else :
f[i][j] = (f[i - 1 ][j] +
f[i - 1 ][j - 1 ]) % p
def ProductOfSubsets(arr, n, m):
f = [[ 0 for i in range ( 100 )]
for j in range (n + 1 )]
nCr(n, p - 1 , f, m)
arr.sort(reverse = False )
ans = 1
for i in range (n):
x = 0
for j in range ( 1 , m + 1 , 1 ):
if (m % j = = 0 ):
x = ((x + (f[n - i - 1 ][m - j] *
f[i][j - 1 ]) %
(p - 1 )) %
(p - 1 ))
ans = ((ans * power(arr[i], x, p)) % p)
print (ans)
if __name__ = = '__main__' :
arr = [ 4 , 5 , 7 , 9 , 3 ]
K = 4
N = len (arr);
ProductOfSubsets(arr, N, K)
|
C#
using System;
class GFG{
static int p = 1000000007;
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static void nCr( int n, int p,
int [,]f, int m)
{
for ( int i = 0; i <= n; i++)
{
for ( int j = 0; j <= m; j++)
{
if (j > i)
{
f[i, j] = 0;
}
else if (j == 0 || j == i)
{
f[i, j] = 1;
}
else
{
f[i, j] = (f[i - 1, j] +
f[i - 1, j - 1]) % p;
}
}
}
}
static void ProductOfSubsets( int []arr, int n,
int m)
{
int [,]f = new int [n + 1, 100];
nCr(n, p - 1, f, m);
Array.Sort(arr);
long ans = 1;
for ( int i = 0; i < n; i++)
{
int x = 0;
for ( int j = 1; j <= m; j++)
{
if (m % j == 0)
{
x = (x + (f[n - i - 1, m - j] *
f[i, j - 1]) %
(p - 1)) %
(p - 1);
}
}
ans = ((ans * power(arr[i], x, p)) % p);
}
Console.Write(ans + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 7, 9, 3 };
int K = 4;
int N = arr.Length;
ProductOfSubsets(arr, N, K);
}
}
|
Javascript
<script>
let p = 1000000007;
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function nCr(n, p, f, m)
{
for (let i = 0; i <= n; i++)
{
for (let j = 0; j <= m; j++)
{
if (j > i)
{
f[i][j] = 0;
}
else if (j == 0 || j == i)
{
f[i][j] = 1;
}
else
{
f[i][j] = (f[i - 1][j] +
f[i - 1][j - 1]) % p;
}
}
}
}
function ProductOfSubsets(arr, n, m)
{
let f = new Array(n+1);
for ( var i = 0; i < f.length; i++) {
f[i] = new Array(2);
}
nCr(n, p - 1, f, m);
arr.sort();
let ans = 1;
for (let i = 0; i < n; i++)
{
let x = 0;
for (let j = 1; j <= m; j++)
{
if (m % j == 0)
{
x = (x + (f[n - i - 1][m - j] *
f[i][j - 1]) %
(p - 1)) %
(p - 1);
}
}
ans = ((ans * power(arr[i], x, p)) % p);
}
document.write(ans + "<br/>" );
}
let arr = [ 4, 5, 7, 9, 3 ];
let K = 4;
let N = arr.length;
ProductOfSubsets(arr, N, K);
</script>
|
Time Complexity: (N * K)
Auxiliary Space: O(N * 100)
Last Updated :
03 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...