给定两个分别由大小分别为N和M的不同元素和整数K组成的整数数组arr []和brr [] ,任务是找到对(arr [i],brr [j])的对数,使得(brr [j] – arr [i])> K。
例子:
Input: arr[] = {5, 9, 1, 8}, brr[] {10, 12, 7, 4, 2, 3}, K = 3
Output: 6
Explanation:
Possible pairs that satisfy the given conditions are: { (5, 10), (5, 12), (1, 10), (1, 12), (1, 7), (8, 12) }.
Therefore, the required output is 6.
Input: arr[] = {2, 10}, brr[] = {5, 7}, K = 2
Output: 2
Explanation:
Possible pairs that satisfy the given conditions are: { (2, 5), (2, 7) }.
Therefore, the required output is 2.
天真的方法:解决此问题的最简单方法是遍历数组并生成给定数组的所有可能的对,对于每对,检查(brr [j] – arr [i])是否大于K。如果发现为真,则增加计数器。最后,打印计数器的值。
时间复杂度: O(N×M)
辅助空间: O(1)
高效的方法:为了优化上述方法,其思想是首先对数组进行排序,然后使用两种指针技术。请按照以下步骤解决问题:
- 初始化一个变量,例如cntPairs,以存储满足给定条件的对数。
- 对给定的数组进行排序。
- 初始化两个变量,例如i = 0和j = 0 ,分别存储左指针和右指针的索引。
- 遍历数组并检查(brr [j] – arr [i])是否>K 。如果发现为真,则更新cntPairs + =(M – j)的值并增加i指针变量的值。
- 否则,增加j指针变量的值。
- 最后,打印cntPrint的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to count pairs that satisfy
// the given conditions
int count_pairs(int arr[], int brr[],
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
sort(arr, arr + N);
// Sort brr[] array
sort(brr, brr + M);
// Traverse both the array
// and count then pairs
while (i < N && j < M) {
// If the value of
// (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K) {
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else {
// Update j
j++;
}
}
return cntPairs;
}
// Driver Code
int main()
{
int arr[] = { 5, 9, 1, 8 };
int brr[] = { 10, 12, 7, 4, 2, 3 };
int K = 3;
int N = sizeof(arr) / sizeof(arr[0]);
int M = sizeof(brr) / sizeof(brr[0]);
cout << count_pairs(arr, brr, N, M, K);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to count pairs that satisfy
// the given conditions
static int count_pairs(int arr[], int brr[],
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
Arrays.sort(arr);
// Sort brr[] array
Arrays.sort(brr);
// Traverse both the array
// and count then pairs
while (i < N && j < M)
{
// If the value of
// (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K)
{
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else
{
// Update j
j++;
}
}
return cntPairs;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 5, 9, 1, 8 };
int brr[] = { 10, 12, 7, 4, 2, 3 };
int K = 3;
int N = arr.length;
int M = brr.length;
System.out.println(count_pairs(arr, brr, N, M, K));
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# Python3 program to implement
# the above approach
# Function to count pairs that satisfy
# the given conditions
def count_pairs(arr, brr, N, M, K):
# Stores index of
# the left pointer.
i = 0
# Stores index of
# the right pointer
j = 0
# Stores count of total pairs
# that satisfy the conditions
cntPairs = 0
# Sort arr[] array
arr = sorted(arr)
# Sort brr[] array
brr = sorted(brr)
# Traverse both the array
# and count then pairs
while (i < N and j < M):
# If the value of
# (brr[j] - arr[i]) exceeds K
if (brr[j] - arr[i] > K):
# Update cntPairs
cntPairs += (M - j)
# Update
i += 1
else:
# Update j
j += 1
return cntPairs
# Driver Code
if __name__ == '__main__':
arr = [ 5, 9, 1, 8 ]
brr = [ 10, 12, 7, 4, 2, 3 ]
K = 3
N = len(arr)
M = len(brr)
print(count_pairs(arr, brr, N, M, K))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count pairs
// that satisfy the given
// conditions
static int count_pairs(int[] arr, int[] brr,
int N, int M, int K)
{
// Stores index of
// the left pointer.
int i = 0;
// Stores index of
// the right pointer
int j = 0;
// Stores count of total pairs
// that satisfy the conditions
int cntPairs = 0;
// Sort arr[] array
Array.Sort(arr);
// Sort brr[] array
Array.Sort(brr);
// Traverse both the array
// and count then pairs
while (i < N && j < M)
{
// If the value of
// (brr[j] - arr[i])
// exceeds K
if (brr[j] - arr[i] > K)
{
// Update cntPairs
cntPairs += (M - j);
// Update
i++;
}
else
{
// Update j
j++;
}
}
return cntPairs;
}
// Driver code
static void Main()
{
int[] arr = {5, 9, 1, 8};
int[] brr = {10, 12,
7, 4, 2, 3};
int K = 3;
int N = arr.Length;
int M = brr.Length;
Console.WriteLine(
count_pairs(arr, brr,
N, M, K));
}
}
// This code is contributed by divyeshrabadiya07
6
时间复杂度: O(N * log(N)+ M * log(M))
辅助空间: O(1)