Get the best out of our app
GFG App
Open App
Browser
Continue

# Find maximum product of Bitwise AND and Bitwise OR of K-size subarray

Given an array arr[] containing N integers and an integer K, the task is to find the maximum value of the product of Bitwise AND and Bitwise OR of all elements of a K-sized subarray.

Example:

Input: arr[] = {1, 2, 3, 4}, K = 2
Output: 6
Explanation: Bitwise AND and Bitwise XOR of all K-sized subarrays is {(0, 3), (2, 3), (0, 7)} respectively. Therefore the maximum possible value of their product is 2 * 3 = 6 which is the required answer.

Input: arr[] = {6, 7, 7, 10, 8, 2}, K = 3
Output: 42

Approach: The given problem can be solved using the sliding window technique. The idea is to maintain the value of bitwise AND and bitwise OR for the K-sized subarray windows, which can be done by maintaining an array storing the count of each bit over all the elements of the current window. If the count of ith bit is K, that bit must be included in the bitwise AND and if the count of the bit is greater than 1, it must be included in the bitwise OR. Store the maximum value of their product over all the windows in a variable ans which is the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to convert bit array to` `// decimal number representing OR` `int` `build_or(``int` `bit[])` `{` `    ``int` `ans = 0;`   `    ``for` `(``int` `i = 0; i < 32; i++)` `        ``if` `(bit[i])` `            ``ans += (1 << i);`   `    ``return` `ans;` `}`   `// Function to convert bit array to` `// decimal number representing AND` `int` `build_and(``int` `bit[], ``int` `k)` `{` `    ``int` `ans = 0;`   `    ``for` `(``int` `i = 0; i < 32; i++)` `        ``if` `(bit[i] == k)` `            ``ans += (1 << i);`   `    ``return` `ans;` `}`   `// Function to find maximum value of` `// AND * OR over all K sized subarrays` `int` `maximizeAndOr(``int` `arr[], ``int` `N, ``int` `K)` `{` `    ``// Maintain an integer array bit[]` `    ``// of size 32 all initialized to 0` `    ``int` `bit[32] = { 0 };`   `    ``// Create a sliding window of size k` `    ``for` `(``int` `i = 0; i < K; i++) {` `        ``for` `(``int` `j = 0; j < 32; j++) {` `            ``if` `(arr[i] & (1 << j))` `                ``bit[j]++;` `        ``}` `    ``}`   `    ``int` `ans = build_and(bit, K) * build_or(bit);`   `    ``for` `(``int` `i = K; i < N; i++) {`   `        ``// Perform operation for` `        ``// removed element` `        ``for` `(``int` `j = 0; j < 32; j++) {` `            ``if` `(arr[i - K] & (1 << j))` `                ``bit[j]--;` `        ``}`   `        ``// Perform operation for` `        ``// added_element` `        ``for` `(``int` `j = 0; j < 32; j++) {` `            ``if` `(arr[i] & (1 << j))` `                ``bit[j]++;` `        ``}`   `        ``// Taking maximum value` `        ``ans = max(ans, build_and(bit, K) ` `                  ``* build_or(bit));` `    ``}`   `    ``return` `ans;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `arr[] = { 6, 7, 7, 10, 8, 2 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``int` `K = 3;`   `    ``cout << maximizeAndOr(arr, N, K);` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to convert bit array to` `// decimal number representing OR` `static` `int` `build_or(``int``[] bit)` `{` `    ``int` `ans = ``0``;`   `    ``for``(``int` `i = ``0``; i < ``32``; i++)` `        ``if` `(bit[i] > ``0``)` `            ``ans += (``1` `<< i);` `            `  `    ``return` `ans;` `}`   `// Function to convert bit array to` `// decimal number representing AND` `static` `int` `build_and(``int``[] bit, ``int` `k)` `{` `    ``int` `ans = ``0``;`   `    ``for``(``int` `i = ``0``; i < ``32``; i++)` `        ``if` `(bit[i] == k)` `            ``ans += (``1` `<< i);`   `    ``return` `ans;` `}`   `// Function to find maximum value of` `// AND * OR over all K sized subarrays` `static` `int` `maximizeAndOr(``int``[] arr, ``int` `N, ``int` `K)` `{` `    `  `    ``// Maintain an integer array bit[]` `    ``// of size 32 all initialized to 0` `    ``int``[] bit = ``new` `int``[``32``];`   `    ``// Create a sliding window of size k` `    ``for``(``int` `i = ``0``; i < K; i++)` `    ``{` `        ``for``(``int` `j = ``0``; j < ``32``; j++)` `        ``{` `            ``if` `((arr[i] & (``1` `<< j)) > ``0``)` `                ``bit[j]++;` `        ``}` `    ``}`   `    ``int` `ans = build_and(bit, K) * build_or(bit);`   `    ``for``(``int` `i = K; i < N; i++) ` `    ``{` `        `  `        ``// Perform operation for` `        ``// removed element` `        ``for``(``int` `j = ``0``; j < ``32``; j++)` `        ``{` `            ``if` `((arr[i - K] & (``1` `<< j)) > ``0``)` `                ``bit[j]--;` `        ``}`   `        ``// Perform operation for` `        ``// added_element` `        ``for``(``int` `j = ``0``; j < ``32``; j++) ` `        ``{` `            ``if` `((arr[i] & (``1` `<< j)) > ``0``)` `                ``bit[j]++;` `        ``}`   `        ``// Taking maximum value` `        ``ans = Math.max(ans, build_and(bit, K) * ` `                            ``build_or(bit));` `    ``}` `    ``return` `ans;` `}`   `// Driver Code` `public` `static` `void` `main(String args[])` `{` `    ``int``[] arr = { ``6``, ``7``, ``7``, ``10``, ``8``, ``2` `};` `    ``int` `N = arr.length;` `    ``int` `K = ``3``;`   `    ``System.out.println(maximizeAndOr(arr, N, K));` `}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Python3

 `# Python3 program for the above approach`   `# Function to convert bit array to` `# decimal number representing OR` `def` `build_or(bit):` `    `  `    ``ans ``=` `0`   `    ``for` `i ``in` `range``(``0``, ``32``):` `        ``if` `(bit[i]):` `            ``ans ``+``=` `(``1` `<< i)`   `    ``return` `ans`   `# Function to convert bit array to` `# decimal number representing AND` `def` `build_and(bit, k):`   `    ``ans ``=` `0`   `    ``for` `i ``in` `range``(``0``, ``32``):` `        ``if` `(bit[i] ``=``=` `k):` `            ``ans ``+``=` `(``1` `<< i)`   `    ``return` `ans`   `# Function to find maximum value of` `# AND * OR over all K sized subarrays` `def` `maximizeAndOr(arr, N, K):`   `    ``# Maintain an integer array bit[]` `    ``# of size 32 all initialized to 0` `    ``bit ``=` `[``0` `for` `_ ``in` `range``(``32``)]`   `    ``# Create a sliding window of size k` `    ``for` `i ``in` `range``(``0``, K):` `        ``for` `j ``in` `range``(``0``, ``32``):` `            ``if` `(arr[i] & (``1` `<< j)):` `                ``bit[j] ``+``=` `1`   `    ``ans ``=` `build_and(bit, K) ``*` `build_or(bit)`   `    ``for` `i ``in` `range``(K, N):`   `        ``# Perform operation for` `        ``# removed element` `        ``for` `j ``in` `range``(``0``, ``32``):` `            ``if` `(arr[i ``-` `K] & (``1` `<< j)):` `                ``bit[j] ``-``=` `1`   `        ``# Perform operation for` `        ``# added_element` `        ``for` `j ``in` `range``(``0``, ``32``):` `            ``if` `(arr[i] & (``1` `<< j)):` `                ``bit[j] ``+``=` `1`   `        ``# Taking maximum value` `        ``ans ``=` `max``(ans, build_and(bit, K) ``*` `                       ``build_or(bit))`   `    ``return` `ans`   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:`   `    ``arr ``=` `[ ``6``, ``7``, ``7``, ``10``, ``8``, ``2` `]` `    ``N ``=` `len``(arr)` `    ``K ``=` `3`   `    ``print``(maximizeAndOr(arr, N, K))`   `# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach` `using` `System;` `class` `GFG ` `{` `  `  `    ``// Function to convert bit array to` `    ``// decimal number representing OR` `    ``static` `int` `build_or(``int``[] bit)` `    ``{` `        ``int` `ans = 0;`   `        ``for` `(``int` `i = 0; i < 32; i++)` `            ``if` `(bit[i] > 0)` `                ``ans += (1 << i);`   `        ``return` `ans;` `    ``}`   `    ``// Function to convert bit array to` `    ``// decimal number representing AND` `    ``static` `int` `build_and(``int``[] bit, ``int` `k)` `    ``{` `        ``int` `ans = 0;`   `        ``for` `(``int` `i = 0; i < 32; i++)` `            ``if` `(bit[i] == k)` `                ``ans += (1 << i);`   `        ``return` `ans;` `    ``}`   `    ``// Function to find maximum value of` `    ``// AND * OR over all K sized subarrays` `    ``static` `int` `maximizeAndOr(``int``[] arr, ``int` `N, ``int` `K)` `    ``{` `        ``// Maintain an integer array bit[]` `        ``// of size 32 all initialized to 0` `        ``int``[] bit = ``new` `int``[32];`   `        ``// Create a sliding window of size k` `        ``for` `(``int` `i = 0; i < K; i++) {` `            ``for` `(``int` `j = 0; j < 32; j++) {` `                ``if` `((arr[i] & (1 << j)) > 0)` `                    ``bit[j]++;` `            ``}` `        ``}`   `        ``int` `ans = build_and(bit, K) * build_or(bit);`   `        ``for` `(``int` `i = K; i < N; i++) {`   `            ``// Perform operation for` `            ``// removed element` `            ``for` `(``int` `j = 0; j < 32; j++) {` `                ``if` `((arr[i - K] & (1 << j)) > 0)` `                    ``bit[j]--;` `            ``}`   `            ``// Perform operation for` `            ``// added_element` `            ``for` `(``int` `j = 0; j < 32; j++) {` `                ``if` `((arr[i] & (1 << j)) > 0)` `                    ``bit[j]++;` `            ``}`   `            ``// Taking maximum value` `            ``ans = Math.Max(ans, build_and(bit, K)` `                                    ``* build_or(bit));` `        ``}`   `        ``return` `ans;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr = { 6, 7, 7, 10, 8, 2 };` `        ``int` `N = arr.Length;` `        ``int` `K = 3;`   `        ``Console.WriteLine(maximizeAndOr(arr, N, K));` `    ``}` `}`   `// This code is contributed by ukasp.`

## Javascript

 ``

Output

`42`

Time Complexity: O(N*K) where N is the size of the input array and K is the size of the subarrays being considered. This is because there are two nested loops in the maximizeAndOr function, each of which iterates through a window of size K.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Similar Reads
Related Tutorials