Count N-length arrays of made up of elements not exceeding 2^K – 1 having maximum sum and Bitwise AND equal to 0
Last Updated :
05 Aug, 2021
Given two integers N and K, the task is to find the number of N-length arrays that satisfies the following conditions:
- The sum of the array elements is maximum possible.
- For every possible value of i ( 1 ? i ? N ), the ith element should lie between 0 and 2K – 1.
- Also, Bitwise AND of all the array elements should be 0.
Note: Since, the answer can be large, so print the answer modulo 10^9?+?7.
Examples :
Input : N=2 K =2
Output : 4
Explanation : The required arrays are ( {1, 2}, {2, 1}, {0, 3}, {3, 0} )
Input : N=1 K =1
Output : 1
Approach: The idea is to observe that if all the bits of all the elements in the array are 1, then the bitwise AND of all elements wont be 0 although the sum would be maximized. So for each bit, flip the 1 to 0 at each bit in at least one of the elements to make the bitwise AND equal to 0 and at the same time keeping the sum maximum. So for every bit, choose exactly one element and flip the bit there. Since there are K bits and N elements, the answer is just N^K. Follow the steps below to solve the problem:
- Define a function power(long long x, long long y, int p) and perform the following tasks:
- Initialize the variable res as 1 to store the result.
- Update the value of x as x%p.
- If x is equal to 0, then return 0.
- Iterate in a while loop till y is greater than 0 and perform the following tasks.
- If y is odd, then set the value of res as (res*x)%p.
- Divide y by 2.
- Set the value of x as (x*x)%p.
- Initialize the variable mod as 1e9+7.
- Initialize the variable ans as the value returned by the function power(N, K, mod).
- After performing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( long long x, unsigned int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int countArrays( int n, int k)
{
int mod = 1000000007;
int ans = power(n, k, mod);
return ans;
}
int main()
{
int n = 3, k = 5;
int ans = countArrays(n, k);
cout << ans << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int countArrays( int n, int k)
{
int mod = 1000000007 ;
int ans = power(n, k, mod);
return ans;
}
public static void main (String[] args)
{
int n = 3 , k = 5 ;
int ans = countArrays(n, k);
System.out.println(ans);
}
}
|
Python3
def power(x, y, p):
res = 1
x = x % p
if (x = = 0 ):
return 0
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def countArrays(n, k):
mod = 1000000007
ans = power(n, k, mod)
return ans
n = 3
k = 5
ans = countArrays(n, k)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) !=0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int countArrays( int n, int k)
{
int mod = 1000000007;
int ans = power(n, k, mod);
return ans;
}
public static void Main()
{
int n = 3, k = 5;
int ans = countArrays(n, k);
Console.Write(ans);
}
}
|
Javascript
<script>
function power(x, y, p) {
let res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function countArrays(n, k) {
let mod = 1000000007;
let ans = power(n, k, mod);
return ans;
}
let n = 3, k = 5;
let ans = countArrays(n, k);
document.write(ans);
</script>
|
Time Complexity: O(log(K))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...