📜  平均至少为 K 的 Array 元素对的计数

📅  最后修改于: 2022-05-13 01:56:10.739000             🧑  作者: Mango

平均至少为 K 的 Array 元素对的计数

给定一个由N个整数组成的大小为N的数组A[] ,任务是计算对的数量,以使它们的平均值大于或等于K。

例子:

方法: 根据以下观察,可以使用对第一次出现的元素进行二分搜索来解决此问题:

请按照以下步骤解决此问题:

  • 对数组A[] 进行排序。
  • 遍历数组A[]
    • 找到 2*kA[i] 的第一次出现 对于每个元素 A[i] 在 A 中。
    • 如果 2*kA[i] 不存在,则在数组 A 中找到第一次出现的刚好大于 2*kA[i] 的元素,并将其结果存储在变量中(比如ind )。
    • 如果ind不是 -1,然后在答案中添加N-ind
  • 遍历结束后返回最终答案。

下面是上述方法的实现:

C++14
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to return the index of 2*K-A[i]
int findElement(int A[], int low,
                int high, int key)
{
    int ans = -1;
 
    // Binary search
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (key <= A[mid]) {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return ans;
}
 
// Count the number of pairs
int countPairs(int A[], int& N, int& k)
{
    sort(A, A + N);
    int count = 0;
 
    // Loop to count the number of pairs
    for (int i = 0; i < N; i++) {
        int index
            = findElement(A, i + 1, N - 1,
                          2 * k - A[i]);
        if (index != -1)
            count += N - index;
    }
    return count;
}
 
// Driver Code
int main()
{
    int A[] = { 5, 1, 3, 4 };
    int N = sizeof(A) / sizeof(A[0]);
    int K = 3;
 
    // Function call
    cout << countPairs(A, N, K);
    return 0;
}


Java
// Java code to implement the approach
import java.util.*;
 
class GFG {   
 
  // Function to return the index of 2*K-A[i]
  static int findElement(int A[], int low,
                         int high, int key)
  {
    int ans = -1;
 
    // Binary search
    while (low <= high) {
      int mid = low + (high - low) / 2;
      if (key <= A[mid]) {
        ans = mid;
        high = mid - 1;
      }
      else
        low = mid + 1;
    }
    return ans;
  }
 
  // Count the number of pairs
  static int countPairs(int A[], int N, int k)
  {
    Arrays.sort(A);
    int count = 0;
 
    // Loop to count the number of pairs
    for (int i = 0; i < N; i++) {
      int index
        = findElement(A, i + 1, N - 1,
                      2 * k - A[i]);
      if (index != -1)
        count += N - index;
    }
    return count;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int A[] = { 5, 1, 3, 4 };
    int N = A.length;
    int K = 3;
 
    // Function call
    System.out.print(countPairs(A, N, K));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 program for above approach
 
# Function to return the index of 2*K-A[i]
def findElement(A, low, high, key):
    ans = -1
 
    # binary search
    while (low <= high):
        mid = low + (high - low)//2
        if key <= A[mid]:
            ans = mid
            high = mid - 1
        else:
            low = mid + 1
    return ans
 
# Count the number of pairs
def countPairs(A, N, k):
    A.sort()
    count = 0
     
    # Loop to count the number of pairs
    for i in range(N):
        index = findElement(A, i + 1, N - 1, 2 * k - A[i])
        if index != -1:
            count += N - index
    return count
 
# Driver code
A = [5, 1, 3, 4]
N = len(A)
K = 3
 
# Function call
print(countPairs(A, N, K))
 
# this code is contributed by phasing17


Javascript


输出
4

时间复杂度: O(N log N)
辅助空间: O(N)