Count pairs in array whose sum is divisible by K in C++

C++Server Side ProgrammingProgramming

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 k or not.

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

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

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

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

Output − Count pairs in array whose sum is divisible by k 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 an integer variable as k then 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 k.

  • 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 % k == 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 k.

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

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

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

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

  • Start loop FOR as i to 1 till i <= k/2 AND i != (k-i)

  • Inside the loop, set count as count + new_arr[i] * (new_arr[k - i])

  • Check IF k % 2 = 0 then set count as count + (new_arr[k / 2] * (new_arr[k / 2] - 1) / 2)

  • Return the count

  • Print the result.

Example (naive approach)

 Live Demo

#include <iostream>
using namespace std;
int pair_k(int arr[], int size, int k){
   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 % k == 0){
            count++;
         }
      }
   }
   return count;
}
int main(){
   int arr[] = {4, 1, 2, 0, 2};
   int size = sizeof(arr) / sizeof(arr[0]);
   int k = 2;
   cout<<"Count pairs in array whose sum is divisible by 4 are: "<<pair_k(arr, size, k);
   return 0;
}

Output

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

Count pairs in array whose sum is divisible by k are: 6

Example (Efficient Approach)

 Live Demo

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

Output

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

Count pairs in array whose sum is divisible by k are: 6
raja
Published on 02-Nov-2020 05:55:39
Advertisements