给定一个由N 个整数组成的数组arr[] ,任务是找到索引总和与索引处元素之和相同的对(i, j)的数量。
例子:
Input: arr[] = {0, 1, 7, 4, 3, 2}
Output: 1
Explanation: There exists only pair that satisfies the condition is {(0, 1)}.
Input: arr[] = {1, 6, 2, 4, 5, 6}
Output: 0
朴素方法:解决给定问题的简单方法是生成给定数组的所有可能对,如果任何对的总和与其索引的总和相同,则计算该对。检查所有对后,打印获得的总计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
void countPairs(int arr[], int N)
{
// Stores the total count of pairs
int answer = 0;
// Iterate over the range
for (int i = 0; i < N; i++) {
// Iterate over the range
for (int j = i + 1; j < N; j++) {
if (arr[i] + arr[j] == i + j) {
answer++;
}
}
}
// Print the total count
cout << answer;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
countPairs(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
public static void countPairs(int arr[], int N)
{
// Stores the total count of pairs
int answer = 0;
// Iterate over the range
for(int i = 0; i < N; i++)
{
// Iterate over the range
for(int j = i + 1; j < N; j++)
{
if (arr[i] + arr[j] == i + j)
{
answer++;
}
}
}
// Print the total count
System.out.println(answer);
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = arr.length;
countPairs(arr, N);
}
}
// This code is contributed by gfgking
Python3
# Python3 program for the above approach
# Function to find all possible pairs
# of the given array such that the sum
# of arr[i] + arr[j] is i + j
def countPairs(arr, N):
# Stores the total count of pairs
answer = 0
# Iterate over the range
for i in range(N):
# Iterate over the range
for j in range(i + 1, N):
if arr[i] + arr[j] == i + j:
answer += 1
# Print the total count
print(answer)
# Driver code
arr = [ 0, 1, 2, 3, 4, 5 ]
N = len(arr)
countPairs(arr, N)
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
class GFG{
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
static void countPairs(int[] arr, int N)
{
// Stores the total count of pairs
int answer = 0;
// Iterate over the range
for(int i = 0; i < N; i++)
{
// Iterate over the range
for(int j = i + 1; j < N; j++)
{
if (arr[i] + arr[j] == i + j)
{
answer++;
}
}
}
// Print the total count
Console.Write(answer);
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 0, 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
// This code is contributed by target_2
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
void countPairs(int arr[], int N)
{
// Stores the total count of pairs
int answer = 0;
unordered_map mp;
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
mp[keyValue]++;
}
// Iterate over the range [0, N]
for (auto i : mp) {
int size = i.second;
answer += (size * (size - 1)) / 2;
}
// Print the answer
cout << answer;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
countPairs(arr, N);
return 0;
}
Java
/*package whatever //do not write package name here */
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
public static void countPairs(int[] arr, int n)
{
// Stores the total count of pairs
int answer = 0;
HashMap mp
= new HashMap();
// Iterate over the range [0, N]
for (int i = 0; i < n; i++) {
int value = arr[i] - i;
if (mp.containsKey(value)) {
mp.put(value, mp.get(value) + 1);
}
else {
mp.put(value, 1);
}
}
// Iterate over the range [0, N]
for (Map.Entry map :
mp.entrySet()) {
int temp = map.getValue();
answer += temp * (temp - 1) / 2;
}
// Print the answer
System.out.println(answer);
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 0, 1, 2, 3, 4, 5 };
int n = 6;
countPairs(arr, n);
}
}
// This code is contributed by maddler.
Python3
# Python3 program for the above approach
# Function to find all possible pairs
# of the given array such that the sum
# of arr[i] + arr[j] is i + j
def countPairs(arr, N):
# Stores the total count of pairs
answer = 0
mp = {}
# Iterate over the range [0, N]
for i in range(N):
keyValue = arr[i] - i
if keyValue in mp.keys():
mp[keyValue] += 1
else:
mp[keyValue] = 1
# Iterate over the range [0, N]
for size in mp.values():
answer += (size * (size - 1)) // 2
print(answer)
# Driver code
arr = [ 0, 1, 2, 3, 4, 5 ]
N = len(arr)
countPairs(arr, N)
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
static void countPairs(int []arr, int N)
{
// Stores the total count of pairs
int answer = 0;
Dictionary mp = new Dictionary();
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
if(mp.ContainsKey(keyValue))
mp[keyValue]++;
else
mp.Add(keyValue,1);
}
// Iterate over the range [0, N]
foreach(KeyValuePair entry in mp)
{
int size = entry.Value;
answer += (size * (size - 1)) / 2;
}
// Print the answer
Console.Write(answer);
}
// Driver Code
public static void Main()
{
int []arr = {0, 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
15
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:上述方法也可以通过使用无序映射来优化数组arr[] 中具有(arr[i] – i)值的元素的数量。请按照以下步骤解决问题:
- 初始化变量,说answer为0以在数组arr[] 中存储对的计数。
- 初始化无序映射mp[]以存储数组arr[] 中具有值(arr[i] – i)的元素的频率。
- 使用变量i在范围[0, N] 上迭代并执行以下步骤:
- 将变量keyValue初始化为(arr[i] – i) 的值。
- 将无序映射mp[]中keyValue的值增加1 。
- 使用变量i迭代无序映射mp[]并执行以下步骤:
- 将变量大小初始化为i.second无序映射mp[] 的值。
- 将size*(size – 1)/2的值添加到变量answer 。
- 执行上述步骤后,打印答案的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
void countPairs(int arr[], int N)
{
// Stores the total count of pairs
int answer = 0;
unordered_map mp;
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
mp[keyValue]++;
}
// Iterate over the range [0, N]
for (auto i : mp) {
int size = i.second;
answer += (size * (size - 1)) / 2;
}
// Print the answer
cout << answer;
}
// Driver Code
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
countPairs(arr, N);
return 0;
}
Java
/*package whatever //do not write package name here */
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
public static void countPairs(int[] arr, int n)
{
// Stores the total count of pairs
int answer = 0;
HashMap mp
= new HashMap();
// Iterate over the range [0, N]
for (int i = 0; i < n; i++) {
int value = arr[i] - i;
if (mp.containsKey(value)) {
mp.put(value, mp.get(value) + 1);
}
else {
mp.put(value, 1);
}
}
// Iterate over the range [0, N]
for (Map.Entry map :
mp.entrySet()) {
int temp = map.getValue();
answer += temp * (temp - 1) / 2;
}
// Print the answer
System.out.println(answer);
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 0, 1, 2, 3, 4, 5 };
int n = 6;
countPairs(arr, n);
}
}
// This code is contributed by maddler.
蟒蛇3
# Python3 program for the above approach
# Function to find all possible pairs
# of the given array such that the sum
# of arr[i] + arr[j] is i + j
def countPairs(arr, N):
# Stores the total count of pairs
answer = 0
mp = {}
# Iterate over the range [0, N]
for i in range(N):
keyValue = arr[i] - i
if keyValue in mp.keys():
mp[keyValue] += 1
else:
mp[keyValue] = 1
# Iterate over the range [0, N]
for size in mp.values():
answer += (size * (size - 1)) // 2
print(answer)
# Driver code
arr = [ 0, 1, 2, 3, 4, 5 ]
N = len(arr)
countPairs(arr, N)
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find all possible pairs
// of the given array such that the sum
// of arr[i] + arr[j] is i + j
static void countPairs(int []arr, int N)
{
// Stores the total count of pairs
int answer = 0;
Dictionary mp = new Dictionary();
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
if(mp.ContainsKey(keyValue))
mp[keyValue]++;
else
mp.Add(keyValue,1);
}
// Iterate over the range [0, N]
foreach(KeyValuePair entry in mp)
{
int size = entry.Value;
answer += (size * (size - 1)) / 2;
}
// Print the answer
Console.Write(answer);
}
// Driver Code
public static void Main()
{
int []arr = {0, 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
15
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。