# Bitwise AND of sub-array closest to K

Given an integer array arr[] of size N and an integer K, the task is to find the sub-array arr[i….j] where i ≤ j and compute the bitwise AND of all sub-array elements say X then print the minimum value of |K – X| among all possible values of X.

Example:

Input: arr[] = {1, 6}, K = 3
Output: 2

Sub-array Bitwise AND |K – X|
{1} 1 2
{6} 6 3
{1, 6} 1 2

Input: arr[] = {4, 7, 10}, K = 2
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1:
Find the bitwise AND of all possible sub-arrays and keep track of the minimum possible value of |K – X|.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum possible value ` `// of |K - X| where X is the bitwise AND of ` `// the elements of some sub-array ` `int` `closetAND(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `ans = INT_MAX; ` ` `  `    ``// Check all possible sub-arrays ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``int` `X = arr[i]; ` `        ``for` `(``int` `j = i; j < n; j++) { ` `            ``X &= arr[j]; ` ` `  `            ``// Find the overall minimum ` `            ``ans = min(ans, ``abs``(k - X)); ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 7, 10 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 2; ` `    ``cout << closetAND(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.ArrayList; ` `import` `java.util.Collections; ` `import` `java.util.List; ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to return the minimum possible value ` `    ``// of |K - X| where X is the bitwise AND of ` `    ``// the elements of some sub-array ` `    ``static` `int` `closetAND(``int` `arr[], ``int` `n, ``int` `k) ` `    ``{ ` `        ``int` `ans = Integer.MAX_VALUE; ` ` `  `        ``// Check all possible sub-arrays ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``int` `X = arr[i]; ` `            ``for` `(``int` `j = i; j < n; j++) { ` `                ``X &= arr[j]; ` ` `  `                ``// Find the overall minimum ` `                ``ans = Math.min(ans, Math.abs(k - X)); ` `            ``} ` `        ``} ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `arr[] = { ``4``, ``7``, ``10` `}; ` `        ``int` `n = arr.length; ` `        ``int` `k = ``2``; ` `        ``System.out.println(closetAND(arr, n, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by jit_t `

## Python3

 `# Python implementation of the approach ` ` `  `# Function to return the minimum possible value ` `# of |K - X| where X is the bitwise AND of ` `# the elements of some sub-array ` `def` `closetAND(arr, n, k): ` ` `  `    ``ans ``=` `10``*``*``9` ` `  `    ``# Check all possible sub-arrays ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``X ``=` `arr[i] ` ` `  `        ``for` `j ``in` `range``(i,n): ` `            ``X &``=` `arr[j] ` ` `  `            ``# Find the overall minimum ` `            ``ans ``=` `min``(ans, ``abs``(k ``-` `X)) ` `         `  `    ``return` `ans ` ` `  `# Driver code ` `arr ``=` `[``4``, ``7``, ``10``] ` `n ``=` `len``(arr) ` `k ``=` `2``; ` `print``(closetAND(arr, n, k)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `    ``// Function to return the minimum possible value  ` `    ``// of |K - X| where X is the bitwise AND of  ` `    ``// the elements of some sub-array  ` `    ``static` `int` `closetAND(``int` `[]arr, ``int` `n, ``int` `k)  ` `    ``{  ` `        ``int` `ans = ``int``.MaxValue;  ` ` `  `        ``// Check all possible sub-arrays  ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{  ` ` `  `            ``int` `X = arr[i];  ` `            ``for` `(``int` `j = i; j < n; j++) ` `            ``{  ` `                ``X &= arr[j];  ` ` `  `                ``// Find the overall minimum  ` `                ``ans = Math.Min(ans, Math.Abs(k - X));  ` `            ``}  ` `        ``}  ` `        ``return` `ans;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]arr = { 4, 7, 10 };  ` `        ``int` `n = arr.Length;  ` `        ``int` `k = 2;  ` `         `  `        ``Console.WriteLine(closetAND(arr, n, k));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01 `

## PHP

 ` `

Output:

```0
```

Time complexity: O(n2)

Method 2:
It can be observed that while performing AND operation in the sub-array, the value of X can remain constant or decrease but will never increase.
Hence, we will start from the first element of a sub-array and will be doing bitwise AND and comparing the |K – X| with the current minimum difference until X ≤ K because after that |K – X| will start increasing.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum possible value ` `// of |K - X| where X is the bitwise AND of ` `// the elements of some sub-array ` `int` `closetAND(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `ans = INT_MAX; ` ` `  `    ``// Check all possible sub-arrays ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``int` `X = arr[i]; ` `        ``for` `(``int` `j = i; j < n; j++) { ` `            ``X &= arr[j]; ` ` `  `            ``// Find the overall minimum ` `            ``ans = min(ans, ``abs``(k - X)); ` ` `  `            ``// No need to perform more AND operations ` `            ``// as |k - X| will increase ` `            ``if` `(X <= k) ` `                ``break``; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 7, 10 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `k = 2; ` `    ``cout << closetAND(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` ` `  `// Function to return the minimum possible value ` `// of |K - X| where X is the bitwise AND of ` `// the elements of some sub-array ` `static` `int` `closetAND(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``int` `ans = Integer.MAX_VALUE; ` ` `  `    ``// Check all possible sub-arrays ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` ` `  `        ``int` `X = arr[i]; ` `        ``for` `(``int` `j = i; j < n; j++)  ` `        ``{ ` `            ``X &= arr[j]; ` ` `  `            ``// Find the overall minimum ` `            ``ans = Math.min(ans, Math.abs(k - X)); ` ` `  `            ``// No need to perform more AND operations ` `            ``// as |k - X| will increase ` `            ``if` `(X <= k) ` `                ``break``; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `arr[] = { ``4``, ``7``, ``10` `}; ` `    ``int` `n = arr.length; ` `    ``int` `k = ``2``; ` `    ``System.out.println(closetAND(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## Python3

 `# Python implementation of the approach ` `import` `sys ` ` `  `# Function to return the minimum possible value ` `# of |K - X| where X is the bitwise AND of ` `# the elements of some sub-array ` `def` `closetAND(arr, n, k): ` `    ``ans ``=` `sys.maxsize; ` ` `  `    ``# Check all possible sub-arrays ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``X ``=` `arr[i]; ` `        ``for` `j ``in` `range``(i,n): ` `            ``X &``=` `arr[j]; ` ` `  `            ``# Find the overall minimum ` `            ``ans ``=` `min``(ans, ``abs``(k ``-` `X)); ` ` `  `            ``# No need to perform more AND operations ` `            ``# as |k - X| will increase ` `            ``if` `(X <``=` `k): ` `                ``break``; ` `    ``return` `ans; ` ` `  `# Driver code ` `arr ``=` `[``4``, ``7``, ``10` `]; ` `n ``=` `len``(arr); ` `k ``=` `2``; ` `print``(closetAND(arr, n, k)); ` ` `  `# This code is contributed by PrinciRaj1992 `

## C#

 `// C# implementation of the approach ` `using` `System;      ` `     `  `class` `GFG  ` `{ ` ` `  `// Function to return the minimum possible value ` `// of |K - X| where X is the bitwise AND of ` `// the elements of some sub-array ` `static` `int` `closetAND(``int` `[]arr, ``int` `n, ``int` `k) ` `{ ` `    ``int` `ans = ``int``.MaxValue; ` ` `  `    ``// Check all possible sub-arrays ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``int` `X = arr[i]; ` `        ``for` `(``int` `j = i; j < n; j++)  ` `        ``{ ` `            ``X &= arr[j]; ` ` `  `            ``// Find the overall minimum ` `            ``ans = Math.Min(ans, Math.Abs(k - X)); ` ` `  `            ``// No need to perform more AND operations ` `            ``// as |k - X| will increase ` `            ``if` `(X <= k) ` `                ``break``; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `[]arr = { 4, 7, 10 }; ` `    ``int` `n = arr.Length; ` `    ``int` `k = 2; ` `    ``Console.WriteLine(closetAND(arr, n, k)); ` `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

Output:

```0
```

Time complexity: O(n2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.