
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count pairs in array whose sum is divisible by K 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 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)
#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)
#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
- Related Articles
- Count pairs in array whose sum is divisible by 4 in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Count subarrays whose product is divisible by k in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count pairs in a sorted array whose product is less than k in C++
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- 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++
- Count all sub-arrays having sum divisible by k
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Maximize the number of sum pairs which are divisible by K in C++
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Count number of distinct pairs whose sum exists in the given array in C++
