

Count N-length arrays of made up of elements not exceeding 2^K - 1 having maximu...
source link: https://www.geeksforgeeks.org/count-n-length-arrays-of-made-up-of-elements-not-exceeding-2k-1-having-maximum-sum-and-bitwise-and-equal-to-0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
- Python3
- Javascript
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the power of n^k % p int power( long long x, unsigned int y, int p) { int res = 1; // Update x if it is more // than or equal to p x = x % p; // In case x is divisible by p; if (x == 0) return 0; while (y > 0) { // If y is odd, multiply // x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Function to count the number of // arrays satisfying required conditions int countArrays( int n, int k) { int mod = 1000000007; // Calculating N^K int ans = power(n, k, mod); return ans; } // Driver Code int main() { int n = 3, k = 5; int ans = countArrays(n, k); cout << ans << endl; return 0; } |
243
Time Complexity: O(log(K))
Auxiliary Space: O(1)
Recommend
-
37
The C99 (ISO/IEC 9899:1999) standard of C introduced a new, powerful feature called Variable Length Arrays (VLAs). The size of an array with automatic storage duration (i.e. stack allocated) can be determined at run time....
-
9
Variable-length arrays and the max() mess This article brought to you by LWN subscribersSubscribers to LWN.net made this article — and everything that surrounds it — possible. If you appreciate our...
-
13
Number of non-decreasing sub-arrays of length K Last Updated...
-
5
Improve Article Count all N-length arrays made up of distinct consecutive elements whose first and last elements are equalLast Updated : 27 Apr, 2021Given two integers M...
-
11
What is the best way to implement variable-length arrays? advertisements I want to store a large result set fro...
-
7
Length of the largest subarray with contiguous elementsSkip to content
-
7
Maximize the smallest array element by incrementing all elements in a K-length subarray by 1 exactly M timesDifficulty Level : Medium...
-
11
Generate an N-length array having length of non-decreasing subarrays maximized and minimum difference between first and last array elementsLast Updated : 30 Jul, 2021Given...
-
6
Search Questions and Answers
-
8
Count the number of arrays formed by rearranging elements such that one adjacent element is multiple of other...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK