Count of Arrays of size N with elements in range [0, (2^K)-1] having maximum sum & bitwise AND 0
Last Updated :
16 Jun, 2022
Given two integers N and K, The task is to find the count of all possible arrays of size N with maximum sum & bitwise AND of all elements as 0. Also, elements should be within the range of 0 to 2K-1.
Examples:
Input: N = 3, K = 2
Output: 9
Explanation: 22 - 1 = 3, so elements of arrays should be between 0 to 3. All possible arrays are- [3, 3, 0], [1, 2, 3], [3, 0, 3], [0, 3, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] Bitwise AND of all the arrays is 0 & also the sum = 6 is maximum
Input: N = 2, K = 2
Output: 4
Explanation: All possible arrays are - [3, 0], [0, 3], [1, 2], [2, 1]
Approach: To better understand the approach, refer to the steps below:
- As the maximum possible element is (2K - 1) and the size of the array is N so if all elements of the array are equal to the maximum element then the sum will be maximum i.e. N * (2K - 1) = N * ( 20 + 21 + ................. + 2K - 1 ). Keep in mind that there are K bits in ( 2K - 1) and all bits are set.
- So now to make bitwise AND of all elements equal to 0 we have to unset each bit at least in one element. Also, we can not unset the same bit in more than 1 element because in that case sum will not be maximum.
- After unsetting each bit in one element, the maximum possible Sum = N * ( 20 + 21 + ......... + 2K - 1 ) - ( 20 + 21 + .......... + 2K - 1 ) = (N * (2K -1 )) - (2K -1)= (N - 1) * (2K - 1).
- Now the goal is to find all the ways through which we can unset all K bits in at least one element. You can see that for unsetting a single bit you have N options i.e. you can unset it in any one of N elements. So the total way for unsetting K bits will be NK. This is our final answer.
Illustration:
Let N = 3, K = 3
- Make all elements of the array equal to 23 - 1 = 7. The array will be [7, 7, 7]. Take binary representation of all elements : [111, 111, 111].
- Unset each bit in exactly one element. Suppose we unset the 3rd bit of the 1st element and the 1st two bits of the 2nd element. array becomes [110, 001, 111] = [6, 1, 7]. This is one of the valid arrays. You can generate all arrays in such a way.
- The total number of arrays will be 33 = 27.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Iterative Function to calculate
// (x^y) in O(log y)
int power(int x, int y)
{
// Initialize answer
int res = 1;
// Check till the number becomes zero
while (y) {
// If y is odd, multiply x with result
if (y % 2 == 1)
res = (res * x);
// y = y/2
y = y >> 1;
// Change x to x^2
x = (x * x);
}
return res;
}
// Driver Code
int main()
{
int N = 3, K = 2;
cout << power(N, K);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
public class GFG
{
// Iterative Function to calculate
// (x^y) in O(log y)
static int power(int x, int y)
{
// Initialize answer
int res = 1;
// Check till the number becomes zero
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1)
res = (res * x);
// y = y/2
y = y >> 1;
// Change x to x^2
x = (x * x);
}
return res;
}
// Driver Code
public static void main(String args[])
{
int N = 3, K = 2;
System.out.print(power(N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 program for the above approach
# Iterative Function to calculate
# (x^y) in O(log y)
def power(x, y):
# Initialize answer
res = 1
# Check till the number becomes zero
while (y):
# If y is odd, multiply x with result
if (y % 2 == 1):
res = (res * x)
# y = y/2
y = y >> 1
# Change x to x^2
x = (x * x)
return res
# Driver Code
if __name__ == "__main__":
N = 3
K = 2
print(power(N, K))
# This code is contributed by rakeshsahni
C#
// C# code to implement above approach
using System;
class GFG
{
// Iterative Function to calculate
// (x^y) in O(log y)
static int power(int x, int y)
{
// Initialize answer
int res = 1;
// Check till the number becomes zero
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1)
res = (res * x);
// y = y/2
y = y >> 1;
// Change x to x^2
x = (x * x);
}
return res;
}
// Driver code
public static void Main()
{
int N = 3, K = 2;
Console.Write(power(N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Iterative Function to calculate
// (x^y) in O(log y)
function power(x, y) {
// Initialize answer
let res = 1;
// Check till the number becomes zero
while (y) {
// If y is odd, multiply x with result
if (y % 2 == 1)
res = (res * x);
// y = y/2
y = y >> 1;
// Change x to x^2
x = (x * x);
}
return res;
}
// Driver Code
let N = 3, K = 2;
document.write(power(N, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(logK)
Auxiliary Space: O(1)
Similar Reads
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array Given an array arr[] of size N, the task is to count the number of pairs from the given array such that the Bitwise AND (&) of each pair is greater than its Bitwise XOR(^). Examples : Input: arr[] = {1, 2, 3, 4} Output:1Explanation:Pairs that satisfy the given conditions are: (2 & 3) > (2
6 min read
Sum of Bitwise-OR of all subarrays of a given Array | Set 2 Give an array of positive integers. The task is to find the total sum after performing the bitwise OR operation on all the sub-arrays of the given array. Examples: Input : arr[] = {1, 2, 3, 4, 5} Output : 71 Input : arr[] = {6, 5, 4, 3, 2} Output : 84 Explanation: Simple Approach: A simple approach
12 min read
Count total set bits in all numbers from 1 to n | Set 2 Given a positive integer N, the task is to count the sum of the number of set bits in the binary representation of all the numbers from 1 to N.Examples: Input: N = 3 Output: 4 DecimalBinarySet Bit Count101121013112 1 + 1 + 2 = 4Input: N = 16 Output: 33 Recommended PracticeCount total set bitsTry It!
11 min read
Count total set bits in all numbers from 1 to N | Set 3 Given a positive integer N, the task is to count the total number of set bits in binary representation of all the numbers from 1 to N. Examples: Input: N = 3 Output: 4 setBits(1) + setBits(2) + setBits(3) = 1 + 1 + 2 = 4 Input: N = 6 Output: 9 Approach: Solution to this problem has been published in
12 min read
Sum of Max of Subarrays Given an array arr[], the task is to find the sum of the maximum elements of every possible non-empty sub-arrays of the given array arr[].Examples: Input: arr[] = [1, 3, 2]Output: 15Explanation: All possible non-empty subarrays of [1, 3, 2] are {1}, {3}, {2}, {1, 3}, {3, 2} and {1, 3, 2}. The maximu
12 min read