平均至少为 K 的 Array 元素对的计数
给定一个由N个整数组成的大小为N的数组A[] ,任务是计算对的数量,以使它们的平均值大于或等于K。
例子:
Input: N = 4, K = 3, A = {5, 1, 3, 4}
Output: 4
Explanation: (5, 1), (5, 3), (5, 4) and (3, 4) are the required pairs with average greater or equal to K = 3.
Input: N = 3, K = 3, A = {1, 2, 3}
Output: 0
Explanation: No pairs exist with average greater or equal to K = 3.
方法: 根据以下观察,可以使用对第一次出现的元素进行二分搜索来解决此问题:
- Just need to find 2*K – A[i] because average of two numbers X and Y is K ≤ (X+Y)/2. Now replace X with the current element that we are traversing i.e A[i] then equation becomes Y ≥ 2*K-A[i].
- So for any element A[i] in the array A[] total number of pairs formed are all the numbers in A[] that are greater than or equal to 2*K-A[i] i.e size of array ‘A’ -index of 2*K-A[i].
- So go as left as possible in A[] and for that find the first occurrence of 2*K-A[i]. If 2*K-A[i] is not found in A[] thenl return the index of next greater element of 2*K-A[i] because if average ≤ (X+Y)/2 for any two integers then also average ≤ (X+Z)/2 for all Z ≥ Y.
请按照以下步骤解决此问题:
- 对数组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)