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++
// C++ program for maximum values of
// each bitwise OR operation on
// element of subarray of size K
#include <iostream>
using namespace std;
// Function to convert bit array to
// decimal number
int build_num(int bit[])
{
int ans = 0;
for (int i = 0; i < 32; i++)
if (bit[i])
ans += (1 << i);
return ans;
}
// Function to find maximum values of
// each bitwise OR operation on
// element of subarray of size K
int maximumOR(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]++;
}
}
// Function call
int max_or = build_num(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
max_or = max(build_num(bit), max_or);
}
// Return the result
return max_or;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = sizeof arr / sizeof arr[0];
// Function Call
cout << maximumOR(arr, n, k);
return 0;
}
Java
// Java program for maximum values of
// each bitwise OR operation on
// element of subarray of size K
import java.util.*;
class GFG{
// Function to convert bit array to
// decimal number
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;
}
// Function to find maximum values of
// each bitwise OR operation on
// element of subarray of size K
static int maximumOR(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]++;
}
}
// Function call
int max_or = build_num(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
max_or = Math.max(build_num(bit), max_or);
}
// Return the result
return max_or;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.length;
// Function Call
System.out.print(maximumOR(arr, n, k));
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program for maximum values of
# each bitwise OR operation on
# element of subarray of size K
# Function to convert bit array to
# decimal number
def build_num(bit):
ans = 0;
for i in range(32):
if (bit[i] > 0):
ans += (1 << i);
return ans;
# Function to find maximum values of
# each bitwise OR operation on
# element of subarray of size K
def maximumOR(arr, n, k):
# Maintain an integer array bit
# of size 32 all initialized to 0
bit = [0] * 32;
# Create a sliding window of size k
for i in range(k):
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Function call
max_or = build_num(bit);
for i in range(k, n):
# Perform operation for
# removed element
for j in range(32):
if ((arr[i - k] & (1 << j)) > 0):
bit[j] -= 1;
# Perform operation for
# added_element
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Taking maximum value
max_or = max(build_num(bit), max_or);
# Return the result
return max_or;
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 2, 5, 3, 6, 11, 13 ];
# Given subarray size K
k = 3;
n = len(arr);
# Function call
print(maximumOR(arr, n, k));
# This code is contributed by Amit Katiyar
C#
// C# program for maximum values of
// each bitwise OR operation on
// element of subarray of size K
using System;
class GFG{
// Function to convert bit
// array to decimal number
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;
}
// Function to find maximum values of
// each bitwise OR operation on
// element of subarray of size K
static int maximumOR(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]++;
}
}
// Function call
int max_or = build_num(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
max_or = Math.Max(build_num(bit), max_or);
}
// Return the result
return max_or;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int[] arr = {2, 5, 3, 6, 11, 13};
// Given subarray size K
int k = 3;
int n = arr.Length;
// Function Call
Console.Write(maximumOR(arr, n, k));
}
}
// This code is contributed by Rohit_ranjan
JavaScript
<script>
// Javascript program for maximum values of
// each bitwise OR operation on
// element of subarray of size K
// Function to convert bit array to
// decimal number
function build_num(bit)
{
let ans = 0;
for (let i = 0; i < 32; i++)
if (bit[i] > 0)
ans += (1 << i);
return ans;
}
// Function to find maximum values of
// each bitwise OR operation on
// element of subarray of size K
function maximumOR(arr, n, k)
{
// Maintain an integer array bit[]
// of size 32 all initialized to 0
let bit = new Array(32);
bit.fill(0);
// Create a sliding window of size k
for (let i = 0; i < k; i++)
{
for (let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
}
// Function call
let max_or = build_num(bit);
for (let i = k; i < n; i++)
{
// Perform operation for
// removed element
for (let j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation for
// added_element
for (let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking maximum value
max_or = Math.max(build_num(bit), max_or);
}
// Return the result
return max_or;
}
// Given array []arr
let arr = [2, 5, 3, 6, 11, 13];
// Given subarray size K
let k = 3;
let n = arr.length;
// Function Call
document.write(maximumOR(arr, n, k));
// This code is contributed by divyesh072019.
</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++
// C++ program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
#include <iostream>
using namespace std;
// Function to convert bit array
// to decimal number
int build_num(int bit[])
{
int ans = 0;
for (int i = 0; i < 32; i++)
if (bit[i])
ans += (1 << i);
return ans;
}
// Function to find minimum values of
// each bitwise OR operation on
// element of subarray of size K
int minimumOR(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]++;
}
}
// Function call
int min_or = build_num(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 minimum value
min_or = min(build_num(bit),
min_or);
}
// Return the result
return min_or;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = sizeof arr / sizeof arr[0];
// Function Call
cout << minimumOR(arr, n, k);
return 0;
}
Java
// Java program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
import java.util.*;
class GFG{
// Function to convert bit array
// to decimal number
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;
}
// Function to find minimum values of
// each bitwise OR operation on
// element of subarray of size K
static int minimumOR(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]++;
}
}
// Function call
int min_or = build_num(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 minimum value
min_or = Math.min(build_num(bit),
min_or);
}
// Return the result
return min_or;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.length;
// Function call
System.out.print(minimumOR(arr, n, k));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for minimum values
# of each bitwise OR operation on
# element of subarray of size K
# Function to convert bit array
# to decimal number
def build_num(bit):
ans = 0;
for i in range(32):
if (bit[i] > 0):
ans += (1 << i);
return ans;
# Function to find minimum values of
# each bitwise OR operation on
# element of subarray of size K
def minimumOR(arr, n, k):
# Maintain an integer array bit
# of size 32 all initialized to 0
bit = [0] * 32;
# Create a sliding window of size k
for i in range(k):
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Function call
min_or = build_num(bit);
for i in range(k, n):
# Perform operation for
# removed element
for j in range(32):
if ((arr[i - k] & (1 << j)) > 0):
bit[j] -= 1;
# Perform operation for
# added_element
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Taking minimum value
min_or = min(build_num(bit), min_or);
# Return the result
return min_or;
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 2, 5, 3, 6, 11, 13 ];
# Given subarray size K
k = 3;
n = len(arr);
# Function call
print(minimumOR(arr, n, k));
# This code is contributed by Amit Katiyar
C#
// C# program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
using System;
class GFG{
// Function to convert bit array
// to decimal number
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;
}
// Function to find minimum values of
// each bitwise OR operation on
// element of subarray of size K
static int minimumOR(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]++;
}
}
// Function call
int min_or = build_num(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 minimum value
min_or = Math.Min(build_num(bit),
min_or);
}
// Return the result
return min_or;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.Length;
// Function call
Console.Write(minimumOR(arr, n, k));
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
// Function to convert bit array
// to decimal number
function build_num(bit)
{
let ans = 0;
for(let i = 0; i < 32; i++)
if (bit[i] > 0)
ans += (1 << i);
return ans;
}
// Function to find minimum values of
// each bitwise OR operation on
// element of subarray of size K
function minimumOR(arr, n, k)
{
// Maintain an integer array bit[]
// of size 32 all initialized to 0
let bit = new Array(32);
bit.fill(0);
// Create a sliding window of size k
for(let i = 0; i < k; i++)
{
for(let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
}
// Function call
let min_or = build_num(bit);
for(let i = k; i < n; i++)
{
// Perform operation for
// removed element
for(let j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation for
// added_element
for(let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking minimum value
min_or = Math.min(build_num(bit), min_or);
}
// Return the result
return min_or;
}
// Driver code
// Given array arr[]
let arr = [ 2, 5, 3, 6, 11, 13 ];
// Given subarray size K
let k = 3;
let n = arr.length;
// Function Call
document.write(minimumOR(arr, n, k));
// This code is contributed by rameshtravel07
</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++
// C++ program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
#include <iostream>
using namespace std;
// Function to convert bit array
// to decimal number
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;
}
// Function to find maximum values of
// each bitwise AND operation on
// element of subarray of size K
int maximumAND(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]++;
}
}
// Function call
int max_and = build_num(bit, k);
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
max_and = max(build_num(bit, k),
max_and);
}
// Return the result
return max_and;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = sizeof arr / sizeof arr[0];
// Function Call
cout << maximumAND(arr, n, k);
return 0;
}
Java
// Java program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
class GFG{
// Function to convert bit array
// to decimal number
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;
}
// Function to find maximum values of
// each bitwise AND operation on
// element of subarray of size K
static int maximumAND(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]++;
}
}
// Function call
int max_and = build_num(bit, k);
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
max_and = Math.max(build_num(bit, k),
max_and);
}
// Return the result
return max_and;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.length;
// Function call
System.out.print(maximumAND(arr, n, k));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for maximum values of
# each bitwise AND operation on
# element of subarray of size K
# Function to convert bit array
# to decimal number
def build_num(bit, k):
ans = 0;
for i in range(32):
if (bit[i] == k):
ans += (1 << i);
return ans;
# Function to find maximum values of
# each bitwise AND operation on
# element of subarray of size K
def maximumAND(arr, n, k):
# Maintain an integer array bit
# of size 32 all initialized to 0
bit = [0] * 32;
# Create a sliding window of size k
for i in range(k):
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Function call
max_and = build_num(bit, k);
for i in range(k, n):
# Perform operation for
# removed element
for j in range(32):
if ((arr[i - k] & (1 << j)) > 0):
bit[j] -= 1;
# Perform operation for
# added element
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Taking maximum value
max_and = max(build_num(bit, k),
max_and);
# Return the result
return max_and;
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 2, 5, 3, 6, 11, 13 ];
# Given subarray size K
k = 3;
n = len(arr);
# Function call
print(maximumAND(arr, n, k));
# This code is contributed by Amit Katiyar
C#
// C# program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
using System;
class GFG{
// Function to convert bit
// array to decimal number
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;
}
// Function to find maximum values of
// each bitwise AND operation on
// element of subarray of size K
static int maximumAND(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]++;
}
}
// Function call
int max_and = build_num(bit, k);
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
max_and = Math.Max(build_num(bit, k),
max_and);
}
// Return the result
return max_and;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int[] arr = {2, 5, 3, 6, 11, 13};
// Given subarray size K
int k = 3;
int n = arr.Length;
// Function call
Console.Write(maximumAND(arr, n, k));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
// Function to convert bit
// array to decimal number
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 to find maximum values of
// each bitwise AND operation on
// element of subarray of size K
function maximumAND(arr, n, k)
{
// Maintain an integer array bit[]
// of size 32 all initialized to 0
let bit = new Array(32);
bit.fill(0);
// Create a sliding window of size k
for (let i = 0; i < k; i++)
{
for (let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
}
// Function call
let max_and = build_num(bit, k);
for (let i = k; i < n; i++)
{
// Perform operation for
// removed element
for (let j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation for
// added element
for (let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking maximum value
max_and = Math.max(build_num(bit, k),
max_and);
}
// Return the result
return max_and;
}
// Given array []arr
let arr = [2, 5, 3, 6, 11, 13];
// Given subarray size K
let k = 3;
let n = arr.length;
// Function call
document.write(maximumAND(arr, n, k));
// This code is contributed by mukesh07.
</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++
// C++ program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
#include <iostream>
using namespace std;
// Function to convert bit array
// to decimal number
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;
}
// Function to find minimum values of
// each bitwise AND operation on
// element of subarray of size K
int minimumAND(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]++;
}
}
// Function call
int min_and = build_num(bit, k);
for (int i = k; i < n; i++) {
// Perform operation to removed
// element
for (int j = 0; j < 32; j++) {
if (arr[i - k] & (1 << j))
bit[j]--;
}
// Perform operation to add
// element
for (int j = 0; j < 32; j++) {
if (arr[i] & (1 << j))
bit[j]++;
}
// Taking minimum value
min_and = min(build_num(bit, k),
min_and);
}
// Return the result
return min_and;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = sizeof arr / sizeof arr[0];
// Function Call
cout << minimumAND(arr, n, k);
return 0;
}
Java
// Java program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
class GFG{
// Function to convert bit array
// to decimal number
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;
}
// Function to find minimum values of
// each bitwise AND operation on
// element of subarray of size K
static int minimumAND(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]++;
}
}
// Function call
int min_and = build_num(bit, k);
for(int i = k; i < n; i++)
{
// Perform operation to removed
// element
for(int j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation to add
// element
for(int j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking minimum value
min_and = Math.min(build_num(bit, k),
min_and);
}
// Return the result
return min_and;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.length;
// Function call
System.out.print(minimumAND(arr, n, k));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for minimum values of
# each bitwise AND operation on
# elements of subarray of size K
# Function to convert bit array
# to decimal number
def build_num(bit, k):
ans = 0;
for i in range(32):
if (bit[i] == k):
ans += (1 << i);
return ans;
# Function to find minimum values of
# each bitwise AND operation on
# element of subarray of size K
def minimumAND(arr, n, k):
# Maintain an integer array bit
# of size 32 all initialized to 0
bit = [0] * 32;
# Create a sliding window of size k
for i in range(k):
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Function call
min_and = build_num(bit, k);
for i in range(k, n):
# Perform operation to removed
# element
for j in range(32):
if ((arr[i - k] & (1 << j)) > 0):
bit[j] -=1;
# Perform operation to add
# element
for j in range(32):
if ((arr[i] & (1 << j)) > 0):
bit[j] += 1;
# Taking minimum value
min_and = min(build_num(bit, k), min_and);
# Return the result
return min_and;
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [2, 5, 3, 6, 11, 13];
# Given subarray size K
k = 3;
n = len(arr);
# Function call
print(minimumAND(arr, n, k));
# This code contributed by Rajput-Ji
C#
// C# program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
using System;
class GFG{
// Function to convert bit array
// to decimal number
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;
}
// Function to find minimum values of
// each bitwise AND operation on
// element of subarray of size K
static int minimumAND(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]++;
}
}
// Function call
int min_and = build_num(bit, k);
for(int i = k; i < n; i++)
{
// Perform operation to removed
// element
for(int j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation to add
// element
for(int j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking minimum value
min_and = Math.Min(build_num(bit, k),
min_and);
}
// Return the result
return min_and;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 2, 5, 3, 6, 11, 13 };
// Given subarray size K
int k = 3;
int n = arr.Length;
// Function call
Console.Write(minimumAND(arr, n, k));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
// Function to convert bit array
// to decimal number
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 to find minimum values of
// each bitwise AND operation on
// element of subarray of size K
function minimumAND(arr, n, k)
{
// Maintain an integer array bit[]
// of size 32 all initialized to 0
let bit = new Array(32);
bit.fill(0);
// Create a sliding window of size k
for(let i = 0; i < k; i++)
{
for(let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
}
// Function call
let min_and = build_num(bit, k);
for(let i = k; i < n; i++)
{
// Perform operation to removed
// element
for(let j = 0; j < 32; j++)
{
if ((arr[i - k] & (1 << j)) > 0)
bit[j]--;
}
// Perform operation to add
// element
for(let j = 0; j < 32; j++)
{
if ((arr[i] & (1 << j)) > 0)
bit[j]++;
}
// Taking minimum value
min_and = Math.min(build_num(bit, k), min_and);
}
// Return the result
return min_and;
}
// Given array []arr
let arr = [ 2, 5, 3, 6, 11, 13 ];
// Given subarray size K
let k = 3;
let n = arr.length;
// Function call
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++
// C++ program to find the subarray
/// with minimum XOR
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum XOR
// of the subarray of size K
void findMinXORSubarray(int arr[],
int n, int k)
{
// K must be smaller than
// or equal to n
if (n < k)
return;
// Initialize the beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for (int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for (int i = k; i < n; i++) {
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor) {
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
// Print the minimum XOR
cout << min_xor << "\n";
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
// Given subarray size K
int k = 3;
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
findMinXORSubarray(arr, n, k);
return 0;
}
Java
// Java program to find the subarray
// with minimum XOR
class GFG{
// Function to find the minimum XOR
// of the subarray of size K
static void findMinXORSubarray(int arr[],
int n, int k)
{
// K must be smaller than
// or equal to n
if (n < k)
return;
// Initialize the beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for(int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for(int i = k; i < n; i++)
{
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
// Print the minimum XOR
System.out.println(min_xor);
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 3, 7, 90, 20, 10, 50, 40 };
// Given subarray size K
int k = 3;
int n = arr.length;
// Function call
findMinXORSubarray(arr, n, k);
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to find the subarray
# with minimum XOR
# Function to find the minimum XOR
# of the subarray of size K
def findMinXORSubarray(arr, n, k):
# K must be smaller than
# or equal to n
if (n < k):
return;
# Initialize the beginning
# index of result
res_index = 0;
# Compute XOR sum of first
# subarray of size K
curr_xor = 0;
for i in range(k):
curr_xor ^= arr[i];
# Initialize minimum XOR
# sum as current xor
min_xor = curr_xor;
# Traverse from (k+1)'th
# element to n'th element
for i in range(k, n):
# XOR with current item
# and first item of
# previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
# Update result if needed
if (curr_xor < min_xor):
min_xor = curr_xor;
res_index = (i - k + 1);
# Print the minimum XOR
print(min_xor);
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 3, 7, 90, 20, 10, 50, 40 ];
# Given subarray size K
k = 3;
n = len(arr);
# Function call
findMinXORSubarray(arr, n, k);
# This code is contributed by Amit Katiyar
C#
// C# program to find the subarray
// with minimum XOR
using System;
class GFG{
// Function to find the minimum XOR
// of the subarray of size K
static void findMinXORSubarray(int []arr,
int n, int k)
{
// K must be smaller than
// or equal to n
if (n < k)
return;
// Initialize the beginning
// index of result
int res_index = 0;
// Compute XOR sum of first
// subarray of size K
int curr_xor = 0;
for(int i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
int min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for(int i = k; i < n; i++)
{
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
// Print the minimum XOR
Console.WriteLine(min_xor);
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 3, 7, 90, 20, 10, 50, 40 };
// Given subarray size K
int k = 3;
int n = arr.Length;
// Function call
findMinXORSubarray(arr, n, k);
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// Javascript program to find the subarray
// with minimum XOR
// Function to find the minimum XOR
// of the subarray of size K
function findMinXORSubarray(arr, n, k)
{
// K must be smaller than
// or equal to n
if (n < k)
return;
// Initialize the beginning
// index of result
let res_index = 0;
// Compute XOR sum of first
// subarray of size K
let curr_xor = 0;
for(let i = 0; i < k; i++)
curr_xor ^= arr[i];
// Initialize minimum XOR
// sum as current xor
let min_xor = curr_xor;
// Traverse from (k+1)'th
// element to n'th element
for(let i = k; i < n; i++)
{
// XOR with current item
// and first item of
// previous subarray
curr_xor ^= (arr[i] ^ arr[i - k]);
// Update result if needed
if (curr_xor < min_xor)
{
min_xor = curr_xor;
res_index = (i - k + 1);
}
}
// Print the minimum XOR
document.write(min_xor);
}
// Driver code
// Given array arr[]
let arr = [ 3, 7, 90, 20, 10, 50, 40 ];
// Given subarray size K
let k = 3;
let n = arr.length;
// Function Call
findMinXORSubarray(arr, n, k);
// This code is contributed by divyeshrabadiya07
</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
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