Given the integer values of **N** and **K**. The task is to find the number of pairs from the set of natural numbers up to N{1, 2, 3……N-1, N} whose sum is divisible by K.

**Note :** 1 <= K <= N <= 10^6.

**Examples:**

Input :N = 10, K = 5

Output :9

Explanation :The possible pairs whose sum is divisible by 5 are (1, 4), (1, 9), (6, 4), (6, 9), (2, 3), (2, 8), (3, 7), (7, 8) and (5, 10). Hence the count is 9.

Input :N = 7, K = 3

Output :

Explanation :The possible pairs whose sum is divisible by 3 are (1, 2), (1, 5), (2, 4), (2, 7), (3, 6), (4, 5) and (5, 7). Hence the count is 7.

**Simple Approach:** A naive approach is to use a nested loop and check for all possible pairs and its divisibility by K. The time complexity of such an approach is O(N^2) which is not very efficient.

**Efficient Approach**: An efficient approach is to use basic Hashing technique.

**Firstly**, create array rem[K], where rem[i] contains the count of integers from 1 to N which gives the remainder i when divided by K. rem[i] can be calculated by the formula rem[i] = (N – i)/K + 1.

**Secondly**, the sum of two integers is divisible by K if:

- Both the integers are divisible by K. The count of which is calculated by rem[0]*(rem[0]-1)/2.
- Remainder of first integer is R and remainder of other number is K-R. The count of which is calculated by rem[R]*rem[K-R], where R varies from 1 to K/2.
- K is even and both the remainders are K/2. The count of which is calculated by rem[K/2]*(rem[K/2]-1)/2.

The sum of counts of all these cases gives the required count of pairs such that their sum is divisible by K.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of pairs ` `// from the set of natural numbers up to ` `// N whose sum is divisible by K ` `int` `findPairCount(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Declaring a Hash to store count ` ` ` `int` `rem[K]; ` ` ` ` ` `rem[0] = N / K; ` ` ` ` ` `// Storing the count of integers with ` ` ` `// a specific remainder in Hash array ` ` ` `for` `(` `int` `i = 1; i < K; i++) ` ` ` `rem[i] = (N - i) / K + 1; ` ` ` ` ` `// Check if K is even ` ` ` `if` `(K % 2 == 0) { ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[0] * (rem[0] - 1)) / 2; ` ` ` ` ` `// Count of pairs when one remainder ` ` ` `// is R and other remainder is K - R ` ` ` `for` `(` `int` `i = 1; i < K / 2; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` ` ` `// Count of pairs when both the ` ` ` `// remainders are K / 2 ` ` ` `count += (rem[K / 2] * (rem[K / 2] - 1)) / 2; ` ` ` `} ` ` ` `else` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[0] * (rem[0] - 1)) / 2; ` ` ` ` ` `// Count of pairs when one remainder is R ` ` ` `// and other remainder is K - R ` ` ` `for` `(` `int` `i = 1; i <= K / 2; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 10, K = 4; ` ` ` ` ` `// Print the count of pairs ` ` ` `cout << findPairCount(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GfG ` `{ ` ` ` `// Function to find the number of pairs ` `// from the set of natural numbers up to ` `// N whose sum is divisible by K ` `static` `int` `findPairCount(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Declaring a Hash to store count ` ` ` `int` `rem[] = ` `new` `int` `[K]; ` ` ` ` ` `rem[` `0` `] = N / K; ` ` ` ` ` `// Storing the count of integers with ` ` ` `// a specific remainder in Hash array ` ` ` `for` `(` `int` `i = ` `1` `; i < K; i++) ` ` ` `rem[i] = (N - i) / K + ` `1` `; ` ` ` ` ` `// Check if K is even ` ` ` `if` `(K % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[` `0` `] * (rem[` `0` `] - ` `1` `)) / ` `2` `; ` ` ` ` ` `// Count of pairs when one remainder ` ` ` `// is R and other remainder is K - R ` ` ` `for` `(` `int` `i = ` `1` `; i < K / ` `2` `; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` ` ` `// Count of pairs when both the ` ` ` `// remainders are K / 2 ` ` ` `count += (rem[K / ` `2` `] * (rem[K / ` `2` `] - ` `1` `)) / ` `2` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[` `0` `] * (rem[` `0` `] - ` `1` `)) / ` `2` `; ` ` ` ` ` `// Count of pairs when one remainder is R ` ` ` `// and other remainder is K - R ` ` ` `for` `(` `int` `i = ` `1` `; i <= K / ` `2` `; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `10` `, K = ` `4` `; ` ` ` ` ` `// Print the count of pairs ` ` ` `System.out.println(findPairCount(N, K)); ` `} ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the number of pairs ` `# from the set of natural numbers up to ` `# N whose sum is divisible by K ` `def` `findPairCount(N, K) : ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# Declaring a Hash to store count ` ` ` `rem ` `=` `[` `0` `] ` `*` `K; ` ` ` ` ` `rem[` `0` `] ` `=` `N ` `/` `/` `K; ` ` ` ` ` `# Storing the count of integers with ` ` ` `# a specific remainder in Hash array ` ` ` `for` `i ` `in` `range` `(` `1` `, K) : ` ` ` `rem[i] ` `=` `(N ` `-` `i) ` `/` `/` `K ` `+` `1` `; ` ` ` ` ` `# Check if K is even ` ` ` `if` `(K ` `%` `2` `=` `=` `0` `) : ` ` ` ` ` `# Count of pairs when both ` ` ` `# integers are divisible by K ` ` ` `count ` `+` `=` `(rem[` `0` `] ` `*` `(rem[` `0` `] ` `-` `1` `)) ` `/` `/` `2` `; ` ` ` ` ` `# Count of pairs when one remainder ` ` ` `# is R and other remainder is K - R ` ` ` `for` `i ` `in` `range` `(` `1` `, K ` `/` `/` `2` `) : ` ` ` `count ` `+` `=` `rem[i] ` `*` `rem[K ` `-` `i]; ` ` ` ` ` `# Count of pairs when both the ` ` ` `# remainders are K / 2 ` ` ` `count ` `+` `=` `(rem[K ` `/` `/` `2` `] ` `*` `(rem[K ` `/` `/` `2` `] ` `-` `1` `)) ` `/` `/` `2` `; ` ` ` ` ` `else` `: ` ` ` ` ` `# Count of pairs when both ` ` ` `# integers are divisible by K ` ` ` `count ` `+` `=` `(rem[` `0` `] ` `*` `(rem[` `0` `] ` `-` `1` `)) ` `/` `/` `2` `; ` ` ` ` ` `# Count of pairs when one remainder is R ` ` ` `# and other remainder is K - R ` ` ` `for` `i ` `in` `rage(` `1` `, K` `/` `/` `2` `+` `1` `) : ` ` ` `count ` `+` `=` `rem[i] ` `*` `rem[K ` `-` `i]; ` ` ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `10` `; K ` `=` `4` `; ` ` ` ` ` `# Print the count of pairs ` ` ` `print` `(findPairCount(N, K)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `class` `GfG ` `{ ` ` ` `// Function to find the number of pairs ` `// from the set of natural numbers up to ` `// N whose sum is divisible by K ` `static` `int` `findPairCount(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Declaring a Hash to store count ` ` ` `int` `[] rem = ` `new` `int` `[K]; ` ` ` ` ` `rem[0] = N / K; ` ` ` ` ` `// Storing the count of integers with ` ` ` `// a specific remainder in Hash array ` ` ` `for` `(` `int` `i = 1; i < K; i++) ` ` ` `rem[i] = (N - i) / K + 1; ` ` ` ` ` `// Check if K is even ` ` ` `if` `(K % 2 == 0) ` ` ` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[0] * (rem[0] - 1)) / 2; ` ` ` ` ` `// Count of pairs when one remainder ` ` ` `// is R and other remainder is K - R ` ` ` `for` `(` `int` `i = 1; i < K / 2; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` ` ` `// Count of pairs when both the ` ` ` `// remainders are K / 2 ` ` ` `count += (rem[K / 2] * (rem[K / 2] - 1)) / 2; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `count += (rem[0] * (rem[0] - 1)) / 2; ` ` ` ` ` `// Count of pairs when one remainder is R ` ` ` `// and other remainder is K - R ` ` ` `for` `(` `int` `i = 1; i <= K / 2; i++) ` ` ` `count += rem[i] * rem[K - i]; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `N = 10, K = 4; ` ` ` ` ` `// Print the count of pairs ` ` ` `System.Console.WriteLine(findPairCount(N, K)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to find the number of pairs ` `// from the set of natural numbers up ` `// to N whose sum is divisible by K ` `function` `findPairCount(` `$N` `, ` `$K` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `// Declaring a Hash to store count ` ` ` `$rem` `= ` `array` `(0, ` `$K` `, NULL); ` ` ` ` ` `$rem` `[0] = ` `intval` `(` `$N` `/ ` `$K` `); ` ` ` ` ` `// Storing the count of integers with ` ` ` `// a specific remainder in Hash array ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `$K` `; ` `$i` `++) ` ` ` `$rem` `[` `$i` `] = ` `intval` `((` `$N` `- ` `$i` `) / ` `$K` `) + 1; ` ` ` ` ` `// Check if K is even ` ` ` `if` `(` `$K` `% 2 == 0) ` ` ` `{ ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `$count` `+= (` `$rem` `[0] * ` `intval` `((` `$rem` `[0] - 1)) / 2); ` ` ` ` ` `// Count of pairs when one remainder ` ` ` `// is R and other remainder is K - R ` ` ` `for` `(` `$i` `= 1; ` `$i` `< ` `intval` `(` `$K` `/ 2); ` `$i` `++) ` ` ` `$count` `+= ` `$rem` `[` `$i` `] * ` `$rem` `[` `$K` `- ` `$i` `]; ` ` ` ` ` `// Count of pairs when both the ` ` ` `// remainders are K / 2 ` ` ` `$count` `+= (` `$rem` `[` `intval` `(` `$K` `/ 2)] * ` ` ` `intval` `((` `$rem` `[` `intval` `(` `$K` `/ 2)] - 1)) / 2); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Count of pairs when both ` ` ` `// integers are divisible by K ` ` ` `$count` `+= (` `$rem` `[0] * ` `intval` `((` `$rem` `[0] - 1)) / 2); ` ` ` ` ` `// Count of pairs when one remainder is R ` ` ` `// and other remainder is K - R ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `intval` `(` `$K` `/ 2); ` `$i` `++) ` ` ` `$count` `+= ` `$rem` `[` `$i` `] * ` `$rem` `[` `$K` `- ` `$i` `]; ` ` ` `} ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$N` `= 10; ` `$K` `= 4; ` ` ` `// Print the count of pairs ` `echo` `findPairCount(` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity**: O(K).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the first natural number whose factorial is divisible by x
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Check whether factorial of N is divisible by sum of first N natural numbers
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Check if product of first N natural numbers is divisible by their sum
- Count of pairs with sum N from first N natural numbers
- Count natural numbers whose factorials are divisible by x but not y
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Check if the concatenation of first N natural numbers is divisible by 3
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Find if given number is sum of first n natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Count pairs in array whose sum is divisible by K
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.