给定长度为N的数组arr [],计算对数(i,j),以使arr [i] * arr [j] = arr [i] + arr [j]和0 <= i
例子:
Input : arr[] = {2, 0, 3, 2, 0}
Output : 2
Input : arr[] = {1, 2, 3, 4}
Output : 0
简单的解决方案:
我们可以生成数组的所有可能的对,并对满足给定条件的那些对进行计数。
下面是上述方法的实现:
CPP
// C++ program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
#include
using namespace std;
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
long count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Increment count if condition satisfy
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
// Return count of pairs
return count;
}
// Driver code
int main()
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
// Get and print count of pairs
cout << countPairs(arr, n);
return 0;
}
Java
// Java program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int arr[], int n)
{
long count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Increment count if condition satisfy
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
// Return count of pairs
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = arr.length;
// Get and print count of pairs
System.out.println(countPairs(arr, n));
}
}
Python3
# Python3 program to count pairs (i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
def countPairs(arr, n) :
count = 0;
for i in range(n - 1) :
for j in range(i + 1, n) :
# Increment count if condition satisfy
if (arr[i] * arr[j] == arr[i] + arr[j]) :
count += 1;
# Return count of pairs
return count;
# Driver code
if __name__ == "__main__" :
arr = [ 2, 0, 3, 2, 0 ];
n = len(arr);
# Get and print count of pairs
print(countPairs(arr, n));
# This code is contributed by AnkitRai01
C#
// C# program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
using System;
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int[] arr, int n)
{
long count = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Increment count if condition satisfy
if (arr[i] * arr[j] == arr[i] + arr[j])
count++;
}
}
// Return count of pairs
return count;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 2, 0, 3, 2, 0 };
int n = arr.Length;
// Get and print count of pairs
Console.WriteLine(countPairs(arr, n));
}
}
CPP
// C++ program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
#include
using namespace std;
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
int main()
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
// Get and print count of pairs
cout << countPairs(arr, n);
return 0;
}
Java
// Java program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int arr[], int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = arr.length;
// Get and print count of pairs
System.out.println(countPairs(arr, n));
}
}
Python3
# Python3 program to count pairs (i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
def countPairs(arr, n):
countZero = 0;
countTwo = 0;
# Count number of 0's and 2's in the array
for i in range(n) :
if (arr[i] == 0) :
countZero += 1;
elif (arr[i] == 2) :
countTwo += 1;
# Total pairs due to occurrence of 0's
pair0 = (countZero * (countZero - 1)) // 2;
# Total pairs due to occurrence of 2's
pair2 = (countTwo * (countTwo - 1)) // 2;
# Return count of all pairs
return pair0 + pair2;
# Driver code
if __name__ == "__main__" :
arr = [ 2, 0, 3, 2, 0 ];
n = len(arr);
# Get and print count of pairs
print(countPairs(arr, n));
# This code is contributed by AnkitRai01
C#
// C# program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
using System;
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int[] arr, int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 2, 0, 3, 2, 0 };
int n = arr.Length;
// Get and print count of pairs
Console.WriteLine(countPairs(arr, n));
}
}
输出:
2
时间复杂度: O(n 2 )
高效的解决方案:
以arr [i]为x且arr [j]为y,我们可以将给定条件重写为以下方程式。
xy = x + y
xy - x - y = 0
xy - x - y + 1 = 1
x(y - 1) -(y - 1) = 1
(x - 1)(y - 1) = 1
Case 1:
x - 1 = 1 i.e x = 2
y - 1 = 1 i.e y = 2
Case 2:
x - 1 = -1 i.e x = 0
y - 1 = -1 i.e y = 0
因此,现在我们知道,只有当arr [i] = arr [j] = 0或arr [i] = arr时,条件arr [i] * arr [j] = arr [i] + arr [j]才能满足[j] = 2 。
我们需要做的只是计算2和0的出现次数。然后我们可以使用公式获得对数
(count * (count - 1)) / 2
下面是上述方法的实现:
CPP
// C++ program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
#include
using namespace std;
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
long countPairs(int arr[], int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
int main()
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
// Get and print count of pairs
cout << countPairs(arr, n);
return 0;
}
Java
// Java program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int arr[], int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 0, 3, 2, 0 };
int n = arr.length;
// Get and print count of pairs
System.out.println(countPairs(arr, n));
}
}
Python3
# Python3 program to count pairs (i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
# Function to return the count of pairs(i, j)
# such that arr[i] * arr[j] = arr[i] + arr[j]
def countPairs(arr, n):
countZero = 0;
countTwo = 0;
# Count number of 0's and 2's in the array
for i in range(n) :
if (arr[i] == 0) :
countZero += 1;
elif (arr[i] == 2) :
countTwo += 1;
# Total pairs due to occurrence of 0's
pair0 = (countZero * (countZero - 1)) // 2;
# Total pairs due to occurrence of 2's
pair2 = (countTwo * (countTwo - 1)) // 2;
# Return count of all pairs
return pair0 + pair2;
# Driver code
if __name__ == "__main__" :
arr = [ 2, 0, 3, 2, 0 ];
n = len(arr);
# Get and print count of pairs
print(countPairs(arr, n));
# This code is contributed by AnkitRai01
C#
// C# program to count pairs (i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
using System;
class GFG {
// Function to return the count of pairs(i, j)
// such that arr[i] * arr[j] = arr[i] + arr[j]
static long countPairs(int[] arr, int n)
{
int countZero = 0;
int countTwo = 0;
// Count number of 0's and 2's in the array
for (int i = 0; i < n; i++) {
if (arr[i] == 0)
countZero++;
else if (arr[i] == 2)
countTwo++;
}
// Total pairs due to occurrence of 0's
long pair0 = (countZero * (countZero - 1)) / 2;
// Total pairs due to occurrence of 2's
long pair2 = (countTwo * (countTwo - 1)) / 2;
// Return count of all pairs
return pair0 + pair2;
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 2, 0, 3, 2, 0 };
int n = arr.Length;
// Get and print count of pairs
Console.WriteLine(countPairs(arr, n));
}
}
输出:
2
时间复杂度: O(n)