索引对的计数,使得这些索引处的元素的乘积等于索引的绝对差
给定一个由N个正整数组成的数组arr[] ,任务是找到对(i, j)的数量,使得i < j并且这些索引处的元素的乘积等于它们索引的绝对差。
例子:
Input: arr[] = {1, 1, 2, 4}
Output: 2
Explanation:
Following are the possible pairs:
- (0, 1): The sum of these indices is 0 + 1 = 1 and the product of elements at these indices is arr[0]*arr[1] = 1*1 = 1.
- (0, 2): The sum of these indices is 0 + 2 = 2 and the product of elements at these indices is arr[0]*arr[1] = 1*2 = 2.
Therefore, the total count of pairs is 2.
Input: arr[] = {1, 2, 1}
Output: 0
朴素方法:解决给定问题的简单方法是生成给定数组的所有可能对并计算满足给定标准的那些对。检查所有对后,打印获得的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
int getPairsCount(int a[], int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Generate all possible pairs
// from the array arr[]
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If the given condition
// satisfy then increment
// the value of count
if ((a[i] * a[j]) == abs(i - j))
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
int main()
{
int arr[] = { 1, 1, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int a[], int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Generate all possible pairs
// from the array arr[]
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
// If the given condition
// satisfy then increment
// the value of count
if ((a[i] * a[j]) == Math.abs(i - j))
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 1, 1, 2, 4 };
int N = arr.length;
System.out.print(getPairsCount(arr, N));
}
}
// This code is contributed by avijitmondal1998
Python3
# Python3 program for the above approach
# Function to count the number of
# pairs (i, j) such that arr[i]*arr[j]
# is equal to abs(i - j)
def getPairsCount(a, n):
# Stores the resultant number
# of pairs
count = 0
# Generate all possible pairs
# from the array arr[]
for i in range(n - 1):
for j in range(i + 1, n):
# If the given condition
# satisfy then increment
# the value of count
if ((a[i] * a[j]) == abs(i - j)):
count += 1
# Return the resultant count
return count
# Driver Code
if __name__ == '__main__':
arr = [ 1, 1, 2, 4 ]
N = len(arr)
print(getPairsCount(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int[] a, int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Generate all possible pairs
// from the array arr[]
for(int i = 0; i < n - 1; i++)
{
for(int j = i + 1; j < n; j++)
{
// If the given condition
// satisfy then increment
// the value of count
if ((a[i] * a[j]) == Math.Abs(i - j))
count++;
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 1, 2, 4 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
// This code is contributed by subhammahato348
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
int getPairsCount(int arr[], int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for (int i = 0; i < n; i++) {
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for (int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) == abs(i - j)) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
int main()
{
int arr[] = { 1, 1, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int []arr, int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for (int i = 0; i < n; i++) {
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for (int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) == Math.abs(i - j)) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 1, 1, 2, 4 };
int N = arr.length;
System.out.print(getPairsCount(arr, N));
}
}
// This code isontributed by SURENDRA_GANGWAR.
Python3
# Python3 program for the above approach
# Function to count the number of
# pairs(i, j) such that arr[i]*arr[j]
# is equal to abs(i - j)
def getPairsCount(arr, n):
# Stores the resultant number
# of pairs
count = 0
# Iterate over the range[0, N)
for i in range(0, n):
# Now, iterate from the value
# arr[i]-(i % arr[i]) till N
# with an increment of arr[i]
s = arr[i]-(i % arr[i])
for j in range(s, n):
# If the given criteria
# satisfy then increment
# the value of count
if (i < j and (arr[i] * arr[j]) ==
abs(i - j)):
count += 1
# Return the resultant count
return count
# Driver Code
arr = [ 1, 1, 2, 4 ]
N = len(arr)
print(getPairsCount(arr, N))
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int[] arr, int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for(int i = 0; i < n; i++)
{
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for(int j = arr[i] - (i % arr[i]);
j < n; j += arr[i])
{
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) ==
Math.Abs(i - j))
{
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 1, 2, 4 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
// This code is contributed by ukasp
Javascript
输出:
2
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:上述方法也可以通过优化上述步骤中使用的内循环来进行优化。这个想法是在第一个循环中迭代范围[0, N – 1] ,并在第二个循环中使用变量j从arr[i] – (i%arr[i])迭代,并将j的值增加arr[i]直到N然后检查给定的标准。请按照以下步骤解决问题:
- 初始化变量,比如count为0 ,存储对的结果计数。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 使用变量j迭代范围[arr[i] – (i%arr[i]), N] ,增量为arr[i]并且如果i小于j并且arr[i]*arr[j]等于abs(i – j) ,然后将count的值增加1 。
- 完成上述步骤后,打印count的值作为结果。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
int getPairsCount(int arr[], int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for (int i = 0; i < n; i++) {
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for (int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) == abs(i - j)) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
int main()
{
int arr[] = { 1, 1, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << getPairsCount(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int []arr, int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for (int i = 0; i < n; i++) {
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for (int j = arr[i] - (i % arr[i]);
j < n;
j += arr[i]) {
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) == Math.abs(i - j)) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void main(String args[])
{
int []arr = { 1, 1, 2, 4 };
int N = arr.length;
System.out.print(getPairsCount(arr, N));
}
}
// This code isontributed by SURENDRA_GANGWAR.
Python3
# Python3 program for the above approach
# Function to count the number of
# pairs(i, j) such that arr[i]*arr[j]
# is equal to abs(i - j)
def getPairsCount(arr, n):
# Stores the resultant number
# of pairs
count = 0
# Iterate over the range[0, N)
for i in range(0, n):
# Now, iterate from the value
# arr[i]-(i % arr[i]) till N
# with an increment of arr[i]
s = arr[i]-(i % arr[i])
for j in range(s, n):
# If the given criteria
# satisfy then increment
# the value of count
if (i < j and (arr[i] * arr[j]) ==
abs(i - j)):
count += 1
# Return the resultant count
return count
# Driver Code
arr = [ 1, 1, 2, 4 ]
N = len(arr)
print(getPairsCount(arr, N))
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of
// pairs (i, j) such that arr[i]*arr[j]
// is equal to abs(i - j)
static int getPairsCount(int[] arr, int n)
{
// Stores the resultant number
// of pairs
int count = 0;
// Iterate over the range [0, N)
for(int i = 0; i < n; i++)
{
// Now, iterate from the value
// arr[i]-(i%arr[i]) till N
// with an increment of arr[i]
for(int j = arr[i] - (i % arr[i]);
j < n; j += arr[i])
{
// If the given criteria
// satisfy then increment
// the value of count
if (i < j && (arr[i] * arr[j]) ==
Math.Abs(i - j))
{
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 1, 2, 4 };
int N = arr.Length;
Console.Write(getPairsCount(arr, N));
}
}
// This code is contributed by ukasp
Javascript
输出:
2
时间复杂度: O(N*log N)
辅助空间: O(1)