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.
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find two numbers made up of a given digit such that their difference is divisible by N
- Find two distinct numbers such that their LCM lies in given range
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Find two Composite Numbers such that there difference is N
- Find two numbers such that difference of their squares equal to N
- Find two numbers with sum N such that neither of them contains digit K
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find sub-arrays from given two arrays such that they have equal sum
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find two numbers whose sum and GCD are given
- Find two numbers with the given LCM and minimum possible difference
- Find two numbers with given sum and maximum possible LCM
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Print all integers that are sum of powers of two given numbers
- Ways of dividing a group into two halves such that two elements are in different groups
- Find two co-prime integers such that the first divides A and the second divides B
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Program to find if two numbers and their AM and HM are present in an array using STL
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.