📌  相关文章
📜  计算数组中的 (i, j) 对,使得 i < j 和 arr[j] – arr[i] = X * (j – i)

📅  最后修改于: 2021-10-27 09:16:27             🧑  作者: Mango

给定一个由N 个整数和一个整数X组成的数组arr[] ,任务是计算对(i, j)的数量,使得i < jarr[i] – arr[j] = X * ( j – i )

例子:

朴素的方法:解决问题的最简单的方法是从给定的数组中生成所有可能的对,对于每一对,检查给定的方程是否满足。

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

高效的方法:这个问题可以使用HashMap来解决。请按照以下步骤解决此问题:

  • 给定的条件是arr[j] – arr[i] = X * ( j – i )。这个方程可以改写为arr[j] – arr[i] = x * j – x * i,也可以写成arr[j] – x * j = arr[i] – x * i。
  • 所以现在条件改变为找到对(i, j)使得i < jarr[j] – x * j = arr[i] – x * i
  • 所以所有那些arr[index] – x * index相同的索引,它们可以配对。让我们说经过所有迭代后, arr[index] – x * index的计数是y 。所以现在从y索引中选择两个不同的索引。这只是yC2,等于(y*(y-1))/2
  • 现在,使用变量i[0, N-1]范围内迭代:
    • 在每次迭代期间执行arr[index] – x * index 。同时增加arr[index] – x * index的计数
  • 遍历地图:
    • 在每次迭代期间执行count = count + (y * (y – 1)) / 2.,其中y是 map 的第二个值。这只不过是arr[index] – x * index 的计数

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of
// pairs (i, j) such that i < j and
// arr[i] - arr[j] = X*( j - i )
void countPairs(int arr[], int n, int x)
{
 
    // Stores the count of all such
    // pairs that satisfies the condition.
    int count = 0;
 
    map mp;
 
    for (int i = 0; i < n; i++) {
 
        // Stores count of distinct
        // values of arr[i] - x * i
        mp[arr[i] - x * i]++;
    }
 
    // Iterate over the Map
    for (auto x : mp) {
 
        int n = x.second;
 
        // Increase count of pairs
        count += (n * (n - 1)) / 2;
    }
 
    // Print the count of such pairs
    cout << count;
}
 
// Driver Code
int main()
{
    int n = 6, x = 3;
    int arr[] = { 5, 4, 8, 11, 13, 16 };
 
    countPairs(arr, n, x);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count the number of
// pairs (i, j) such that i < j and
// arr[i] - arr[j] = X*( j - i )
static void countPairs(int arr[], int n, int x)
{
     
    // Stores the count of all such
    // pairs that satisfies the condition.
    int count = 0;
 
    HashMap mp = new HashMap<>();
 
    for(int i = 0; i < n; i++)
    {
         
        // Stores count of distinct
        // values of arr[i] - x * i
        mp.put(arr[i] - x * i,
               mp.getOrDefault(arr[i] - x * i, 0) + 1);
    }
 
    // Iterate over the Map
    for(int v : mp.values())
    {
         
        // Increase count of pairs
        count += (v * (v - 1)) / 2;
    }
 
    // Print the count of such pairs
    System.out.println(count);
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 6, x = 3;
    int arr[] = { 5, 4, 8, 11, 13, 16 };
 
    countPairs(arr, n, x);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count the number of
# pairs (i, j) such that i < j and
# arr[i] - arr[j] = X*( j - i )
def countPairs(arr, n, x):
     
    # Stores the count of all such
    # pairs that satisfies the condition.
    count = 0
 
    mp = {}
 
    for i in range(n):
         
        # Stores count of distinct
        # values of arr[i] - x * i
        if ((arr[i] - x * i) in mp):
            mp[arr[i] - x * i] += 1
        else:
            mp[arr[i] - x * i] = 1
 
    # Iterate over the Map
    for key, value in mp.items():
        n = value
         
        # Increase count of pairs
        count += (n * (n - 1)) // 2
 
    # Print the count of such pairs
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    n = 6
    x = 3
    arr = [ 5, 4, 8, 11, 13, 16 ]
     
    countPairs(arr, n, x)
 
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to count the number of
// pairs (i, j) such that i < j and
// arr[i] - arr[j] = X*( j - i )
static void countPairs(int[] arr, int n, int x)
{
     
    // Stores the count of all such
    // pairs that satisfies the condition.
    int count = 0;
 
    Dictionary mp = new Dictionary();
 
    for(int i = 0; i < n; i++)
    {
         
        // Stores count of distinct
        // values of arr[i] - x * i
        if (!mp.ContainsKey(arr[i] - x * i))
            mp[arr[i] - x * i] = 0;
             
        mp[arr[i] - x * i] = mp[arr[i] - x * i] + 1;
    }
 
    // Iterate over the Map
    foreach(KeyValuePair v in mp)
    {
         
        // Increase count of pairs
        count += (v.Value * (v.Value - 1)) / 2;
    }
 
    // Print the count of such pairs
    Console.WriteLine(count);
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 6, x = 3;
    int[] arr = { 5, 4, 8, 11, 13, 16 };
 
    countPairs(arr, n, x);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程