Skip to content
Related Articles

Related Articles

Count pair sums that are factors of the sum of the array

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2021
Improve Article
Save Article

Given an array arr[] consisting of N integers, the task is to find the number of pairs, where i ≤ j, such that the sum of pairs divides the sum of array elements.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 3
Explanation:
Below are the pairs that divides the sum of array( = 1 + 2 + 3 + 4 + 5 = 15):

  1. (1, 2): Sum of pairs = 1 + 2 = 3, that divides sum(= 15).
  2. (1, 4): Sum of pairs = 1 + 4 = 5, that divides sum(= 15).
  3. (2, 3): Sum of pairs = 2 + 3 = 5, that divides sum(= 15).

Therefore, the count of pairs is 3.

Input: arr[] = {1, 5, 2}
Output: 0

Approach: The given problem can be solved by generating all possible pairs (arr[i], arr[j]) from the given array such that i ≤  j and count all those pairs whose sum of elements divides the sum of the array. After checking for all possible pairs, print the total count obtained.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
 
#include <iostream>
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




<script>
        // JavaScript program for the above approach
 
        // Function to find the number of pairs
        // whose sums divides the sum of array
        function countPairs(arr, N)
        {
         
            // Initialize the totalSum and
            // count as 0
            let count = 0, totalSum = 0;
 
            // Calculate the total sum of array
            for (let i = 0; i < N; i++) {
                totalSum += arr[i];
            }
 
            // Generate all possible pairs
            for (let i = 0; i < N; i++)
            {
                for (let 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
            document.write(count);
        }
 
        // Driver Code
        let arr = [1, 2, 3, 4, 5];
        let N = arr.length;
        countPairs(arr, N);
 
// This code is contributed by Potta Lokesh
    </script>


Output: 

3

 

Time Complexity: O(N2)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!