给定一个整数数组ARR的大小N和整数K [],任务是找到子阵列ARR [我…·J其中i≤j和计算所有子数组元素的按位说X然后打印| K – X |的最小值在X的所有可能值中。
例子:
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
方法1:
找到所有可能的子数组的按位与,并跟踪| K – X |的最小可能值。 。
下面是上述方法的实现:
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[0]);
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
Javascript
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[0]);
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
输出:
0
时间复杂度: O(n 2 )
方法2:
可以观察到,在子阵列中执行“与”运算时, X的值可以保持恒定或减小,但永远不会增大。
因此,我们将从子数组的第一个元素开始,将按位与并比较| K – X |。直到X≤K为止的电流最小差,因为在那之后| K – X |将开始增加。
下面是上述方法的实现:
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[0]);
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
输出:
0
时间复杂度: O(n 2 )