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 an 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) = Bint 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 codeint main(){Â Â Â Â int a = 21, b = 5;Â
    cout << countX(a, b);Â
    return 0;} |
Java
// Java implementation of the approachclass 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) = Bdef 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 codeif __name__ == "__main__":Â Â Â Â a = 21Â Â Â Â b = 5Â
    print(countX(a, b))         # This code is contributed by ChitraNayal |
C#
// C# implementation of the approachusing 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.. |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the count// of all possible values for x// such that (A % x) = Bfunction countX(a, b){Â Â Â Â // Case 1Â Â Â Â if (b > a)Â Â Â Â Â Â Â Â return 0;Â
    // Case 2    else if (a == b)        return -1;Â
    // Case 3    else {        let x = a - b, ans = 0;Â
        // Find the number of divisors of x        // which are greater than b        for (let i = 1; i * i <= x; i++) {            if (x % i == 0) {                let d1 = i, d2 = b - 1;                if (i * i != x)                    d2 = parseInt(x / i);                if (d1 > b)                    ans++;                if (d2 > b)                    ans++;            }        }        return ans;    }}Â
// Driver code    let a = 21, b = 5;Â
    document.write(countX(a, b));Â
</script> |
Output:Â
2
Â
Time Complexity: O(sqrt(a – b))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



