Count of pairs from 1 to a and 1 to b whose sum is divisible by N
Last Updated :
15 Jun, 2022
Given three integers a, b and N. Find the total number of distinct pairs which can be formed by selecting one integer from 1 to a and other from 1 to b, such that their sum is divisible by N.
Examples:
Input : a = 4, b = 4, N = 4
Output : 4
Input : a = 5, b = 13, N = 3
Output : 22
Basic Approach: For a pair to be divisible by N it must contain one number from range 1 to a and other from 1 to b.
So, for this iterate over integers from 1 to a and for each integer (i), b/N numbers are there whose sum with i will be divisible by N. Also if (i%N + b%N) >= N then 1 more pair exists whose sum is divisible by N.
For example took a = 7, b = 6 and N = 4:
Let's check for i = 3:
- b/N = 6/4 = 1 => there is one integer from 1 to b,
whose sum with 3 is divisible by 4 i.e.(3, 1).
- Also i%N + b%N = 3%4 + 6%4 = 3+2 = 5 > 4,
means one more integer exists from 1 to b
whose sum with 3 is divisible by 4 i.e.(3, 5).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCountOfPairs( int a, int b, int n)
{
int ans = 0;
for ( int i = 1; i <= a; i++) {
ans += b / n;
ans += (i % n + b % n) >= n ? 1 : 0;
}
return ans;
}
int main()
{
int a = 5, b = 13, n = 3;
cout << findCountOfPairs(a, b, n);
return 0;
}
|
Java
class GFG
{
static int findCountOfPairs( int a, int b, int n)
{
int ans = 0 ;
for ( int i = 1 ; i <= a; i++)
{
ans += b / n;
ans += (i % n + b % n) >= n ? 1 : 0 ;
}
return ans;
}
public static void main(String[] args)
{
int a = 5 , b = 13 , n = 3 ;
System.out.println(findCountOfPairs(a, b, n));
}
}
|
Python3
def findCountOfPairs(a, b, n):
ans = 0
for i in range ( 1 , a + 1 ):
ans + = b / / n
ans + = 1 if (i % n + b % n) > = n else 0
return ans
a = 5 ; b = 13 ; n = 3
print (findCountOfPairs(a, b, n))
|
C#
using System;
class GFG
{
static int findCountOfPairs( int a, int b, int n)
{
int ans = 0;
for ( int i = 1; i <= a; i++)
{
ans += b / n;
ans += (i % n + b % n) >= n ? 1 : 0;
}
return ans;
}
static public void Main ()
{
int a = 5, b = 13, n = 3;
Console.WriteLine(findCountOfPairs(a, b, n));
}
}
|
PHP
<?php
function findCountOfPairs( $a , $b , $n )
{
$ans = 0;
for ( $i = 1; $i <= $a ; $i ++)
{
$ans += (int)( $b / $n );
$ans += (( $i % $n ) +
( $b % $n )) >= $n ? 1 : 0;
}
return $ans ;
}
$a = 5;
$b = 13;
$n = 3;
echo findCountOfPairs( $a , $b , $n );
?>
|
Javascript
<script>
function findCountOfPairs(a, b, n)
{
let ans = 0;
for (let i = 1; i <= a; i++)
{
ans += parseInt(b / n, 10);
ans += (i % n + b % n) >= n ? 1 : 0;
}
return ans;
}
let a = 5, b = 13, n = 3;
document.write(findCountOfPairs(a, b, n));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Second Approach:- This Approach Is a little Tricky. Here we find how much pair for a multiple of N.
First:- Keep (a<b), if not then make using swap.
Second:- Start a for loop from the lowest multiple of N and go through the multiple.
- Now Smallest element ( a ) is greater than or equally current multiple then we add ((Current_Multiple) – 1) pair to the ans.
- Now a is smaller But b is greater than or equally current multiple than we add a to the ans.
- Now if a and b both smaller than we count the remaining pair for add a – (current_multiple – b ) + 1.
- Break The Loop.
Below is the implementation of the above logic.
C++
#include <bits/stdc++.h>
using namespace std;
int findCountOfPairs( int a, int b, int n)
{
if (a > b)
{
swap(a, b);
}
int temp = 1, count = 0;
for ( int i = n; temp > 0; i += n)
{
if (a >= i)
{
temp = i - 1;
}
else if (b >= i)
{
temp = a;
}
else if (i > b)
{
temp = a - (i - b) + 1;
}
if (temp > 0)
{
count += temp;
}
}
return count;
}
int main()
{
int a = 5, b = 13, n = 3;
cout << findCountOfPairs(a, b, n);
return 0;
}
|
Java
class GFG{
public static int findCountOfPairs( int a,
int b,
int n)
{
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
int temp = 1 , count = 0 ;
for ( int i = n; temp > 0 ; i += n)
{
if (a >= i)
{
temp = i - 1 ;
}
else if (b >= i)
{
temp = a;
}
else if (i > b)
{
temp = a - (i - b) + 1 ;
}
if (temp > 0 )
{
count += temp;
}
}
return count;
}
public static void main(String[] args)
{
int a = 5 , b = 13 , n = 3 ;
System.out.print(findCountOfPairs(a,
b, n));
}
}
|
Python3
def findCountOfPairs(a, b, n):
if (a > b):
a, b = b, a
temp = 1
count = 0
i = n
while (temp > 0 ):
if (a > = i):
temp = i - 1
elif (b > = i):
temp = a
elif (i > b):
temp = a - (i - b) + 1
if (temp > 0 ):
count + = temp
i + = n
return count
a = 5
b = 13
n = 3
print (findCountOfPairs(a, b, n))
|
C#
using System;
class GFG
{
static int findCountOfPairs( int a, int b, int n)
{
if (a > b)
{
int temp1 = a;
a = b;
b = temp1;
}
int temp = 1, count = 0;
for ( int i = n; temp > 0; i += n)
{
if (a >= i)
{
temp = i - 1;
}
else if (b >= i)
{
temp = a;
}
else if (i > b)
{
temp = a - (i - b) + 1;
}
if (temp > 0)
{
count += temp;
}
}
return count;
}
static void Main()
{
int a = 5, b = 13, n = 3;
Console.WriteLine(findCountOfPairs(a, b, n));
}
}
|
Javascript
<script>
function findCountOfPairs(a, b, n)
{
if (a > b)
{
let temp1 = a;
a = b;
b = temp1;
}
let temp = 1, count = 0;
for (let i = n; temp > 0; i += n)
{
if (a >= i)
{
temp = i - 1;
}
else if (b >= i)
{
temp = a;
}
else if (i > b)
{
temp = a - (i - b) + 1;
}
if (temp > 0)
{
count += temp;
}
}
return count;
}
let a = 5, b = 13, n = 3;
document.write(findCountOfPairs(a, b, n));
</script>
|
Output :
22
Time Complexity: O(n)
Auxiliary Space: O(1)
Efficient Approach: For solving the problem efficiently break it into four-part and solve as:
- Each integer from range 1 to N*(a/N) will have exactly b/N integers from 1 to N*(b/N) whose sum is divisible by N.
- There exist a/N integers from range 1 to N*(a/N) which can form pairs with b%N integer ranging from N*(b/N) to b.
- There exist a%N integers from range N*(a/N) to a which can form pairs with b/N integer ranging from 1 to N*(b/N).
- There exists (a%N + b%N)/N integers from range N*(a/N) to a and from N*(b/N) to b which can form pair whose sum is divisible by N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCountOfPairs( int a, int b, int n)
{
int ans = 0;
ans += n * (a / n) * (b / n);
ans += (a / n) * (b % n);
ans += (a % n) * (b / n);
ans += ((a % n) + (b % n)) / n;
return ans;
}
int main()
{
int a = 5, b = 13, n = 3;
cout << findCountOfPairs(a, b, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int findCountOfPairs( int a, int b, int n)
{
int ans = 0 ;
ans += n * (a / n) * (b / n);
ans += (a / n) * (b % n);
ans += (a % n) * (b / n);
ans += ((a % n) + (b % n)) / n;
return ans;
}
public static void main (String[] args)
{
int a = 5 , b = 13 , n = 3 ;
System.out.println (findCountOfPairs(a, b, n));
}
}
|
Python3
def findCountOfPairs(a, b, n):
ans = 0
ans + = n * int (a / n) * int (b / n)
ans + = int (a / n) * (b % n)
ans + = (a % n) * int (b / n)
ans + = int (((a % n) + (b % n)) / n);
return ans
if __name__ = = '__main__' :
a = 5
b = 13
n = 3
print (findCountOfPairs(a, b, n))
|
C#
using System;
class GFG
{
static int findCountOfPairs( int a, int b, int n)
{
int ans = 0;
ans += n * (a / n) * (b / n);
ans += (a / n) * (b % n);
ans += (a % n) * (b / n);
ans += ((a % n) + (b % n)) / n;
return ans;
}
static public void Main (){
int a = 5, b = 13, n = 3;
Console.WriteLine(findCountOfPairs(a, b, n));
}
}
|
PHP
<?php
function findCountOfPairs( $a , $b , $n )
{
$ans = 0;
$ans += $n * (int)( $a / $n ) * (int)( $b / $n );
$ans += (int)( $a / $n ) * ( $b % $n );
$ans += ( $a % $n ) * (int)( $b / $n );
$ans += (( $a % $n ) + (int)( $b % $n )) / $n ;
return $ans ;
}
$a = 5;
$b = 13;
$n = 3;
echo findCountOfPairs( $a , $b , $n );
?>
|
Javascript
<script>
function findCountOfPairs(a, b, n)
{
let ans = 0;
ans += n * parseInt(a / n, 10) * parseInt(b / n, 10)
ans += parseInt(a / n, 10) * parseInt(b % n, 10);
ans += parseInt(a % n, 10) * parseInt(b / n, 10);
ans += parseInt(((a % n) + (b % n)) / n, 10);
return ans;
}
let a = 5, b = 13, n = 3;
document.write(findCountOfPairs(a, b, n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...