Bitwise operations on Subarrays of size K
Last Updated : 11 Jul, 2022
Given an array arr[] of positive integers and a number K, the task is to find the minimum and maximum values of Bitwise operation on elements of subarray of size K.
Examples:
Input: arr[]={2, 5, 3, 6, 11, 13}, k = 3
Output:
Maximum AND = 2
Minimum AND = 0
Maximum OR = 15
Minimum OR = 7
Explanation:
Maximum AND is generated by subarray 3, 6 and 11, 3 & 6 & 11 = 2
Minimum AND is generated by subarray 2, 3 and 5, 2 & 3 & 5 = 0
Maximum OR is generated by subarray 2, 6 and 13, 2 | 6 | 13 = 15
Minimum OR is generated by subarray 2, 3 and 5, 2 | 3 | 5 = 7
Input: arr[]={5, 9, 7, 19}, k = 2
Output:
Maximum AND = 3
Minimum AND = 1
Maximum OR = 23
Minimum OR = 13
Naive Approach: The naive approach is to generate all possible subarrays of size K and check which of the above-formed subarray will give the minimum and maximum Bitwise OR and AND.
Time Complexity: O(N2)
Auxiliary Space: O(K)
Efficient Approach: The idea is to use the Sliding Window Technique to solve this problem. Below are the steps:
- Traverse the prefix array of size K and for each array, element goes through it’s each bit and increases bit array (by maintaining an integer array bit of size 32) by 1 if it is set.
- Convert this bit array to a decimal number lets say ans, and move the sliding window to the next index.
- For newly added element for the next subarray of size K, Iterate through each bit of the newly added element and increase bit array by 1 if it is set.
- For removing the first element from the previous window, decrease bit array by 1 if it is set.
- Update ans with a minimum or maximum of the new decimal number generated by bit array.
- Below is the program to find the Maximum Bitwise OR subarray:
C++
#include <iostream> using namespace std; int build_num( int bit[]) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i]) ans += (1 << i); return ans; } int maximumOR( int arr[], int n, int k) { int bit[32] = { 0 }; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } } int max_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if (arr[i - k] & (1 << j)) bit[j]--; } for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } max_or = max(build_num(bit), max_or); } return max_or; } int main() { int arr[] = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = sizeof arr / sizeof arr[0]; cout << maximumOR(arr, n, k); return 0; } |
Java
import java.util.*; class GFG{ static int build_num( int bit[]) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i++) if (bit[i] > 0 ) ans += ( 1 << i); return ans; } static int maximumOR( int arr[], int n, int k) { int bit[] = new int [ 32 ]; for ( int i = 0 ; i < k; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } } int max_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i - k] & ( 1 << j)) > 0 ) bit[j]--; } for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } max_or = Math.max(build_num(bit), max_or); } return max_or; } public static void main(String[] args) { int arr[] = { 2 , 5 , 3 , 6 , 11 , 13 }; int k = 3 ; int n = arr.length; System.out.print(maximumOR(arr, n, k)); } } |
Python3
def build_num(bit): ans = 0 ; for i in range ( 32 ): if (bit[i] > 0 ): ans + = ( 1 << i); return ans; def maximumOR(arr, n, k): bit = [ 0 ] * 32 ; for i in range (k): for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; max_or = build_num(bit); for i in range (k, n): for j in range ( 32 ): if ((arr[i - k] & ( 1 << j)) > 0 ): bit[j] - = 1 ; for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; max_or = max (build_num(bit), max_or); return max_or; if __name__ = = '__main__' : arr = [ 2 , 5 , 3 , 6 , 11 , 13 ]; k = 3 ; n = len (arr); print (maximumOR(arr, n, k)); |
C#
using System; class GFG{ static int build_num( int [] bit) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] > 0) ans += (1 << i); return ans; } static int maximumOR( int [] arr, int n, int k) { int [] bit = new int [32]; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } int max_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } max_or = Math.Max(build_num(bit), max_or); } return max_or; } public static void Main(String[] args) { int [] arr = {2, 5, 3, 6, 11, 13}; int k = 3; int n = arr.Length; Console.Write(maximumOR(arr, n, k)); } } |
Javascript
<script> function build_num(bit) { let ans = 0; for (let i = 0; i < 32; i++) if (bit[i] > 0) ans += (1 << i); return ans; } function maximumOR(arr, n, k) { let bit = new Array(32); bit.fill(0); for (let i = 0; i < k; i++) { for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } let max_or = build_num(bit); for (let i = k; i < n; i++) { for (let j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } max_or = Math.max(build_num(bit), max_or); } return max_or; } let arr = [2, 5, 3, 6, 11, 13]; let k = 3; let n = arr.length; document.write(maximumOR(arr, n, k)); </script> |
Time Complexity: O(n * B) where n is the size of the array and B is the integer array bit of size 32.
Auxiliary Space: O(n)
- Below is the program to find the Minimum Bitwise OR subarray:
C++
#include <iostream> using namespace std; int build_num( int bit[]) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i]) ans += (1 << i); return ans; } int minimumOR( int arr[], int n, int k) { int bit[32] = { 0 }; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } } int min_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if (arr[i - k] & (1 << j)) bit[j]--; } for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } min_or = min(build_num(bit), min_or); } return min_or; } int main() { int arr[] = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = sizeof arr / sizeof arr[0]; cout << minimumOR(arr, n, k); return 0; } |
Java
import java.util.*; class GFG{ static int build_num( int bit[]) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i++) if (bit[i] > 0 ) ans += ( 1 << i); return ans; } static int minimumOR( int arr[], int n, int k) { int bit[] = new int [ 32 ]; for ( int i = 0 ; i < k; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } } int min_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i - k] & ( 1 << j)) > 0 ) bit[j]--; } for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } min_or = Math.min(build_num(bit), min_or); } return min_or; } public static void main(String[] args) { int arr[] = { 2 , 5 , 3 , 6 , 11 , 13 }; int k = 3 ; int n = arr.length; System.out.print(minimumOR(arr, n, k)); } } |
Python3
def build_num(bit): ans = 0 ; for i in range ( 32 ): if (bit[i] > 0 ): ans + = ( 1 << i); return ans; def minimumOR(arr, n, k): bit = [ 0 ] * 32 ; for i in range (k): for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; min_or = build_num(bit); for i in range (k, n): for j in range ( 32 ): if ((arr[i - k] & ( 1 << j)) > 0 ): bit[j] - = 1 ; for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; min_or = min (build_num(bit), min_or); return min_or; if __name__ = = '__main__' : arr = [ 2 , 5 , 3 , 6 , 11 , 13 ]; k = 3 ; n = len (arr); print (minimumOR(arr, n, k)); |
C#
using System; class GFG{ static int build_num( int []bit) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] > 0) ans += (1 << i); return ans; } static int minimumOR( int []arr, int n, int k) { int []bit = new int [32]; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } int min_or = build_num(bit); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } min_or = Math.Min(build_num(bit), min_or); } return min_or; } public static void Main(String[] args) { int []arr = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = arr.Length; Console.Write(minimumOR(arr, n, k)); } } |
Javascript
<script> function build_num(bit) { let ans = 0; for (let i = 0; i < 32; i++) if (bit[i] > 0) ans += (1 << i); return ans; } function minimumOR(arr, n, k) { let bit = new Array(32); bit.fill(0); for (let i = 0; i < k; i++) { for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } let min_or = build_num(bit); for (let i = k; i < n; i++) { for (let j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } min_or = Math.min(build_num(bit), min_or); } return min_or; } let arr = [ 2, 5, 3, 6, 11, 13 ]; let k = 3; let n = arr.length; document.write(minimumOR(arr, n, k)); </script> |
Time Complexity: O(n * B) where n is the size of the array and B is the integer array bit of size 32.
Auxiliary Space: O(n)
- Below is the program to find the Maximum Bitwise AND subarray:
C++
#include <iostream> using namespace std; int build_num( int bit[], int k) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } int maximumAND( int arr[], int n, int k) { int bit[32] = { 0 }; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } } int max_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if (arr[i - k] & (1 << j)) bit[j]--; } for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } max_and = max(build_num(bit, k), max_and); } return max_and; } int main() { int arr[] = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = sizeof arr / sizeof arr[0]; cout << maximumAND(arr, n, k); return 0; } |
Java
class GFG{ static int build_num( int bit[], int k) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i++) if (bit[i] == k) ans += ( 1 << i); return ans; } static int maximumAND( int arr[], int n, int k) { int bit[] = new int [ 32 ]; for ( int i = 0 ; i < k; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } } int max_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i - k] & ( 1 << j)) > 0 ) bit[j]--; } for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } max_and = Math.max(build_num(bit, k), max_and); } return max_and; } public static void main(String[] args) { int arr[] = { 2 , 5 , 3 , 6 , 11 , 13 }; int k = 3 ; int n = arr.length; System.out.print(maximumAND(arr, n, k)); } } |
Python3
def build_num(bit, k): ans = 0 ; for i in range ( 32 ): if (bit[i] = = k): ans + = ( 1 << i); return ans; def maximumAND(arr, n, k): bit = [ 0 ] * 32 ; for i in range (k): for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; max_and = build_num(bit, k); for i in range (k, n): for j in range ( 32 ): if ((arr[i - k] & ( 1 << j)) > 0 ): bit[j] - = 1 ; for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; max_and = max (build_num(bit, k), max_and); return max_and; if __name__ = = '__main__' : arr = [ 2 , 5 , 3 , 6 , 11 , 13 ]; k = 3 ; n = len (arr); print (maximumAND(arr, n, k)); |
C#
using System; class GFG{ static int build_num( int [] bit, int k) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } static int maximumAND( int [] arr, int n, int k) { int [] bit = new int [32]; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } int max_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } max_and = Math.Max(build_num(bit, k), max_and); } return max_and; } public static void Main(String[] args) { int [] arr = {2, 5, 3, 6, 11, 13}; int k = 3; int n = arr.Length; Console.Write(maximumAND(arr, n, k)); } } |
Javascript
<script> function build_num(bit, k) { let ans = 0; for (let i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } function maximumAND(arr, n, k) { let bit = new Array(32); bit.fill(0); for (let i = 0; i < k; i++) { for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } let max_and = build_num(bit, k); for (let i = k; i < n; i++) { for (let j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } max_and = Math.max(build_num(bit, k), max_and); } return max_and; } let arr = [2, 5, 3, 6, 11, 13]; let k = 3; let n = arr.length; document.write(maximumAND(arr, n, k)); </script> |
Time Complexity: O(n * B) where n is the size of the array and B is the integer array bit of size 32.
Auxiliary Space: O(n)
C++
#include <iostream> using namespace std; int build_num( int bit[], int k) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } int minimumAND( int arr[], int n, int k) { int bit[32] = { 0 }; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } } int min_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if (arr[i - k] & (1 << j)) bit[j]--; } for ( int j = 0; j < 32; j++) { if (arr[i] & (1 << j)) bit[j]++; } min_and = min(build_num(bit, k), min_and); } return min_and; } int main() { int arr[] = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = sizeof arr / sizeof arr[0]; cout << minimumAND(arr, n, k); return 0; } |
Java
class GFG{ static int build_num( int bit[], int k) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i++) if (bit[i] == k) ans += ( 1 << i); return ans; } static int minimumAND( int arr[], int n, int k) { int bit[] = new int [ 32 ]; for ( int i = 0 ; i < k; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } } int min_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0 ; j < 32 ; j++) { if ((arr[i - k] & ( 1 << j)) > 0 ) bit[j]--; } for ( int j = 0 ; j < 32 ; j++) { if ((arr[i] & ( 1 << j)) > 0 ) bit[j]++; } min_and = Math.min(build_num(bit, k), min_and); } return min_and; } public static void main(String[] args) { int arr[] = { 2 , 5 , 3 , 6 , 11 , 13 }; int k = 3 ; int n = arr.length; System.out.print(minimumAND(arr, n, k)); } } |
Python3
def build_num(bit, k): ans = 0 ; for i in range ( 32 ): if (bit[i] = = k): ans + = ( 1 << i); return ans; def minimumAND(arr, n, k): bit = [ 0 ] * 32 ; for i in range (k): for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; min_and = build_num(bit, k); for i in range (k, n): for j in range ( 32 ): if ((arr[i - k] & ( 1 << j)) > 0 ): bit[j] - = 1 ; for j in range ( 32 ): if ((arr[i] & ( 1 << j)) > 0 ): bit[j] + = 1 ; min_and = min (build_num(bit, k), min_and); return min_and; if __name__ = = '__main__' : arr = [ 2 , 5 , 3 , 6 , 11 , 13 ]; k = 3 ; n = len (arr); print (minimumAND(arr, n, k)); |
C#
using System; class GFG{ static int build_num( int []bit, int k) { int ans = 0; for ( int i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } static int minimumAND( int []arr, int n, int k) { int []bit = new int [32]; for ( int i = 0; i < k; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } int min_and = build_num(bit, k); for ( int i = k; i < n; i++) { for ( int j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for ( int j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } min_and = Math.Min(build_num(bit, k), min_and); } return min_and; } public static void Main(String[] args) { int []arr = { 2, 5, 3, 6, 11, 13 }; int k = 3; int n = arr.Length; Console.Write(minimumAND(arr, n, k)); } } |
Javascript
<script> function build_num(bit, k) { let ans = 0; for (let i = 0; i < 32; i++) if (bit[i] == k) ans += (1 << i); return ans; } function minimumAND(arr, n, k) { let bit = new Array(32); bit.fill(0); for (let i = 0; i < k; i++) { for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } } let min_and = build_num(bit, k); for (let i = k; i < n; i++) { for (let j = 0; j < 32; j++) { if ((arr[i - k] & (1 << j)) > 0) bit[j]--; } for (let j = 0; j < 32; j++) { if ((arr[i] & (1 << j)) > 0) bit[j]++; } min_and = Math.min(build_num(bit, k), min_and); } return min_and; } let arr = [ 2, 5, 3, 6, 11, 13 ]; let k = 3; let n = arr.length; document.write(minimumAND(arr, n, k)); </script> |
Time Complexity: O(n * B) where n is the size of the array and B is the integer array bit of size 32.
Auxiliary Space: O(n)
C++
#include <bits/stdc++.h> using namespace std; void findMinXORSubarray( int arr[], int n, int k) { if (n < k) return ; int res_index = 0; int curr_xor = 0; for ( int i = 0; i < k; i++) curr_xor ^= arr[i]; int min_xor = curr_xor; for ( int i = k; i < n; i++) { curr_xor ^= (arr[i] ^ arr[i - k]); if (curr_xor < min_xor) { min_xor = curr_xor; res_index = (i - k + 1); } } cout << min_xor << "\n" ; } int main() { int arr[] = { 3, 7, 90, 20, 10, 50, 40 }; int k = 3; int n = sizeof (arr) / sizeof (arr[0]); findMinXORSubarray(arr, n, k); return 0; } |
Java
class GFG{ static void findMinXORSubarray( int arr[], int n, int k) { if (n < k) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k; i++) curr_xor ^= arr[i]; int min_xor = curr_xor; for ( int i = k; i < n; i++) { curr_xor ^= (arr[i] ^ arr[i - k]); if (curr_xor < min_xor) { min_xor = curr_xor; res_index = (i - k + 1 ); } } System.out.println(min_xor); } public static void main(String[] args) { int arr[] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 }; int k = 3 ; int n = arr.length; findMinXORSubarray(arr, n, k); } } |
Python3
def findMinXORSubarray(arr, n, k): if (n < k): return ; res_index = 0 ; curr_xor = 0 ; for i in range (k): curr_xor ^ = arr[i]; min_xor = curr_xor; for i in range (k, n): curr_xor ^ = (arr[i] ^ arr[i - k]); if (curr_xor < min_xor): min_xor = curr_xor; res_index = (i - k + 1 ); print (min_xor); if __name__ = = '__main__' : arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ]; k = 3 ; n = len (arr); findMinXORSubarray(arr, n, k); |
C#
using System; class GFG{ static void findMinXORSubarray( int []arr, int n, int k) { if (n < k) return ; int res_index = 0; int curr_xor = 0; for ( int i = 0; i < k; i++) curr_xor ^= arr[i]; int min_xor = curr_xor; for ( int i = k; i < n; i++) { curr_xor ^= (arr[i] ^ arr[i - k]); if (curr_xor < min_xor) { min_xor = curr_xor; res_index = (i - k + 1); } } Console.WriteLine(min_xor); } public static void Main(String[] args) { int []arr = { 3, 7, 90, 20, 10, 50, 40 }; int k = 3; int n = arr.Length; findMinXORSubarray(arr, n, k); } } |
Javascript
<script> function findMinXORSubarray(arr, n, k) { if (n < k) return ; let res_index = 0; let curr_xor = 0; for (let i = 0; i < k; i++) curr_xor ^= arr[i]; let min_xor = curr_xor; for (let i = k; i < n; i++) { curr_xor ^= (arr[i] ^ arr[i - k]); if (curr_xor < min_xor) { min_xor = curr_xor; res_index = (i - k + 1); } } document.write(min_xor); } let arr = [ 3, 7, 90, 20, 10, 50, 40 ]; let k = 3; let n = arr.length; findMinXORSubarray(arr, n, k); </script> |
Time Complexity: O(n * B) where n is the size of the array and B is the integer array bit of size 32.
Auxiliary Space: O(n)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Similar Reads
Bitwise Operations in Subarrays: Query and Result
Given a positive integer array A[] of size N and Q queries. Given a 2D integer array Queries[][] of length Q where each query has four integers l1, r1, l2, r2, the task is to calculate (Al1 & Al1+1 & Al1+2 ...& Ar1) | (Al2 & Al2+1 & Al2+2....& Ar2) and return an integer array
9 min read
Number of subarray's of K size with Even Bitwise-OR
You are given an array arr[] containing n integers and an integer k. Find the number of subarrays of arr with length k whose bitwise OR is even. Examples: Input: arr[] = {4, 2, 6, 7, 8} , k = 3Output: 2Explanation: There are 3 subarrays of length =3[4,2,6] with bitwise OR 6[2,6,7] with bitwise OR 7[
5 min read
Sum of bitwise OR of all subarrays
Given an array of positive integers, find the total sum after performing the bit wise OR operation on all the sub arrays of a given array. Examples: Input : 1 2 3 4 5 Output : 71 Input : 6 5 4 3 2 Output : 84 First initialize the two variable sum=0, sum1=0, variable sum will store the total sum and,
5 min read
Count subarrays having odd Bitwise XOR
Given an array arr[] of size N, the task is to count the number of subarrays from the given array having odd Bitwise XOR value. Examples: Input: arr[] = {1, 4, 7, 9, 10}Output: 8Explanation: The subarrays having odd Bitwise XOR are {1}, {1, 4}, {1, 4, 7, 9}, {1, 4, 7, 9, 10}, {7}, {9}, {4, 7}, {9, 1
11 min read
Number of subarrays have bitwise OR >= K
Given an array arr[] and an integer K, the task is to count the number of sub-arrays having bitwise OR ? K. Examples: Input: arr[] = { 1, 2, 3 } K = 3 Output: 4Bitwise OR of sub-arrays: { 1 } = 1 { 1, 2 } = 3 { 1, 2, 3 } = 3 { 2 } = 2 { 2, 3 } = 3 { 3 } = 3 4 sub-arrays have bitwise OR ? K Input: ar
15+ min read
Count of all possible bitonic subarrays
Given an array arr[] consisting of N integers, the task is to count all the subarrays which are Bitonic in nature. A bitonic subarray is a subarray in which elements are either strictly increasing or strictly decreasing, or are first increasing and then decreasing. Examples: Input: arr[] = {2, 1, 4,
6 min read
Sum of all subarrays of size K
Given an array arr[] and an integer K, the task is to calculate the sum of all subarrays of size K. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 3 Output: 6 9 12 15 Explanation: All subarrays of size k and their sum: Subarray 1: {1, 2, 3} = 1 + 2 + 3 = 6 Subarray 2: {2, 3, 4} = 2 + 3 + 4 = 9 Sub
11 min read
GCD of all subarrays of size K
Given an array, arr[] of size N, the task is to print the GCD of all subarrays of size K. Examples: Input: arr[] = {2, 4, 3, 9, 14, 20, 25, 17}, K = 2Output: 2 1 3 1 2 5 1Explanation:gcd(2, 4}) = 2gcd(4, 3) = 1gcd(3, 9) = 3gcd(9, 14) = 1gcd(14, 20) = 2gcd(20, 25) = 5gcd(25, 17) = 1Therefore, the req
5 min read
Count subarrays having even Bitwise XOR
Given an array arr[] of size N, the task is to count the number of subarrays from the given array whose Bitwise XOR is even. Examples: Input: arr[] = {1, 2, 3, 4}Output: 4Explanation: The subarrays having even Bitwise XOR are {{2}, {4}, {1, 2, 3}, {1, 2, 3, 4}}. Input: arr[] = {2, 4, 6}Output: 6Expl
11 min read
Queries for Sum of Bitwise AND of all Subarrays in a Range
Given an array arr[] of size N, the task is to answer a set of Q queries, each in the format of queries[i][0] and queries[i][1]. For each queries[i], find the sum of Bitwise AND of all subarrays whose elements lie in the range [queries[i][0], queries[i][1]]. Examples: Input: N = 3, arr[] = {1, 0, 2}
12 min read