📌  相关文章
📜  从两个数组中计算差异超过 K 的对

📅  最后修改于: 2021-09-06 06:28:55             🧑  作者: Mango

给定两个整数数组arr[]brr[] 分别由大小为NM的不同元素和一个整数K 组成,任务是找到对(arr[i], brr[j])的计数,使得(brr [j] – arr[i]) > K。

例子:

朴素的方法:解决这个问题的最简单的方法是遍历数组并生成给定数组的所有可能对,对于每一对,检查是否(brr[j] – arr[i]) > K。如果发现为真,则增加计数器。最后,打印计数器的值。

时间复杂度: O(N × M)
辅助空间: O(1)

高效的方法:优化上述方法的想法是首先对数组进行排序,然后使用两种指针技术。请按照以下步骤解决问题:

  • 初始化一个变量,比如cntPairs来存储满足给定条件的对的计数。
  • 对给定的数组进行排序。
  • 初始化两个变量,比如i = 0j = 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live