📌  相关文章
📜  计算对(i,j)的对数,以使arr [i] * arr [j]> arr [i] + arr [j]

📅  最后修改于: 2021-04-29 16:47:26             🧑  作者: Mango

给定非负整数的数组arr [] ,任务是对索引对(i,j进行计数,以使arr [i] * arr [j]> arr [i] + arr [j]其中i

例子:

天真的方法:运行两个嵌套循环,并检查每一对是否满足条件。如果任何一对都满足条件,则更新count = count + 1并在最后打印计数

下面是上述方法的实现:

C++
// 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
// 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++) {
  
            // If condition is satisfied
            if (arr[i] * arr[j] > arr[i] + arr[j])
                count++;
        }
    }
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 0, 3, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countPairs(arr, n);
    return 0;
}


Java
// Java program to count pairs (i, j)
// such that arr[i] * arr[j] > arr[i] + arr[j]
import java.util.*;
  
class solution
{
  
// Function to return the count of pairs
// 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++) {
  
            // If condition is satisfied
            if (arr[i] * arr[j] > arr[i] + arr[j])
                count++;
        }
    }
    return count;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 5, 0, 3, 1, 2 };
    int n = arr.length;
    System.out.println(countPairs(arr, n));
      
}
}
  
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 program to count pairs(i,j)
# such that arr[i]*arr[j]>arr[i]+arr[j]
import math as mt
  
# function to return the count of pairs 
# such that arr[i]*arr[j]>arr[i]+arr[j]
def countPairs(arr, n):
    count = 0
      
    for i in range(n):
        for j in range(i + 1, n):
              
            # if condition is satisified
            if arr[i] * arr[j] > arr[i] + arr[j]:
                count += 1
              
    return count
  
# Driver code
arr = [5, 0, 3, 1, 2]
n = len(arr)
  
print(countPairs(arr, n))
          
# This code is contributed 
# by Mohit Kumar 29


C#
// C# program to count pairs (i, j)
// such that arr[i] * arr[j] > arr[i] + arr[j]
  
using System;
  
public class GFG{
      
// Function to return the count of pairs
// 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++) {
  
            // If condition is satisfied
            if (arr[i] * arr[j] > arr[i] + arr[j])
                count++;
        }
    }
    return count;
}
  
// Driver code
    static public void Main (){
    int []arr = { 5, 0, 3, 1, 2 };
    int n = arr.Length;
    Console.WriteLine (countPairs(arr, n));
    }
}


PHP
 arr[i] + arr[j] 
  
// Function to return the count of pairs 
// such that arr[i] * arr[j] > arr[i] + arr[j] 
function countPairs($arr, $n) 
{ 
    $count = 0; 
    for ($i = 0; $i < $n - 1; $i++) 
    { 
        for ($j = $i + 1; $j < $n; $j++) 
        { 
  
            // If condition is satisfied 
            if ($arr[$i] * 
                $arr[$j] > $arr[$i] + 
                           $arr[$j]) 
                $count++; 
        } 
    } 
    return $count; 
} 
  
// Driver code 
$arr = array( 5, 0, 3, 1, 2 ); 
$n = sizeof($arr) ;
  
echo countPairs($arr, $n);
  
// This code is contributed by Ryuga
?>


C++
// 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
// such that arr[i] * arr[j] > arr[i] + arr[j]
long countPairs(const int* arr, int n)
{
    int count_2 = 0, count_others = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == 2)
            count_2++;
        else if (arr[i] > 2)
            count_others++;
    }
    long ans
        = 1L * count_2 * count_others
          + (1L * count_others * (count_others - 1)) / 2;
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 0, 3, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    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 
    // such that arr[i] * arr[j] > arr[i] + arr[j] 
    static long countPairs(int[] arr, int n) 
    {
        int count_2 = 0, count_others = 0;
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 2) 
            {
                count_2++;
            } 
            else if (arr[i] > 2) 
            {
                count_others++;
            }
        }
          
        long ans = 1L * count_2 * count_others +
                (1L * count_others * (count_others - 1)) / 2;
        return ans;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int arr[] = {5, 0, 3, 1, 2};
        int n = arr.length;
        System.out.println(countPairs(arr, n));
    }
}
  
// This code is contributed by 
// 29AjayKumar


Python3
# Python3 program to count pairs(i,j)
# such that arr[i]*arr[j]>arr[i]+arr[j]
import math as mt
  
# function to return the count of pairs 
# such that arr[i]*arr[j]>arr[i]+arr[j]
def countPairs(arr, n):
    count_2, count_others = 0, 0
      
    for i in range(n):
        if arr[i] == 2:
            count_2 += 1
        elif arr[i] > 2:
            count_others += 1
    ans = (count_2 * count_others +
          (count_others * 
          (count_others - 1)) // 2)
    return ans
  
# Driver code
arr = [5, 0, 3, 1, 2]
n = len(arr)
  
print(countPairs(arr, n))
          
# This code is contributed 
# by Mohit Kumar


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 
    // such that arr[i] * arr[j] > arr[i] + arr[j] 
    static long countPairs(int[] arr, int n) 
    {
        int count_2 = 0, count_others = 0;
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 2) 
            {
                count_2++;
            } 
            else if (arr[i] > 2) 
            {
                count_others++;
            }
        }
          
        long ans = 1L * count_2 * count_others +
                  (1L * count_others * 
                       (count_others - 1)) / 2;
        return ans;
    }
  
    // Driver code 
    public static void Main() 
    {
        int[] arr = {5, 0, 3, 1, 2};
        int n = arr.Length;
        Console.WriteLine(countPairs(arr, n));
    }
}
  
// This code is contributed by 
// Mukul Singh


PHP
 arr[i] + arr[j]
  
// Function to return the count of pairs
// such that arr[i] * arr[j] > arr[i] + arr[j]
function countPairs($arr, $n)
{
    $count_2 = 0; $count_others = 0;
    for ($i = 0; $i < $n; $i++) 
    {
        if ($arr[$i] == 2)
            $count_2++;
        else if ($arr[$i] > 2)
            $count_others++;
    }
    $ans = $count_2 * $count_others + 
                     ($count_others * 
                     ($count_others - 1)) / 2;
    return $ans;
}
  
// Driver code
$arr = array( 5, 0, 3, 1, 2 );
$n = sizeof($arr);
echo countPairs($arr, $n);
  
// This code is contributed
// by Akanksha Rai
?>


输出:
3

高效方法:考虑以下情况:

下面是上述方法的实现:

C++

// 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
// such that arr[i] * arr[j] > arr[i] + arr[j]
long countPairs(const int* arr, int n)
{
    int count_2 = 0, count_others = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == 2)
            count_2++;
        else if (arr[i] > 2)
            count_others++;
    }
    long ans
        = 1L * count_2 * count_others
          + (1L * count_others * (count_others - 1)) / 2;
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 5, 0, 3, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    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 
    // such that arr[i] * arr[j] > arr[i] + arr[j] 
    static long countPairs(int[] arr, int n) 
    {
        int count_2 = 0, count_others = 0;
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 2) 
            {
                count_2++;
            } 
            else if (arr[i] > 2) 
            {
                count_others++;
            }
        }
          
        long ans = 1L * count_2 * count_others +
                (1L * count_others * (count_others - 1)) / 2;
        return ans;
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        int arr[] = {5, 0, 3, 1, 2};
        int n = arr.length;
        System.out.println(countPairs(arr, n));
    }
}
  
// This code is contributed by 
// 29AjayKumar 

Python3

# Python3 program to count pairs(i,j)
# such that arr[i]*arr[j]>arr[i]+arr[j]
import math as mt
  
# function to return the count of pairs 
# such that arr[i]*arr[j]>arr[i]+arr[j]
def countPairs(arr, n):
    count_2, count_others = 0, 0
      
    for i in range(n):
        if arr[i] == 2:
            count_2 += 1
        elif arr[i] > 2:
            count_others += 1
    ans = (count_2 * count_others +
          (count_others * 
          (count_others - 1)) // 2)
    return ans
  
# Driver code
arr = [5, 0, 3, 1, 2]
n = len(arr)
  
print(countPairs(arr, n))
          
# This code is contributed 
# by Mohit Kumar

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 
    // such that arr[i] * arr[j] > arr[i] + arr[j] 
    static long countPairs(int[] arr, int n) 
    {
        int count_2 = 0, count_others = 0;
        for (int i = 0; i < n; i++) 
        {
            if (arr[i] == 2) 
            {
                count_2++;
            } 
            else if (arr[i] > 2) 
            {
                count_others++;
            }
        }
          
        long ans = 1L * count_2 * count_others +
                  (1L * count_others * 
                       (count_others - 1)) / 2;
        return ans;
    }
  
    // Driver code 
    public static void Main() 
    {
        int[] arr = {5, 0, 3, 1, 2};
        int n = arr.Length;
        Console.WriteLine(countPairs(arr, n));
    }
}
  
// This code is contributed by 
// Mukul Singh

的PHP

 arr[i] + arr[j]
  
// Function to return the count of pairs
// such that arr[i] * arr[j] > arr[i] + arr[j]
function countPairs($arr, $n)
{
    $count_2 = 0; $count_others = 0;
    for ($i = 0; $i < $n; $i++) 
    {
        if ($arr[$i] == 2)
            $count_2++;
        else if ($arr[$i] > 2)
            $count_others++;
    }
    $ans = $count_2 * $count_others + 
                     ($count_others * 
                     ($count_others - 1)) / 2;
    return $ans;
}
  
// Driver code
$arr = array( 5, 0, 3, 1, 2 );
$n = sizeof($arr);
echo countPairs($arr, $n);
  
// This code is contributed
// by Akanksha Rai
?>
输出:
3