给定大小为N的数组arr [] ,任务是计算对数(arr [i],arr [j]) ,以使arr [j] – arr [i] = j – i 。
例子:
Input: arr[] = {5, 2, 7}
Output: 1
The only valid pair is (arr[0], arr[2]) as 7 – 5 = 2 – 0 = 2.
Input: arr[] = {1, 2, 3, 4}
Output: 6
方法:如果(arr [j] – arr [i])=(j – i) ,则一对(arr [i],arr [j])被说成是有效的,它也可以写成(arr [j] – j)=(arr [i] – i) ,它是元素与其索引的差。现在,任务是将数组划分为多个组,以使每个组的元素与其索引的差值相等,然后对于每个组,如果它具有N个元素,则可能的对数为(N *(N – 1)) / 2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count
// of all valid pairs
int countPairs(int arr[], int n)
{
// To store the frequencies
// of (arr[i] - i)
unordered_map map;
for (int i = 0; i < n; i++)
map[arr[i] - i]++;
// To store the required count
int res = 0;
for (auto x : map) {
int cnt = x.second;
// If cnt is the number of elements
// whose differecne with their index
// is same then ((cnt * (cnt - 1)) / 2)
// such pairs are possible
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
// Driver code
int main()
{
int arr[] = { 1, 5, 6, 7, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countPairs(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.HashMap;
class GFG
{
// Function to return the count
// of all valid pairs
static int countPairs(int arr[], int n)
{
// To store the frequencies
// of (arr[i] - i)
HashMap map = new HashMap();
for (int i = 0; i < n; i++)
map.put(arr[i] - i, 0);
for (int i = 0; i < n; i++)
map.put(arr[i] - i, map.get(arr[i] - i) + 1);
// To store the required count
int res = 0;
for (int x : map.values())
{
int cnt = x;
// If cnt is the number of elements
// whose differecne with their index
// is same then ((cnt * (cnt - 1)) / 2)
// such pairs are possible
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 5, 6, 7, 9 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the count
# of all valid pairs
def countPairs(arr, n):
# To store the frequencies
# of (arr[i] - i)
map = dict()
for i in range(n):
map[arr[i] - i] = map.get(arr[i] - i, 0) + 1
# To store the required count
res = 0
for x in map:
cnt = map[x]
# If cnt is the number of elements
# whose differecne with their index
# is same then ((cnt * (cnt - 1)) / 2)
# such pairs are possible
res += ((cnt * (cnt - 1)) // 2)
return res
# Driver code
arr = [1, 5, 6, 7, 9]
n = len(arr)
print(countPairs(arr, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count
// of all valid pairs
static int countPairs(int []arr, int n)
{
// To store the frequencies
// of (arr[i] - i)
Dictionary map = new Dictionary();
for (int i = 0; i < n; i++)
map[arr[i] - i] = 0;
for (int i = 0; i < n; i++)
map[arr[i] - i]++;
// To store the required count
int res = 0;
foreach(KeyValuePair x in map)
{
int cnt = x.Value;
// If cnt is the number of elements
// whose differecne with their index
// is same then ((cnt * (cnt - 1)) / 2)
// such pairs are possible
res += ((cnt * (cnt - 1)) / 2);
}
return res;
}
// Driver code
public static void Main (String []args)
{
int []arr = { 1, 5, 6, 7, 9 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
// This code is contributed by Arnab Kundu
输出:
3