数组中的对数,使得乘积大于总和
给定一个非负整数数组 a[]。计算数组中对 (i, j) 的数量,使得 a[i] + a[j] < a[i]*a[j]。 (这对 (i, j) 和 (j, i) 被认为是相同的,并且 i 不应该等于 j)
例子:
Input : a[] = {3, 4, 5}
Output : 3
Pairs are (3, 4) , (4, 5) and (3,5)
Input : a[] = {1, 1, 1}
Output : 0
天真的方法
对于每个值 a[i] 计算 a[j] (i > j) 的数量,使得 a[i]*a[j] > a[i] + a[j]
C++
// Naive C++ program to count number of pairs
// such that their sum is more than product.
#include
using namespace std;
// Returns the number of valid pairs
int countPairs (int arr[], int n)
{
int ans = 0; // initializing answer
// Traversing the array. For each array
// element, checking its predecessors that
// follow the condition
for (int i = 0; i= 0; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
// Driver function
int main()
{
int arr[] = {3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout << countPairs(arr, n);
return 0;
}
Java
// Naive java program to count number of pairs
// such that their sum is more than product.
import java.*;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int arr[], int n)
{
int ans = 0; // initializing answer
// Traversing the array. For each array
// element, checking its predecessors that
// follow the condition
for (int i = 0; i= 0; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = {3, 4, 5};
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
// This code is contributed by Sam007
Python3
# Naive Python program to count number
# of pairs such that their sum is more
# than product.
# Returns the number of valid pairs
def countPairs(arr, n):
# initializing answer
ans = 0
# Traversing the array. For each
# array element, checking its
# predecessors that follow the
# condition
for i in range(0, n):
j = i-1
while(j >= 0):
if (arr[i] * arr[j] >
arr[i] + arr[j]):
ans = ans + 1
j = j - 1
return ans
# Driver program to test above function.
arr = [3, 4, 5]
n = len(arr)
k = countPairs(arr, n)
print(k)
# This code is contributed by Sam007.
C#
// Naive C# program to count number of pairs
// such that their sum is more than product.
using System;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int []arr, int n)
{
int ans = 0; // initializing answer
// Traversing the array. For each array
// element, checking its predecessors that
// follow the condition
for (int i = 0; i= 0; j--)
if (arr[i]*arr[j] > arr[i] + arr[j])
ans++;
return ans;
}
// driver program
public static void Main()
{
int []arr = {3, 4, 5};
int n = arr.Length;
Console.Write( countPairs(arr, n));
}
}
// This code is contributed by Sam007
PHP
= 0; $j--)
if ($arr[$i] * $arr[$j] >
$arr[$i] + $arr[$j])
$ans++;
return $ans;
}
// Driver Code
$arr = array(3, 4, 5);
$n = sizeof($arr);
echo(countPairs($arr, $n));
// This code is contributed by Ajit.
?>
Javascript
C++
// C++ implementation of efficient approach
// to count valid pairs.
#include
using namespace std;
// returns the number of valid pairs
int CountPairs (int arr[], int n)
{
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// Driver function
int main()
{
int arr[] = {3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout << CountPairs(arr, n);
return 0;
}
Java
// Java implementation of efficient approach
// to count valid pairs.
import java.*;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int arr[], int n)
{
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// Driver code
public static void main(String args[])
{
int arr[] = {3, 4, 5};
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
// This code is contributed by Sam007
Python3
# python implementation of efficient approach
# to count valid pairs.
# returns the number of valid pairs
def CountPairs (arr,n):
# traversing the array, counting the
# number of 2s and greater than 2
# in array
twoCount = 0
twoGrCount = 0
for i in range(0, n):
if (arr[i] == 2):
twoCount += 1
else if (arr[i] > 2):
twoGrCount += 1
return ((twoCount * twoGrCount)
+ (twoGrCount * (twoGrCount - 1)) / 2)
# Driver function
arr = [3, 4, 5]
n = len(arr)
print( CountPairs(arr, n))
# This code is contributed by Sam007.
C#
// C# implementation of efficient approach
// to count valid pairs.
using System;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int []arr, int n) {
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// driver program
public static void Main()
{
int []arr = {3, 4, 5};
int n = arr.Length;
Console.Write( countPairs(arr, n));
}
}
// This code is contributed by Sam007
PHP
2)
$twoGrCount++;
}
return $twoCount * $twoGrCount +
($twoGrCount * ($twoGrCount -
1)) / 2;
}
// Driver Code
$arr = array(3, 4, 5);
$n = sizeof($arr);
echo(CountPairs($arr, $n));
// This code is contributed by Ajit.
?>
Javascript
输出:
3
高效的方法
当 a[i] = 0 : a[i]*a[j] = 0 且 a[i] + a[j] >= 0 所以如果 a[i] = 0 则找不到对。
当 a[i] = 1 : a[i]*a[j] = a[j] and a[i] + a[j] = 1 + a[j], 所以当 a[i ] = 1
当 a[i] = 2 且 a[j] = 2 时: a[i]*a[j] = a[i] + a[j] = 4
当 a[i] = 2 且 a[j] > 2 或 a[i] > 2 且 a[j] >= 2 时:所有此类对都是有效的。
要解决这个问题,请计算数组中 2 的数量,例如 twoCount。计算数组中大于 2 的数字,比如 twoGreaterCount。答案是 twoCount * twoGreaterCount + twoGreaterCount * (twoGreaterCount-1)/2
C++
// C++ implementation of efficient approach
// to count valid pairs.
#include
using namespace std;
// returns the number of valid pairs
int CountPairs (int arr[], int n)
{
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// Driver function
int main()
{
int arr[] = {3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout << CountPairs(arr, n);
return 0;
}
Java
// Java implementation of efficient approach
// to count valid pairs.
import java.*;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int arr[], int n)
{
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// Driver code
public static void main(String args[])
{
int arr[] = {3, 4, 5};
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
// This code is contributed by Sam007
Python3
# python implementation of efficient approach
# to count valid pairs.
# returns the number of valid pairs
def CountPairs (arr,n):
# traversing the array, counting the
# number of 2s and greater than 2
# in array
twoCount = 0
twoGrCount = 0
for i in range(0, n):
if (arr[i] == 2):
twoCount += 1
else if (arr[i] > 2):
twoGrCount += 1
return ((twoCount * twoGrCount)
+ (twoGrCount * (twoGrCount - 1)) / 2)
# Driver function
arr = [3, 4, 5]
n = len(arr)
print( CountPairs(arr, n))
# This code is contributed by Sam007.
C#
// C# implementation of efficient approach
// to count valid pairs.
using System;
public class GFG
{
// Returns the number of valid pairs
static int countPairs (int []arr, int n) {
// traversing the array, counting the
// number of 2s and greater than 2
// in array
int twoCount = 0, twoGrCount = 0;
for (int i = 0; i 2)
twoGrCount++;
}
return twoCount*twoGrCount +
(twoGrCount*(twoGrCount-1))/2;
}
// driver program
public static void Main()
{
int []arr = {3, 4, 5};
int n = arr.Length;
Console.Write( countPairs(arr, n));
}
}
// This code is contributed by Sam007
PHP
2)
$twoGrCount++;
}
return $twoCount * $twoGrCount +
($twoGrCount * ($twoGrCount -
1)) / 2;
}
// Driver Code
$arr = array(3, 4, 5);
$n = sizeof($arr);
echo(CountPairs($arr, $n));
// This code is contributed by Ajit.
?>
Javascript
输出:
3
时间复杂度: O(n)
辅助空间: O(1)