📌  相关文章
📜  在数组的所有对中找到给定表达式的最小值

📅  最后修改于: 2021-05-25 05:40:00             🧑  作者: Mango

给定大小为N的数组A,找到表达式的最小值: (A_i \& A_j) \oplus (A_i | A_j)    在所有对(i,j)上(其中i!= j)。这里\&    |    \oplus    分别代表按位AND,按位OR和按位XOR。
例子:

Input:  A = [1, 2, 3, 4, 5]
Output:  1
Explanation: 
(A[1] and A[2]) xor (A[1] or A[2]) = 1,
which is minimum possible value.

Input : A = [12, 3, 14, 5, 9, 8]
Output : 1

天真的方法:
遍历具有不同索引的数组的所有对,并在其上找到给定表达式的最小可能值。
下面执行上述方法。

C++
// C++ program to find the minimum
// value of the given expression
// over all pairs of the array
#include 
using namespace std;
 
// Function to find the minimum
// value of the expression
int MinimumValue(int a[], int n)
{
 
    int answer = INT_MAX;
     
    // Iterate over all the pairs
    // and find the minimum value
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            answer = min(answer,
                         ((a[i] & a[j])
                          ^ (a[i] | a[j])));
        }
    }
    return answer;
}
// Driver code
int main()
{
    int N = 6;
    int A[N] = { 12, 3, 14, 5, 9, 8 };
 
    cout << MinimumValue(A, N);
 
    return 0;
}


Java
// Java program to find the minimum
// value of the given expression
// over all pairs of the array
import java.io.*;
import java.util.Arrays;
 
class GFG
{
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int a[], int n)
{
 
    int answer = Integer.MAX_VALUE;
     
    // Iterate over all the pairs
    // and find the minimum value
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            answer = Math.min(answer, ((a[i] & a[j])
                                  ^ (a[i] | a[j])));
        }
    }
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
    System.out.print(MinimumValue(A, N));
 
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program to find the minimum
# value of the given expression
# over all pairs of the array
import sys
 
# Function to find the minimum
# value of the expression
def MinimumValue(a,n):
    answer = sys.maxsize
     
    # Iterate over all the pairs
    # and find the minimum value
    for i in range(n):
        for j in range(i + 1, n, 1):
            answer = min(answer,((a[i] & a[j])^(a[i] | a[j])))
    return answer
 
# Driver code
if __name__ == '__main__':
    N = 6
    A = [12, 3, 14, 5, 9, 8]
 
    print(MinimumValue(A, N))
 
# This code is contributed by Bhupendra_Singh


C#
// C# program to find the minimum
// value of the given expression
// over all pairs of the array
using System;
 
class GFG{
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int []a, int n)
{
    int answer = Int32.MaxValue;
     
    // Iterate over all the pairs
    // and find the minimum value
    for(int i = 0; i < n; i++)
    {
       for(int j = i + 1; j < n; j++)
       {
           answer = Math.Min(answer,
                           ((a[i] & a[j]) ^
                            (a[i] | a[j])));
       }
    }
    return answer;
}
 
// Driver Code
public static void Main()
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
    Console.Write(MinimumValue(A, N));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// C++ program to find the minimum
// value of the given expression
// over all pairs of the array
#include 
using namespace std;
 
// Function to find the minimum
// value of the expression
int MinimumValue(int arr[], int n)
{
 
    // The expression simplifies to
    // finding the minimum xor
    // value pair
 
    // Sort given array
    sort(arr, arr + n);
 
    int minXor = INT_MAX;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for (int i = 0; i < n - 1; i++)
    {
        val = arr[i] ^ arr[i + 1];
        minXor = min(minXor, val);
    }
 
    return minXor;
}
 
// Driver code
int main()
{
    int N = 6;
    int A[N] = { 12, 3, 14, 5, 9, 8 };
 
    cout << MinimumValue(A, N);
 
    return 0;
}


Java
// Java program to find the minimum
// value of the given expression
// over all pairs of the array
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int arr[], int n)
{
     
    // The expression simplifies to
    // finding the minimum xor
    // value pair
    // Sort given array
    Arrays.sort(arr);
 
    int minXor = Integer.MAX_VALUE;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for(int i = 0; i < n - 1; i++)
    {
       val = arr[i] ^ arr[i + 1];
       minXor = Math.min(minXor, val);
    }
 
    return minXor;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
 
    System.out.print(MinimumValue(A, N));
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program to find the minimum
# value of the given expression
# over all pairs of the array
import sys
 
# Function to find the minimum
# value of the expression
def MinimumValue(arr, n):
 
    # The expression simplifies
    # to finding the minimum
    # xor value pair
    # Sort given array
    arr.sort();
 
    minXor = sys.maxsize;
    val = 0;
 
    # Calculate min xor of
    # consecutive pairs
    for i in range(0, n - 1):
        val = arr[i] ^ arr[i + 1];
        minXor = min(minXor, val);
     
    return minXor;
 
# Driver code
if __name__ == '__main__':
     
    N = 6;
    A = [ 12, 3, 14, 5, 9, 8 ];
 
    print(MinimumValue(A, N));
     
# This code is contributed by sapnasingh4991


C#
// C# program to find the minimum
// value of the given expression
// over all pairs of the array
using System;
 
class GFG{
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int []arr, int n)
{
     
    // The expression simplifies to
    // finding the minimum xor
    // value pair
    // Sort given array
    Array.Sort(arr);
 
    int minXor = Int32.MaxValue;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for(int i = 0; i < n - 1; i++)
    {
       val = arr[i] ^ arr[i + 1];
       minXor = Math.Min(minXor, val);
    }
     
    return minXor;
}
 
// Driver Code
public static void Main()
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
 
    Console.Write(MinimumValue(A, N));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
1

时间复杂度: O(N 2 )
高效方法

  • 让我们简化给定的表达式:
  • 由于表达式简化为最小xor值对,因此我们可以简单地使用本文中提到的算法来高效地找到相同的值。

下面执行上述方法。

C++

// C++ program to find the minimum
// value of the given expression
// over all pairs of the array
#include 
using namespace std;
 
// Function to find the minimum
// value of the expression
int MinimumValue(int arr[], int n)
{
 
    // The expression simplifies to
    // finding the minimum xor
    // value pair
 
    // Sort given array
    sort(arr, arr + n);
 
    int minXor = INT_MAX;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for (int i = 0; i < n - 1; i++)
    {
        val = arr[i] ^ arr[i + 1];
        minXor = min(minXor, val);
    }
 
    return minXor;
}
 
// Driver code
int main()
{
    int N = 6;
    int A[N] = { 12, 3, 14, 5, 9, 8 };
 
    cout << MinimumValue(A, N);
 
    return 0;
}

Java

// Java program to find the minimum
// value of the given expression
// over all pairs of the array
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int arr[], int n)
{
     
    // The expression simplifies to
    // finding the minimum xor
    // value pair
    // Sort given array
    Arrays.sort(arr);
 
    int minXor = Integer.MAX_VALUE;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for(int i = 0; i < n - 1; i++)
    {
       val = arr[i] ^ arr[i + 1];
       minXor = Math.min(minXor, val);
    }
 
    return minXor;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
 
    System.out.print(MinimumValue(A, N));
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python3 program to find the minimum
# value of the given expression
# over all pairs of the array
import sys
 
# Function to find the minimum
# value of the expression
def MinimumValue(arr, n):
 
    # The expression simplifies
    # to finding the minimum
    # xor value pair
    # Sort given array
    arr.sort();
 
    minXor = sys.maxsize;
    val = 0;
 
    # Calculate min xor of
    # consecutive pairs
    for i in range(0, n - 1):
        val = arr[i] ^ arr[i + 1];
        minXor = min(minXor, val);
     
    return minXor;
 
# Driver code
if __name__ == '__main__':
     
    N = 6;
    A = [ 12, 3, 14, 5, 9, 8 ];
 
    print(MinimumValue(A, N));
     
# This code is contributed by sapnasingh4991

C#

// C# program to find the minimum
// value of the given expression
// over all pairs of the array
using System;
 
class GFG{
 
// Function to find the minimum
// value of the expression
static int MinimumValue(int []arr, int n)
{
     
    // The expression simplifies to
    // finding the minimum xor
    // value pair
    // Sort given array
    Array.Sort(arr);
 
    int minXor = Int32.MaxValue;
    int val = 0;
 
    // Calculate min xor of
    // consecutive pairs
    for(int i = 0; i < n - 1; i++)
    {
       val = arr[i] ^ arr[i + 1];
       minXor = Math.Min(minXor, val);
    }
     
    return minXor;
}
 
// Driver Code
public static void Main()
{
    int N = 6;
    int[] A = new int[]{ 12, 3, 14, 5, 9, 8 };
 
    Console.Write(MinimumValue(A, N));
}
}
 
// This code is contributed by shivanisinghss2110

Java脚本


输出:
1

时间复杂度: O(N * log(N))

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。