Count of N-size maximum sum Arrays with elements in range [0, 2^K – 1] and Bitwise AND equal to 0
Last Updated :
16 Jul, 2021
Given two positive integers N and K, the task is to find the number of arrays of size N such that each array element lies over the range [0, 2K - 1] with the maximum sum of array element having Bitwise AND of all array elements 0.
Examples:
Input: N = 2 K = 2
Output: 4
Explanation:
The possible arrays with maximum sum having the Bitwise AND of all array element as 0 {0, 3}, {3, 0}, {1, 2}, {2, 1}. The count of such array is 4.
Input: N = 5 K = 6
Output: 15625
Approach: The given problem can be solved by observing the fact that as the Bitwise AND of the generated array should be 0, then for each i in the range [0, K - 1] there should be at least 1 element with an ith bit equal to 0 in its binary representation. Therefore, to maximize the sum of the array, it is optimal to have exactly 1 element with the ith bit unset.
Hence, for each of the K bits, there are NC1 ways to make it unset in 1 array element. Therefore, the resultant count of an array having the maximum sum is given by NK.
Below is the implementation of the approach :
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the value of X to
// the power Y
int power(int x, unsigned int y)
{
// Stores the value of X^Y
int res = 1;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y & 1)
res = res * x;
// Update the value of y and x
y = y >> 1;
x = x * x;
}
// Return the result
return res;
}
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
void countArrays(int N, int K)
{
// Print the value of N^K
cout << int(power(N, K));
}
// Driver Code
int main()
{
int N = 5, K = 6;
countArrays(N, K);
return 0;
}
Java
// Java program for the above approach
public class GFG
{
// Function to find the value of X to
// the power Y
static int power(int x, int y)
{
// Stores the value of X^Y
int res = 1;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y%2!=0)
res = res * x;
// Update the value of y and x
y = y >> 1;
x = x * x;
}
// Return the result
return res;
}
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
static void countArrays(int N, int K)
{
// Print the value of N^K
System.out.println((int)(power(N, K)));
}
// Driver Code
public static void main(String args[])
{
int N = 5, K = 6;
countArrays(N, K);
}
}
// This code is contributed by SoumikMondal
Python3
# Python Program for the above approach
# Function to find the value of X to
# the power Y
def power(x, y):
# Stores the value of X^Y
res = 1
while (y > 0):
# If y is odd, multiply x
# with result
if (y & 1):
res = res * x
# Update the value of y and x
y = y >> 1
x = x * x
# Return the result
return res
# Function to count number of arrays
# having element over the range
# [0, 2^K - 1] with Bitwise AND value
# 0 having maximum possible sum
def countArrays(N, K):
# Print the value of N^K
print(power(N, K))
# Driver Code
N = 5;
K = 6;
countArrays(N, K)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the value of X to
// the power Y
static int power(int x, int y)
{
// Stores the value of X^Y
int res = 1;
while (y > 0)
{
// If y is odd, multiply x
// with result
if (y % 2 != 0)
res = res * x;
// Update the value of y and x
y = y >> 1;
x = x * x;
}
// Return the result
return res;
}
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
static void countArrays(int N, int K)
{
// Print the value of N^K
Console.WriteLine((int)(power(N, K)));
}
// Driver Code
public static void Main()
{
int N = 5, K = 6;
countArrays(N, K);
}
}
// This code is contributed by subhammahato348
JavaScript
<script>
// JavaScript Program for the above approach
// Function to find the value of X to
// the power Y
function power(x, y) {
// Stores the value of X^Y
let res = 1;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y & 1)
res = res * x;
// Update the value of y and x
y = y >> 1;
x = x * x;
}
// Return the result
return res;
}
// Function to count number of arrays
// having element over the range
// [0, 2^K - 1] with Bitwise AND value
// 0 having maximum possible sum
function countArrays(N, K) {
// Print the value of N^K
document.write(power(N, K));
}
// Driver Code
let N = 5, K = 6;
countArrays(N, K);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(log K)
Auxiliary Space: O(1)
Similar Reads
Count N-length arrays of made up of elements not exceeding 2^K - 1 having maximum sum and Bitwise AND equal to 0 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 ele
7 min read
Maximize total set bits of elements in N sized Array with sum M Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1,
8 min read
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum Given an array arr[] consisting of N integers and integer K, the task is to find the smallest integer X with exactly K set bits such that the sum of Bitwise AND of X with every array element arr[i] is maximum. Examples: Input: arr[] = {3, 4, 5, 1}, K = 1Output: 4Explanation: Consider the value of X
8 min read
Generate an array having sum of Bitwise OR of same-indexed elements with given array equal to K Given an array arr[] consisting of N integers and an integer K, the task is to print an array generated such that the sum of Bitwise OR of same indexed elements of the generated array with the given array is equal to K. If it is not possible to generate such an array, then print "-1". Examples: Inpu
7 min read
Maximum set bit sum in array without considering adjacent elements Given an array of integers arr[]. The task is to find the maximum sum of set bits(of the array elements) without adding the set bits of adjacent elements of the array. Examples: Input : arr[] = {1, 2, 4, 5, 6, 7, 20, 25} Output : 9 Input : arr[] = {5, 7, 9, 5, 13, 7, 20, 25} Output : 11 Approach: Fi
8 min read
Count of subsequences with a sum in range [L, R] and difference between max and min element at least X Given an array arr[] consisting of N positive integers and 3 integers L, R, and X, the task is to find the number of subsequences of size atleast 2 with a sum in the range [L, R], and the difference between the maximum and minimum element is at least X. (Nâ¤15) Examples: Input: arr[] = {1 2 3}, L = 5
9 min read