📜  | ai + aj – k |的最小可能值对于给定的数组和k。

📅  最后修改于: 2021-05-24 22:03:46             🧑  作者: Mango

给您一个由n个整数和一个整数K组成的数组。找到总数为{i,j}的无序对的数量,使得(ai + aj – K)的绝对值,即| ai + aj – k |当i!= j时,可能性最小。
例子:

Input : arr[] = {0, 4, 6, 2, 4}, 
            K = 7
Output : Minimal Value = 1
         Total  Pairs = 5 
Explanation : Pairs resulting minimal value are :
              {a1, a3}, {a2, a4}, {a2, a5}, {a3, a4}, {a4, a5} 

Input : arr[] = {4, 6, 2, 4}  , K = 9
Output : Minimal Value = 1
         Total Pairs = 4 
Explanation : Pairs resulting minimal value are :
              {a1, a2}, {a1, a4}, {a2, a3}, {a2, a4} 

一个简单的解决方案是遍历所有可能的对,对于每对,我们将检查(ai + aj – K)的值是否小于当前的最小值not。因此,根据上述条件,我们总共有三种情况:

  1. abs(ai + aj – K)>最小:不执行任何操作,因为这对将不计入最小可能值。
  2. abs(ai + aj – K)=最小:增加对的计数,得出最小可能值。
  3. abs(ai + aj – K)<最小:更新最小值并将计数设置为1。
C++
// CPP program to find number of pairs  and minimal
// possible value
#include
using namespace std;
 
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
    // initialize smallest and count
    int smallest = INT_MAX;
    int count=0;
 
    // iterate over all pairs
    for (int i=0; i


Java
// Java program to find number of pairs
// and minimal possible value
import java.util.*;
 
class GFG {
     
    // function for finding pairs and min value
    static void pairs(int arr[], int n, int k)
    {
        // initialize smallest and count
        int smallest = Integer.MAX_VALUE;
        int count=0;
      
        // iterate over all pairs
        for (int i=0; iPython3
# Python3 program to find number of pairs
# and minimal possible value
 
# function for finding pairs and min value
def pairs(arr, n, k):
     
    # initialize smallest and count
    smallest = 999999999999
    count = 0
 
    # iterate over all pairs
    for i in range(n):
        for j in range(i + 1, n):
             
            # is abs value is smaller than smallest
            # update smallest and reset count to 1
            if abs(arr[i] + arr[j] - k) < smallest:
                smallest = abs(arr[i] + arr[j] - k)
                count = 1
 
            # if abs value is equal to smallest
            # increment count value
            elif abs(arr[i] + arr[j] - k) == smallest:
                count += 1
 
    # print result
    print("Minimal Value = ", smallest)
    print("Total Pairs = ", count)
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 5, 7, 5, 1, 9, 9]
    k = 12
    n = len(arr)
    pairs(arr, n, k)
     
# This code is contributed by PranchalK


C#
// C# program to find number
// of pairs and minimal
// possible value
using System;
 
class GFG
{
 
// function for finding
// pairs and min value
static void pairs(int []arr,
                  int n, int k)
{
    // initialize
    // smallest and count
    int smallest = 0;
    int count = 0;
 
    // iterate over all pairs
    for (int i = 0; i < n; i++)
        for(int j = i + 1; j < n; j++)
        {
            // is abs value is smaller
            // than smallest update
            // smallest and reset
            // count to 1
            if (Math.Abs(arr[i] +
                arr[j] - k) < smallest )
            {
                smallest = Math.Abs(arr[i] +
                                    arr[j] - k);
                count = 1;
            }
 
            // if abs value is equal
            // to smallest increment
            // count value
            else if (Math.Abs(arr[i] +
                              arr[j] - k) ==
                                smallest)
                count++;
        }
 
    // print result
    Console.WriteLine("Minimal Value = " +
                                smallest);
    Console.WriteLine("Total Pairs = " +
                                 count);
}
 
// Driver Code
public static void Main()
{
    int []arr = {3, 5, 7,
                 5, 1, 9, 9};
    int k = 12;
    int n = arr.Length;
    pairs(arr, n, k);
}
}
 
// This code is contributed
// by anuj_67.


PHP


Javascript


C++
// C++ program to find number of pairs
// and minimal possible value
#include
using namespace std;
 
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
    // initialize smallest and count
    int smallest = INT_MAX, count = 0;
    set s;
 
    // iterate over all pairs
    s.insert(arr[0]);
    for (int i=1; i


输出:

Minimal Value = 0
Total Pairs = 4

一个有效的解决方案是使用自平衡二进制搜索树(在C++中以set形式实现,在JavaTreeSet形式实现)。我们可以在地图的O(log n)时间中找到最接近的元素。

C++

// C++ program to find number of pairs
// and minimal possible value
#include
using namespace std;
 
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
    // initialize smallest and count
    int smallest = INT_MAX, count = 0;
    set s;
 
    // iterate over all pairs
    s.insert(arr[0]);
    for (int i=1; i

输出:

Minimal Value = 0
Total Pairs = 4