Given two numbers a and b find all x such that a % x = b .
Input : a = 21, b = 5 Output : 2 The answers of the Modular Equation are 8 and 16 since 21 % 8 = 21 % 16 = 5 .
Here 3 cases arises :
- If ( a < b ) then there will be no answer .
- If ( a = b ) then all the numbers greater than a are the answer so there will be infinite solutions possible.
- If ( a > b ) Suppose x is an answer to our equation. Then x divides (a – b). Also since a % x = b then b < x. These conditions are necessary and sufficient as well. So the answer is number of divisors of a – b which are strictly greater than b which can be solved in O(sqrt( a-b )). Here only one case arises which we have to deal separately when (a-b) is perfect square then we will add its square root two times so we have to subtract one times, if this case arises.
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.
- Find two numbers whose sum and GCD are given
- Program to find LCM of 2 numbers without using GCD
- Find XOR of numbers from the range [L, R]
- Find the sum of first N odd Fibonacci numbers
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find two numbers with sum and product both same as N
- Find max of two Rational numbers
- Find the numbers from 1 to N that contains exactly k non-zero digits
- Find two numbers with sum N such that neither of them contains digit K
- Find the sum of the all amicable numbers up to N
- Find the sum of the first Nth Icosagonal Numbers
- Find the sum of all Betrothed numbers up to N
- Find the sum of the first N Dodecagonal Numbers
- Find two numbers whose sum is N and does not contain any digit as K
- Find the Nth Hogben Numbers
- Find the XOR of first N Prime Numbers
- Find three prime numbers with given sum
- Find two numbers with given sum and maximum possible LCM
- Find two prime numbers with given sum
- To find sum of two numbers without using any operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.