Given two numbers** X** and **Y**, the task is to find the count of integers K which satisfies the equation **(X – 1) % K = (Y – 1) % K**.

**Examples:**

Input:X = 2, Y = 6Output:3Explanation:

K = {1, 2, 4} satisfies the given equation. Therefore, the count is 3.

Input:X = 4, Y = 9Output:2

**Approach: **Follow the steps below to solve the problem:

- The idea is to find the
**absolute difference**of**X**and**Y**. - Find the count of divisors of the calculated absolute difference and print the count as the required answer.

Below is the implementation of the above approach:

## C++14

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count integers K` `// satisfying given equation` `int` `condition(` `int` `a, ` `int` `b)` `{` ` ` `// Calculate the absoluter` ` ` `// difference between a and b` ` ` `int` `d = ` `abs` `(a - b), count = 0;` ` ` `// Iterate till sqrt of the diffeence` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(d); i++) {` ` ` `if` `(d % i == 0) {` ` ` `if` `(d / i == i)` ` ` `count += 1;` ` ` `else` ` ` `count += 2;` ` ` `}` ` ` `}` ` ` `// Return the count` ` ` `return` `count;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `x = 2, y = 6;` ` ` `cout << condition(x, y) << endl;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG{` ` ` `// Function to count integers K` `// satisfying given equation` `static` `int` `condition(` `int` `a, ` `int` `b)` `{` ` ` ` ` `// Calculate the absoluter` ` ` `// difference between a and b` ` ` `int` `d = Math.abs(a - b), count = ` `0` `;` ` ` ` ` `// Iterate till sqrt of the diffeence` ` ` `for` `(` `int` `i = ` `1` `; i <= Math.sqrt(d); i++)` ` ` `{` ` ` `if` `(d % i == ` `0` `)` ` ` `{` ` ` `if` `(d / i == i)` ` ` `count += ` `1` `;` ` ` `else` ` ` `count += ` `2` `;` ` ` `}` ` ` `}` ` ` ` ` `// Return the count` ` ` `return` `count;` `}` ` ` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `x = ` `2` `, y = ` `6` `;` ` ` `System.out.println(condition(x, y));` `}` `}` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to count integers K` `# satisfying given equation` `def` `condition(a, b):` ` ` ` ` `# Calculate the absoluter` ` ` `# difference between a and b` ` ` `d ` `=` `abs` `(a ` `-` `b)` ` ` `count ` `=` `0` ` ` `# Iterate till sqrt of the diffeence` ` ` `for` `i ` `in` `range` `(` `1` `, d ` `+` `1` `):` ` ` `if` `i ` `*` `i > d:` ` ` `break` ` ` `if` `(d ` `%` `i ` `=` `=` `0` `):` ` ` `if` `(d ` `/` `/` `i ` `=` `=` `i):` ` ` `count ` `+` `=` `1` ` ` `else` `:` ` ` `count ` `+` `=` `2` ` ` `# Return the count` ` ` `return` `count` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `x ` `=` `2` ` ` `y ` `=` `6` ` ` ` ` `print` `(condition(x, y))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the ` `// above approach` `using` `System;` `class` `GFG{` ` ` `// Function to count ` `// integers K satisfying ` `// given equation` `static` `int` `condition(` `int` `a, ` ` ` `int` `b)` `{ ` ` ` `// Calculate the absoluter` ` ` `// difference between a and b` ` ` `int` `d = Math.Abs(a - b), count = 0;` ` ` `// Iterate till sqrt of ` ` ` `// the diffeence` ` ` `for` `(` `int` `i = 1; ` ` ` `i <= Math.Sqrt(d); i++)` ` ` `{` ` ` `if` `(d % i == 0)` ` ` `{` ` ` `if` `(d / i == i)` ` ` `count += 1;` ` ` `else` ` ` `count += 2;` ` ` `}` ` ` `}` ` ` `// Return the count` ` ` `return` `count;` `}` ` ` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `x = 2, y = 6;` ` ` `Console.WriteLine(condition(x, y));` `}` `}` `// This code is contributed by shikhasingrajput` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity: **O(√abs(X – Y))**Auxiliary Space: **O(1)

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.

## Recommended Posts:

- Find the largest possible value of K such that K modulo X is Y
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Count subarrays having sum modulo K same as the length of the subarray
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Count of all possible values of X such that A % X = B
- All possible values of floor(N/K) for all values of K
- Count the number of ways to give ranks for N students such that same ranks are possible
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Minimum removals required such that sum of remaining array modulo M is X
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count pairs from two arrays whose modulo operation yields K
- Count pairs whose product modulo 10^9 + 7 is equal to 1
- Number of subsets with same AND, OR and XOR values in an Array
- Find smallest possible Number from a given large Number with same count of digits
- Count of all possible pairs of array elements with same parity
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find the value of P and modular inverse of Q modulo 998244353
- Fibonacci Number modulo M and Pisano Period

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.