📌  相关文章
📜  计算作为数组总和因子的对总和

📅  最后修改于: 2022-05-13 01:56:08.812000             🧑  作者: Mango

计算作为数组总和因子的对总和

给定一个由N个整数组成的数组arr[] ,任务是找到对的数量,其中i ≤ j ,使得对的总和除以数组元素的总和。

例子:

方法:给定的问题可以通过从给定的数组生成所有可能的对(arr[i], arr[j])来解决,使得i ≤ j并计算所有元素总和除以数组总和的对。检查所有可能的对后,打印获得的总计数。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of pairs
// whose sums divides the sum of array
void countPairs(int arr[], int N)
{
    // Initialize the totalSum and
    // count as 0
    int count = 0, totalSum = 0;
 
    // Calculate the total sum of array
    for (int i = 0; i < N; i++) {
        totalSum += arr[i];
    }
 
    // Generate all possible pairs
    for (int i = 0; i < N; i++) {
 
        for (int j = i + 1; j < N; j++) {
 
            // If the sum is a factor
            // of totalSum or not
            if (totalSum
                    % (arr[i] + arr[j])
                == 0) {
 
                // Increment count by 1
                count += 1;
            }
        }
    }
 
    // Print the total count obtained
    cout << count;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countPairs(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find the number of pairs
    // whose sums divides the sum of array
    public static void countPairs(int arr[], int N)
    {
       
        // Initialize the totalSum and
        // count as 0
        int count = 0, totalSum = 0;
 
        // Calculate the total sum of array
        for (int i = 0; i < N; i++)
        {
            totalSum += arr[i];
        }
 
        // Generate all possible pairs
        for (int i = 0; i < N; i++) {
 
            for (int j = i + 1; j < N; j++) {
 
                // If the sum is a factor
                // of totalSum or not
                if (totalSum % (arr[i] + arr[j]) == 0) {
 
                    // Increment count by 1
                    count += 1;
                }
            }
        }
 
        // Print the total count obtained
        System.out.println(count);
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int N = arr.length;
        countPairs(arr, N);
      
    }
}
 
 // This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find the number of pairs
# whose sums divides the sum of array
def countPairs(arr, N):
     
    # Initialize the totalSum and
    # count as 0
    count = 0
    totalSum = 0
 
    # Calculate the total sum of array
    for i in range(N):
        totalSum += arr[i]
 
    # Generate all possible pairs
    for i in range(N):
        for j in range(i + 1, N, 1):
             
            # If the sum is a factor
            # of totalSum or not
            if (totalSum % (arr[i] + arr[j]) == 0):
                 
                # Increment count by 1
                count += 1
 
    # Print the total count obtained
    print(count)
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 5 ]
    N = len(arr)
     
    countPairs(arr, N)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
     
    // Function to find the number of pairs
    // whose sums divides the sum of array
    public static void countPairs(int[] arr, int N)
    {
       
        // Initialize the totalSum and
        // count as 0
        int count = 0, totalSum = 0;
 
        // Calculate the total sum of array
        for (int i = 0; i < N; i++)
        {
            totalSum += arr[i];
        }
 
        // Generate all possible pairs
        for (int i = 0; i < N; i++) {
 
            for (int j = i + 1; j < N; j++) {
 
                // If the sum is a factor
                // of totalSum or not
                if (totalSum % (arr[i] + arr[j]) == 0) {
 
                    // Increment count by 1
                    count += 1;
                }
            }
        }
 
        // Print the total count obtained
        Console.WriteLine(count);
    }
 
// Driver code
static public void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
        int N = arr.Length;
        countPairs(arr, N);
}
}
 
// This code is contributed by splevel62.


Javascript


输出:
3

时间复杂度: O(N 2 )
辅助空间: O(1)