Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Number of subarray's of K size with Even Bitwise-OR
Next article icon

Bitwise operations on Subarrays of size K

Last Updated : 11 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. 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.
  2. Convert this bit array to a decimal number lets say ans, and move the sliding window to the next index.
  3. 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.
  4. For removing the first element from the previous window, decrease bit array by 1 if it is set.
  5. 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>
 
 
Output: 
15

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>
 
 
Output: 
7

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>
 
 
Output: 
2

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 AND subarray:

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>
 
 
Output: 
0

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 XOR subarray:

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>
 
 
Output: 
16

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



Next Article
Number of subarray's of K size with Even Bitwise-OR

S

stream_cipher
Improve
Article Tags :
  • Arrays
  • Bit Magic
  • Competitive Programming
  • DSA
  • Bitwise-AND
  • Bitwise-OR
  • Bitwise-XOR
  • subarray
Practice Tags :
  • Arrays
  • Bit Magic

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences