Count of all possible values of X such that A % X = B
Given two integers A and B. The task is to find the count of all possible values X such that A % X = B. If there are infinite number of possible values then print -1.
Examples:
Input: A = 21, B = 5
Output: 2
8 and 16 are the only valid values for X.
Input: A = 5, B = 5
Output: -1
X can have any value > 5
Approach: There are three possible cases:
- If A < B then no value of X can satisfy the given condition.
- If A = B then infinite solutions are possible. So, print -1 as X can be any value greater than A.
- If A > B then the number of divisors of (A – B) which are greater than B is the required count.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of all possible values for x // such that (A % x) = B int countX(int a, int b) { // Case 1 if (b > a) return 0; // Case 2 else if (a == b) return -1; // Case 3 else { int x = a - b, ans = 0; // Find the number of divisors of x // which are greater than b for (int i = 1; i * i <= x; i++) { if (x % i == 0) { int d1 = i, d2 = b - 1; if (i * i != x) d2 = x / i; if (d1 > b) ans++; if (d2 > b) ans++; } } return ans; } } // Driver code int main() { int a = 21, b = 5; cout << countX(a, b); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the count // of all possible values for x // such that (A % x) = B static int countX(int a, int b) { // Case 1 if (b > a) return 0; // Case 2 else if (a == b) return -1; // Case 3 else { int x = a - b, ans = 0; // Find the number of divisors of x // which are greater than b for (int i = 1; i * i <= x; i++) { if (x % i == 0) { int d1 = i, d2 = b - 1; if (i * i != x) d2 = x / i; if (d1 > b) ans++; if (d2 > b) ans++; } } return ans; } } // Driver code static public void main (String args[]) { int a = 21, b = 5; System.out.println(countX(a, b)); } } // This code is contributed by AnkitRai01 |
Python 3
# Python 3 implementation of the approach # Function to return the count # of all possible values for x # such that (A % x) = B def countX( a, b): # Case 1 if (b > a): return 0 # Case 2 elif (a == b): return -1 # Case 3 else: x = a - b ans = 0 # Find the number of divisors of x # which are greater than b i = 1 while i * i <= x: if (x % i == 0): d1 = i d2 = b - 1 if (i * i != x): d2 = x // i if (d1 > b): ans+=1 if (d2 > b): ans+=1 i+=1 return ans # Driver code if __name__ == "__main__": a = 21 b = 5 print(countX(a, b)) # This code is contributed by ChitraNayal |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count // of all possible values for x // such that (A % x) = B static int countX(int a, int b) { // Case 1 if (b > a) return 0; // Case 2 else if (a == b) return -1; // Case 3 else { int x = a - b, ans = 0; // Find the number of divisors of x // which are greater than b for (int i = 1; i * i <= x; i++) { if (x % i == 0) { int d1 = i, d2 = b - 1; if (i * i != x) d2 = x / i; if (d1 > b) ans++; if (d2 > b) ans++; } } return ans; } } // Driver code static public void Main () { int a = 21, b = 5; Console.WriteLine(countX(a, b)); } } // This code is contributed by anuj_67.. |
2
Recommended Posts:
- Count of alphabets whose ASCII values can be formed with the digits of N
- Number of values of b such that a = b + (a^b)
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Comparing X^Y and Y^X for very large values of X and Y
- Find smallest values of x and y such that ax - by = 0
- Find the values of X and Y in the Given Equations
- Sum of even values and update queries on an array
- Print values of 'a' in equation (a+b) <= n and a+b is divisible by x
- Sum of values of all possible non-empty subsets of the given array
- Possible to form a triangle from array values
- Sum of all natural numbers from L to R ( for large values of L and R )
- Product of values of all possible non-empty subsets of given Array
- Number of sextuplets (or six values) that satisfy an equation
- Maximum and Minimum Values of an Algebraic Expression
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.



