Count pair sums that are factors of the sum of the array
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, 2): Sum of pairs = 1 + 2 = 3, that divides sum(= 15).
- (1, 4): Sum of pairs = 1 + 4 = 5, that divides sum(= 15).
- (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++
#include <iostream>
using namespace std;
void countPairs( int arr[], int N)
{
int count = 0, totalSum = 0;
for ( int i = 0; i < N; i++) {
totalSum += arr[i];
}
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if (totalSum
% (arr[i] + arr[j])
== 0) {
count += 1;
}
}
}
cout << count;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void countPairs( int arr[], int N)
{
int count = 0 , totalSum = 0 ;
for ( int i = 0 ; i < N; i++)
{
totalSum += arr[i];
}
for ( int i = 0 ; i < N; i++) {
for ( int j = i + 1 ; j < N; j++) {
if (totalSum % (arr[i] + arr[j]) == 0 ) {
count += 1 ;
}
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
def countPairs(arr, N):
count = 0
totalSum = 0
for i in range (N):
totalSum + = arr[i]
for i in range (N):
for j in range (i + 1 , N, 1 ):
if (totalSum % (arr[i] + arr[j]) = = 0 ):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
public static void countPairs( int [] arr, int N)
{
int count = 0, totalSum = 0;
for ( int i = 0; i < N; i++)
{
totalSum += arr[i];
}
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if (totalSum % (arr[i] + arr[j]) == 0) {
count += 1;
}
}
}
Console.WriteLine(count);
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(arr, N)
{
let count = 0, totalSum = 0;
for (let i = 0; i < N; i++) {
totalSum += arr[i];
}
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
if (totalSum
% (arr[i] + arr[j])
== 0) {
count += 1;
}
}
}
document.write(count);
}
let arr = [1, 2, 3, 4, 5];
let N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...