Number of pairs from the first N natural numbers whose sum is divisible by K
Last Updated :
19 Apr, 2023
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.
Brute Force Approach:
This is the naive approach. Here we go from 1 to N, and for each number, we check for a number in the range, on the addition of both, which gives a number divisible by k.
Steps that were to follow the above approach:
- Initialize a variable count to 0 to keep track of the number of pairs whose sum is divisible by K.
- Iterate over all pairs of numbers i and j from 1 to N such that i < j.
- If (i+j) is divisible by K, increment the count.
- After iterating over all pairs, return the count as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findPairCount( int N, int K)
{
int count = 0;
for ( int i=1;i<=N;i++)
{
for ( int j=i+1;j<=N;j++)
{
if ((i+j)%K==0)
{
count++;
}
}
}
return count;
}
int main()
{
int N = 10, K = 4;
cout << findPairCount(N, K);
return 0;
}
|
Java
public class Main {
public static int findPairCount( int N, int K)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++) {
for ( int j = i + 1 ; j <= N; j++) {
if ((i + j) % K == 0 ) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int N = 10 , K = 4 ;
System.out.println(findPairCount(N, K));
}
}
|
Python3
def find_pair_count(N: int , K: int ) - > int :
count = 0
for i in range ( 1 , N):
for j in range (i + 1 , N + 1 ):
if (i + j) % K = = 0 :
count + = 1
return count
N = 10
K = 4
print (find_pair_count(N, K))
|
C#
using System;
class Program {
static int FindPairCount( int N, int K)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
if ((i + j) % K == 0) {
count++;
}
}
}
return count;
}
static void Main( string [] args)
{
int N = 10, K = 4;
Console.WriteLine(FindPairCount(N, K));
}
}
|
Javascript
function findPairCount(N, K) {
let count = 0;
for (let i = 1; i <= N; i++) {
for (let j = i + 1; j <= N; j++) {
if ((i + j) % K === 0) {
count++;
}
}
}
return count;
}
const N = 10,
K = 4;
console.log(findPairCount(N, K));
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Efficient Approach: An efficient approach is to use the 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.
Algorithm:
- Create a method named “findPairCount” with int return type which takes two integer values as input named “N” and “K”.
- Create an in-variable named “count” and initialize it with 0.
- Create an array of integer types of K size.
- Start a for loop and traverse from 1 to K-1.
- Inside the loop, calculate the count of integers with the specific remainder i when divided by K and store it in rem[i].
- Now check if K is even.
- If K is even, calculate the count of pairs when both integers are divisible by K and add it to the “count” variable.
- Start a for loop and traverse from 1 to K/2-1.
- Inside the loop, calculate the count of pairs when one integer has remainder i and the other integer has remainder K-i and add it to the “count” variable.
- Calculate the count of pairs when both integers have remainder K/2 and add it to the “count” variable.
- if K is not even, calculate the count of pairs when both integers are divisible by K and add it to the “count” variable.
- Start a for loop and traverse from 1 to K/2.
- Inside the loop, calculate the count of pairs when one integer has remainder i and the other integer has remainder K-i and add it to the “count” variable.
- Not return the final value of the “count” variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findPairCount( int N, int K)
{
int count = 0;
int rem[K];
rem[0] = N / K;
for ( int i = 1; i < K; i++)
rem[i] = (N - i) / K + 1;
if (K % 2 == 0) {
count += (rem[0] * (rem[0] - 1)) / 2;
for ( int i = 1; i < K / 2; i++)
count += rem[i] * rem[K - i];
count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
}
else {
count += (rem[0] * (rem[0] - 1)) / 2;
for ( int i = 1; i <= K / 2; i++)
count += rem[i] * rem[K - i];
}
return count;
}
int main()
{
int N = 10, K = 4;
cout << findPairCount(N, K);
return 0;
}
|
Java
class GfG
{
static int findPairCount( int N, int K)
{
int count = 0 ;
int rem[] = new int [K];
rem[ 0 ] = N / K;
for ( int i = 1 ; i < K; i++)
rem[i] = (N - i) / K + 1 ;
if (K % 2 == 0 )
{
count += (rem[ 0 ] * (rem[ 0 ] - 1 )) / 2 ;
for ( int i = 1 ; i < K / 2 ; i++)
count += rem[i] * rem[K - i];
count += (rem[K / 2 ] * (rem[K / 2 ] - 1 )) / 2 ;
}
else
{
count += (rem[ 0 ] * (rem[ 0 ] - 1 )) / 2 ;
for ( int i = 1 ; i <= K / 2 ; i++)
count += rem[i] * rem[K - i];
}
return count;
}
public static void main(String[] args)
{
int N = 10 , K = 4 ;
System.out.println(findPairCount(N, K));
}
}
|
Python3
def findPairCount(N, K) :
count = 0 ;
rem = [ 0 ] * K;
rem[ 0 ] = N / / K;
for i in range ( 1 , K) :
rem[i] = (N - i) / / K + 1 ;
if (K % 2 = = 0 ) :
count + = (rem[ 0 ] * (rem[ 0 ] - 1 )) / / 2 ;
for i in range ( 1 , K / / 2 ) :
count + = rem[i] * rem[K - i];
count + = (rem[K / / 2 ] * (rem[K / / 2 ] - 1 )) / / 2 ;
else :
count + = (rem[ 0 ] * (rem[ 0 ] - 1 )) / / 2 ;
for i in range ( 1 , K / / 2 + 1 ) :
count + = rem[i] * rem[K - i];
return count;
if __name__ = = "__main__" :
N = 10 ; K = 4 ;
print (findPairCount(N, K));
|
C#
class GfG
{
static int findPairCount( int N, int K)
{
int count = 0;
int [] rem = new int [K];
rem[0] = N / K;
for ( int i = 1; i < K; i++)
rem[i] = (N - i) / K + 1;
if (K % 2 == 0)
{
count += (rem[0] * (rem[0] - 1)) / 2;
for ( int i = 1; i < K / 2; i++)
count += rem[i] * rem[K - i];
count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
}
else
{
count += (rem[0] * (rem[0] - 1)) / 2;
for ( int i = 1; i <= K / 2; i++)
count += rem[i] * rem[K - i];
}
return count;
}
static void Main()
{
int N = 10, K = 4;
System.Console.WriteLine(findPairCount(N, K));
}
}
|
PHP
<?php
function findPairCount( $N , $K )
{
$count = 0;
$rem = array (0, $K , NULL);
$rem [0] = intval ( $N / $K );
for ( $i = 1; $i < $K ; $i ++)
$rem [ $i ] = intval (( $N - $i ) / $K ) + 1;
if ( $K % 2 == 0)
{
$count += ( $rem [0] * intval (( $rem [0] - 1)) / 2);
for ( $i = 1; $i < intval ( $K / 2); $i ++)
$count += $rem [ $i ] * $rem [ $K - $i ];
$count += ( $rem [ intval ( $K / 2)] *
intval (( $rem [ intval ( $K / 2)] - 1)) / 2);
}
else
{
$count += ( $rem [0] * intval (( $rem [0] - 1)) / 2);
for ( $i = 1; $i <= intval ( $K / 2); $i ++)
$count += $rem [ $i ] * $rem [ $K - $i ];
}
return $count ;
}
$N = 10;
$K = 4;
echo findPairCount( $N , $K );
?>
|
Javascript
<script>
function findPairCount(N , K)
{
var count = 0;
var rem = Array.from({length: K}, (_, i) => 0);
rem[0] = parseInt(N / K);
for (i = 1; i < K; i++)
rem[i] = parseInt((N - i) / K + 1);
if (K % 2 == 0)
{
count += parseInt((rem[0] * (rem[0] - 1)) / 2);
for (i = 1; i < K / 2; i++)
count += rem[i] * rem[K - i];
count += (rem[K / 2] * (rem[K / 2] - 1)) / 2;
}
else
{
count += (rem[0] * (rem[0] - 1)) / 2;
for (i = 1; i <= K / 2; i++)
count += rem[i] * rem[K - i];
}
return count;
}
var N = 10, K = 4;
document.write(findPairCount(N, K));
</script>
|
Time Complexity: O(K).
Auxiliary Space: O(K)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...