- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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).

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.

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.

#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; }

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

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

#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; }

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

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

- Related Questions & Answers
- Count pairs in array whose sum is divisible by K in C++
- Count rotations divisible by 4 in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Count subarrays whose product is divisible by k in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Count of pairs in an array whose sum is a perfect square in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count divisible pairs in an array in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count pairs whose products exist in array in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Program to count number of fraction pairs whose sum is 1 in python
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++

Advertisements