Related Articles

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

• Difficulty Level : Basic
• Last Updated : 30 Jun, 2021

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 ``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);``    ``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

 ``
Output:
`3`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up