# Count pairs in array whose sum is divisible by 4 in C++

We are given an array of integer type elements and the task is to form the pairs from the given array and calculate the sum of elements in the pair and check whether the given sum is divisible by 4 or not.

Input − int arr[] = {4, 1, 2, 0, 2}

Output − Count pairs in array whose sum is divisible by 4 are − 2

Explanation − The pairs that can be formed from the given array are: (4, 1) = 5(not divisible by 4), (4, 2) = 6(not divisible by 4), (4, 0) = 4(divisible by 4), (1, 2) = 3(not divisible by 4), (1, 0) = 1(not divisible by 4), (2, 0) = 2(not divisible by 4), (2, 2) = 4(divisible by 4), (0, 2) = 2(not divisible by 4). So the pairs with the sum divisible by 4 are (4, 0) and (2, 2).

Input − int arr[] = {2, 4, 8, 6, 10}

Output − Count pairs in array whose sum is divisible by 4 are − 4

Explanation − The pairs that can be formed from the given array are: (2, 4) = 6(not divisible by 4), (2, 8) = 10(not divisible by 4), (2, 6) = 8(divisible by 4), (2, 10) = 12(divisible by 4), (4, 8) = 12(divisible by 4), (4, 6) = 10(not divisible by 4), (4, 10) = 14(not divisible by 4), (8, 6) = 14(not divisible by 4), (8, 10) = 18(not divisible by 4), (6, 10) = 16(divisible by 4). So the pairs with the sum divisible by 4 are (2, 10), (2, 6), (4, 8) and (6, 10).

## Approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

• Input an array of integer elements and calculate the size of an array and pass the data to the function

• Declare a temporary variable count to store the count of pairs with the sum divisible by 4.

• Start loop FOR from i to 0 till the size of an array

• Inside the loop, start another loop FOR from j to i + 1 till the size of an array

• Inside the loop calculate the sum as arr[i] + arr[j] and check IF sum % 4 == 0 then increment the count by 1.

• Return the count

• Print result.

## Efficient approach

• Input an array of integer elements and calculate the size of an array and pass the data to the function

• Declare a temporary variable count to store the count of pairs with the sum divisible by 4.

• Create an array of size 4 as we have to check the divisibility by 4.

• Start loop FOR from i to 0 till the size of an array

• Inside the loop, set temp as arr[i] % 4 and pre increment the array as ++check[temp]

• Set count as new_arr[0] * (new_arr[0] - 1)/2

• Set count as count + new_arr[2] * (new_arr[2] - 1)/2

• Set count as count + new_arr[1] * new_arr[3]

• Return the count

• Print the result.

## Example (naive approach)

Live Demo

#include <iostream>
using namespace std;
int pair_4(int arr[], int size){
int count = 0;
for(int i = 0 ;i <size ; i++){
for(int j = i+1; j<size; j++){
int sum = arr[i] + arr[j];
if(sum % 4 == 0){
count++;
}
}
}
return count;
}
int main(){
int arr[] = {4, 1, 2, 0, 2};
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"Count pairs in array whose sum is divisible by 4 are: "<<pair_4(arr, size);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count pairs in array whose sum is divisible by 4 are: 2

## Example (Efficient Approach)

Live Demo

#include <iostream>
using namespace std;
int pair_4(int arr[], int size){
int temp = 0;
int count = 0;
int check[] = {0, 0, 0, 0};
for (int i = 0; i < size; i++){
temp = arr[i] % 4;
++check[temp];
}
count = check[0] * (check[0] - 1) / 2;
count = count + check[2] * (check[2] - 1) / 2;
count = count + check[1] * check[3];
return count;
}
int main(){
int arr[] = {4, 1, 2, 0, 2};
int size = sizeof(arr) / sizeof(arr[0]);
cout<<"Count pairs in array whose sum is divisible by 4 are: "<<pair_4(arr, size);
return 0;
}

## Output

If we run the above code it will generate the following output −

Count pairs in array whose sum is divisible by 4 are: 2

Updated on: 02-Nov-2020

203 Views