📜  计数给定数组中的对,其索引和该索引处的值之和相等

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

计数给定数组中的对,其索引和该索引处的值之和相等

给定一个包含正整数的数组arr[] ,计算满足arr[i]+i = arr[j]+j的对的总数,使得0≤i

例子

朴素方法:蛮力方法是通过迭代两个循环并计算所有遵循arr[i]+i = arr[j]+j的对来实现的。

下面是上述方法的实现:

C++
// C++ program to find Count the pair of
// elements in  an array such that
// arr[i]+i=arr[j]+j
#include 
using namespace std;
 
// Function to return the
// total count of pairs
int count(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if ((arr[i] + i) == (arr[j] + j)) {
                ans++;
            }
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 6, 1, 4, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << count(arr, N);
    return 0;
}


Java
// Java program to find Count the pair of
// elements in  an array such that
// arr[i]+i=arr[j]+j
import java.io.*;
class GFG
{
 
  // Function to return the
  // total count of pairs
  static int count(int arr[], int n)
  {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if ((arr[i] + i) == (arr[j] + j)) {
          ans++;
        }
      }
    }
    return ans;
  }
 
  // Driver code
  public static void main (String[] args) {
    int arr[] = { 6, 1, 4, 3 };
    int N = arr.length;
    System.out.println(count(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


C#
// C# program to find Count the pair of
// elements in  an array such that
// arr[i]+i=arr[j]+j
using System;
class GFG {
 
    // Function to return the
    // total count of pairs
    static int count(int[] arr, int n)
    {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if ((arr[i] + i) == (arr[j] + j)) {
                    ans++;
                }
            }
        }
        return ans;
    }
 
    // Driver code
    public static int Main()
    {
        int[] arr = new int[] { 6, 1, 4, 3 };
        int N = arr.Length;
        Console.WriteLine(count(arr, N));
        return 0;
    }
}
 
// This code is contributed by Taranpreet


Javascript


C++
// C++ program to find Count the pair of
// elements in  an array such that
// arr[i]+i=arr[j]+j
#include 
using namespace std;
 
// Function to return the
// total count of pairs
int count(int arr[], int n)
{
    // Modifying the array by storing
    // a[i]+i at all instances
    for (int i = 0; i < n; i++) {
        arr[i] = arr[i] + i;
    }
 
    // Using unordered_map to store
    // the elements
    unordered_map mp;
 
    for (int i = 0; i < n; i++) {
        mp[arr[i]]++;
    }
 
    // Now for each elements in unordered_map
    // we are using the count of that element
    // to determine the number of pairs possible
    int ans = 0;
    for (auto it = mp.begin(); it != mp.end(); it++) {
        int val = it->second;
        ans += (val * (val - 1)) / 2;
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << count(arr, N);
    return 0;
}



输出
3

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

高效方法:这个问题可以通过在 C++ 中使用 unordered_map 来有效解决。这样做是为了在 O(1) 的平均时间内存储相似的元素计数。然后对于每个相似的元素,我们可以使用该元素的计数来评估对的总数,如

下面是上述方法的实现:

C++

// C++ program to find Count the pair of
// elements in  an array such that
// arr[i]+i=arr[j]+j
#include 
using namespace std;
 
// Function to return the
// total count of pairs
int count(int arr[], int n)
{
    // Modifying the array by storing
    // a[i]+i at all instances
    for (int i = 0; i < n; i++) {
        arr[i] = arr[i] + i;
    }
 
    // Using unordered_map to store
    // the elements
    unordered_map mp;
 
    for (int i = 0; i < n; i++) {
        mp[arr[i]]++;
    }
 
    // Now for each elements in unordered_map
    // we are using the count of that element
    // to determine the number of pairs possible
    int ans = 0;
    for (auto it = mp.begin(); it != mp.end(); it++) {
        int val = it->second;
        ans += (val * (val - 1)) / 2;
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << count(arr, N);
    return 0;
}
输出
28

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