📌  相关文章
📜  大小为K的子数组的按位运算

📅  最后修改于: 2021-05-05 01:09:38             🧑  作者: Mango

给定正整数数组arr []和数字K ,任务是找到大小为K的子数组元素上按位运算的最小值最大值

例子:

天真的方法:天真的方法是生成大小为K的所有可能的子数组,并检查上面形成的子数组中的哪一个将给出最小和最大的按位“或”和“与”。
时间复杂度: O(N 2 )
辅助空间: O(K)

高效的方法:想法是使用滑动窗口技术来解决此问题。步骤如下:

  1. 遍历大小为K的前缀数组,对于每个数组,元素都将遍历它的每个位,并将位数组(通过保持大小为32的整数数组位)设置为1。
  2. 将该位数组转换为十进制数字,例如ans ,然后将滑动窗口移至下一个索引。
  3. 对于大小为K的下一个子数组的新添加元素,遍历新添加元素的每个位,如果已设置,则将位数组增加1
  4. 要从上一个窗口中删除第一个元素,请将位数组减1(如果已设置)。
  5. 用位数组生成的新十进制的最小值或最大值更新ans
  • 下面是查找最大按位或子数组的程序
C++
// C++ program for maximum values of
// each bitwise OR operation on
// element of subarray of size K
#include 
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


C++
// C++ program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
#include 
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


C++
// C++ program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
#include 
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


C++
// C++ program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
#include 
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


C++
// C++ program to find the subarray
/// with minimum XOR
#include 
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


输出:
15

时间复杂度: O(n * B) ,其中n是数组的大小,B是大小为32的整数数组位。
辅助空间: O(n)

  • 下面是查找Minimum Bitwise OR子数组的程序

C++

// C++ program for minimum values of
// each bitwise OR operation on
// element of subarray of size K
#include 
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

Java脚本


输出:
7

时间复杂度: O(n * B) ,其中n是数组的大小,B是大小为32的整数数组位。
辅助空间: O(n)

  • 下面是找到最大按位AND子数组的程序

C++

// C++ program for maximum values of
// each bitwise AND operation on
// element of subarray of size K
#include 
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
输出:
2

时间复杂度: O(n * B) ,其中n是数组的大小,B是大小为32的整数数组位。
辅助空间: O(n)

  • 以下是查找Minimum Bitwise AND子数组的程序

C++

// C++ program for minimum values of
// each bitwise AND operation on
// elements of subarray of size K
#include 
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

Java脚本


输出:
0

时间复杂度: O(n * B) ,其中n是数组的大小,B是大小为32的整数数组位。
辅助空间: O(n)

  • 以下是查找最小按位XOR子数组的程序

C++

// C++ program to find the subarray
/// with minimum XOR
#include 
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

Java脚本


输出:
16

时间复杂度: O(n * B) ,其中n是数组的大小,B是大小为32的整数数组位。
辅助空间: O(n)