Count of pairs from 1 to a and 1 to b whose sum is divisible by N
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++
// C++ implementation of above approach#include <bits/stdc++.h>usingnamespacestd;// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.intfindCountOfPairs(inta,intb,intn){intans = 0;// Iterate over 1 to a to find distinct pairsfor(inti = 1; i <= a; i++) {// For each integer from 1 to a// b/n integers exists such that pair// sum is divisible by nans += b / n;// If (i%n +b%n ) >= n one more pair is possibleans += (i % n + b % n) >= n ? 1 : 0;}// Return answerreturnans;}// Driver codeintmain(){inta = 5, b = 13, n = 3;cout << findCountOfPairs(a, b, n);return0;}chevron_rightfilter_noneJava
// Java program for the above approachclassGFG{// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.staticintfindCountOfPairs(inta,intb,intn){intans =0;// Iterate over 1 to a to find distinct pairsfor(inti =1; i <= a; i++){// For each integer from 1 to a// b/n integers exists such that pair// sum is divisible by nans += b / n;// If (i%n +b%n ) >= n one more pair is possibleans += (i % n + b % n) >= n ?1:0;}// Return answerreturnans;}// Driver codepublicstaticvoidmain(String[] args){inta =5, b =13, n =3;System.out.println(findCountOfPairs(a, b, n));}}// This code has been contributed by 29AjayKumarchevron_rightfilter_nonePython3
# Python implementation of above approach# Function to find the distinct pairs from# 1-a & 1-b such that their sum is divisible by n.deffindCountOfPairs(a, b, n):ans=0foriinrange(1, a+1):# For each integer from 1 to a# b/n integers exists such that pair# / sum is divisible by nans+=b//n# If (i%n +b%n ) >= n one more pair is possibleans+=1if(i%n+b%n) >=nelse0returnans# Driver codea=5; b=13; n=3print(findCountOfPairs(a, b, n))# This code is contributed by Shrikant13chevron_rightfilter_noneC#
// C# program for the above approachusingSystem;classGFG{// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.staticintfindCountOfPairs(inta,intb,intn){intans = 0;// Iterate over 1 to a to find distinct pairsfor(inti = 1; i <= a; i++){// For each integer from 1 to a// b/n integers exists such that pair// sum is divisible by nans += b / n;// If (i%n +b%n ) >= n one more pair is possibleans += (i % n + b % n) >= n ? 1 : 0;}// Return answerreturnans;}// Driver codestaticpublicvoidMain (){inta = 5, b = 13, n = 3;Console.WriteLine(findCountOfPairs(a, b, n));}}// This code has been contributed by ajit.chevron_rightfilter_nonePHP
<?php// PHP implementation of above approach// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.functionfindCountOfPairs($a,$b,$n){$ans= 0;// Iterate over 1 to a to find// distinct pairsfor($i= 1;$i<=$a;$i++){// For each integer from 1 to a// b/n integers exists such that pair// sum is divisible by n$ans+= (int)($b/$n);// If (i%n +b%n ) >= n one more// pair is possible$ans+= (($i%$n) +($b%$n)) >=$n? 1 : 0;}// Return answerreturn$ans;}// Driver code$a= 5;$b= 13;$n= 3;echofindCountOfPairs($a,$b,$n);// This code is contributed by akt_mit.?>chevron_rightfilter_noneOutput:22
Time complexity: O(N)
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 exists a/N integers from range 1 to N*(a/N) which can form pair with b%N integer ranging from N*(b/N) to b.
- There exists a%N integers from range N*(a/N) to a which can form pair 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++
// C++ implementation of above approach#include <bits/stdc++.h>usingnamespacestd;// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.intfindCountOfPairs(inta,intb,intn){intans = 0;// pairs from 1 to n*(a/n) and 1 to n*(b/n)ans += n * (a / n) * (b / n);// pairs from 1 to n*(a/n) and n*(b/n) to bans += (a / n) * (b % n);// pairs from n*(a/n) to a and 1 to n*(b/n)ans += (a % n) * (b / n);// pairs from n*(a/n) to a and n*(b/n) to bans += ((a % n) + (b % n)) / n;// Return answerreturnans;}// Driver codeintmain(){inta = 5, b = 13, n = 3;cout << findCountOfPairs(a, b, n);return0;}chevron_rightfilter_noneJava
// Java implementation of above approachimportjava.io.*;classGFG{// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.staticintfindCountOfPairs(inta,intb,intn){intans =0;// pairs from 1 to n*(a/n) and 1 to n*(b/n)ans += n * (a / n) * (b / n);// pairs from 1 to n*(a/n) and n*(b/n) to bans += (a / n) * (b % n);// pairs from n*(a/n) to a and 1 to n*(b/n)ans += (a % n) * (b / n);// pairs from n*(a/n) to a and n*(b/n) to bans += ((a % n) + (b % n)) / n;// Return answerreturnans;}// Driver codepublicstaticvoidmain (String[] args){inta =5, b =13, n =3;System.out.println (findCountOfPairs(a, b, n));}}// This code is contributed by ajit..chevron_rightfilter_nonePython3
# Python 3 implementation of above approach# Function to find the distinct pairs from# 1-a & 1-b such that their sum is divisible by n.deffindCountOfPairs(a, b, n):ans=0# pairs from 1 to n*(a/n) and 1 to n*(b/n)ans+=n*int(a/n)*int(b/n)# pairs from 1 to n*(a/n) and n*(b/n) to bans+=int(a/n)*(b%n)# pairs from n*(a/n) to a and 1 to n*(b/n)ans+=(a%n)*int(b/n)# pairs from n*(a/n) to a and n*(b/n) to bans+=int(((a%n)+(b%n))/n);# Return answerreturnans# Driver codeif__name__=='__main__':a=5b=13n=3print(findCountOfPairs(a, b, n))# This code is contributed by# Surendra_Gangwarchevron_rightfilter_noneC#
// C# implementation of above approachusingSystem;classGFG{// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.staticintfindCountOfPairs(inta,intb,intn){intans = 0;// pairs from 1 to n*(a/n) and 1 to n*(b/n)ans += n * (a / n) * (b / n);// pairs from 1 to n*(a/n) and n*(b/n) to bans += (a / n) * (b % n);// pairs from n*(a/n) to a and 1 to n*(b/n)ans += (a % n) * (b / n);// pairs from n*(a/n) to a and n*(b/n) to bans += ((a % n) + (b % n)) / n;// Return answerreturnans;}// Driver codestaticpublicvoidMain (){inta = 5, b = 13, n = 3;Console.WriteLine(findCountOfPairs(a, b, n));}}// This code is contributed by @Tushilchevron_rightfilter_nonePHP
<?php// PHP implementation of above approach// Function to find the distinct pairs from// 1-a & 1-b such that their sum is divisible by n.functionfindCountOfPairs($a,$b,$n){$ans= 0;// pairs from 1 to n*(a/n) and 1 to n*(b/n)$ans+=$n* (int)($a/$n) * (int)($b/$n);// pairs from 1 to n*(a/n) and n*(b/n) to b$ans+= (int)($a/$n) * ($b%$n);// pairs from n*(a/n) to a and 1 to n*(b/n)$ans+= ($a%$n) * (int)($b/$n);// pairs from n*(a/n) to a and n*(b/n) to b$ans+= (($a%$n) + (int)($b%$n)) /$n;// Return answerreturn$ans;}// Driver code$a= 5;$b= 13;$n= 3;echofindCountOfPairs($a,$b,$n);// This code is contributed by ajit.?>chevron_rightfilter_noneOutput:22
Time complexity: O(1)
Recommended Posts:
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count pairs in array whose sum is divisible by K
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count number of pairs in array having sum divisible by K | SET 2
- Print k numbers where all pairs are divisible by m
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count rotations divisible by 8
- Count rotations divisible by 4
- Count pairs with given sum | Set 2
- Count pairs with Odd XOR
- Count the numbers divisible by 'M' in a given range
- Count integers in the range [A, B] that are not divisible by C and D
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count sub-arrays whose product is divisible by k
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.



