Given an **integer X**, the task is to find the possible values of **Q** for the pair **(Q, R)** such that their product is equal to **X** times their sum, where *Q ≤ R and X < 10 ^{7}*. Print the total count of such values of

**Q**along with values.

**Examples:**

Input:X = 3Output:

2

4, 6Explanation:

On taking Q = 4 and R = 12,

LHS = 12 x 4 = 48

RHS = 3(12 + 4) = 3 x 16 = 48 = LHS

Similarly, the equation also holds for value Q = 6 and R = 6.

LHS = 6 x 6 = 36

RHS = 3(6 + 6) = 3 x 12 = 36 = LHS

Input:X = 16Output:

5

17, 18, 20, 24, 32Explanation:

If Q = 17 and R = 272,

LHS = 17 x 272 = 4624

RHS = 16(17 + 272) = 16(289) = 4624 = LHS.

Similarly, there exists a value R for all other values of Q given in the output.

**Approach: **The idea is to understand the question to form an equation, that is** (Q x R) = X(Q + R).**

- The idea is to iterate from 1 to X and check for every if
**((( X + i ) * X) % i ) ==****0**. - Initialize a resultant vector, and iterate for all the values of X from 1.
- Check if the above condition holds true. If it does then push the value
**X+i**in the vector. - Let’s break the equation in order to understand it more clearly,

The given expression is

**(Q x R) = X(Q + R)**

On simplifying this we get,=> QR – QX = RX

or, QR – RX = QX

or, R = QX / (Q – X)

- Hence, observe that
**(X+i)**is the possible value of Q and**(X+i)*X**is the possible value of R.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find all possible values of Q ` `void` `values_of_Q(` `int` `X) ` `{ ` ` ` `// Vector initialization ` ` ` `// to store all numbers ` ` ` `// satisfying the given condition ` ` ` `vector<` `int` `> val_Q; ` ` ` ` ` `// Iterate for all the values of X ` ` ` `for` `(` `int` `i = 1; i <= X; i++) { ` ` ` ` ` `// Check if condition satisfied ` ` ` `// then push the number ` ` ` `if` `((((X + i) * X)) % i == 0) { ` ` ` ` ` `// Possible value of Q ` ` ` `val_Q.push_back(X + i); ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << val_Q.size() << endl; ` ` ` ` ` `// Print all the numbers ` ` ` `for` `(` `int` `i = 0; i < val_Q.size(); i++) { ` ` ` `cout << val_Q[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 3; ` ` ` ` ` `values_of_Q(X); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find all possible values of Q ` `static` `void` `values_of_Q(` `int` `X) ` `{ ` ` ` `// Vector initialization ` ` ` `// to store all numbers ` ` ` `// satisfying the given condition ` ` ` `ArrayList<Integer> val_Q = ` `new` `ArrayList<Integer>(); ` ` ` ` ` `// Iterate for all the values of X ` ` ` `for` `(` `int` `i = ` `1` `; i <= X; i++) ` ` ` `{ ` ` ` ` ` `// Check if condition satisfied ` ` ` `// then push the number ` ` ` `if` `((((X + i) * X)) % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Possible value of Q ` ` ` `val_Q.add(X + i); ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.println(val_Q.size()); ` ` ` ` ` `// Print all the numbers ` ` ` `for` `(` `int` `i = ` `0` `; i < val_Q.size(); i++) ` ` ` `{ ` ` ` `System.out.print(val_Q.get(i)+` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `X = ` `3` `; ` ` ` ` ` `values_of_Q(X); ` `} ` `} ` ` ` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find all possible values of Q ` `def` `values_of_Q(X): ` ` ` ` ` `# Vector initialization ` ` ` `# to store all numbers ` ` ` `# satisfying the given condition ` ` ` `val_Q ` `=` `[] ` ` ` ` ` `# Iterate for all the values of X ` ` ` `for` `i ` `in` `range` `(` `1` `, X ` `+` `1` `): ` ` ` ` ` `# Check if condition satisfied ` ` ` `# then push the number ` ` ` `if` `((((X ` `+` `i) ` `*` `X)) ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# Possible value of Q ` ` ` `val_Q.append(X ` `+` `i) ` ` ` ` ` `print` `(` `len` `(val_Q)) ` ` ` ` ` `# Print all the numbers ` ` ` `for` `i ` `in` `range` `(` `len` `(val_Q)): ` ` ` `print` `(val_Q[i], end ` `=` `" "` `) ` ` ` `# Driver Code ` `X ` `=` `3` ` ` `values_of_Q(X) ` ` ` `# This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to find all possible ` `// values of Q ` `static` `void` `values_of_Q(` `int` `X) ` `{ ` ` ` ` ` `// List initialization ` ` ` `// to store all numbers ` ` ` `// satisfying the given condition ` ` ` `List<` `int` `> val_Q = ` `new` `List<` `int` `>(); ` ` ` ` ` `// Iterate for all the values of X ` ` ` `for` `(` `int` `i = 1; i <= X; i++) ` ` ` `{ ` ` ` ` ` `// Check if condition satisfied ` ` ` `// then push the number ` ` ` `if` `((((X + i) * X)) % i == 0) ` ` ` `{ ` ` ` ` ` `// Possible value of Q ` ` ` `val_Q.Add(X + i); ` ` ` `} ` ` ` `} ` ` ` ` ` `Console.WriteLine(val_Q.Count); ` ` ` ` ` `// Print all the numbers ` ` ` `for` `(` `int` `i = 0; i < val_Q.Count; i++) ` ` ` `{ ` ` ` `Console.Write(val_Q[i] + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `X = 3; ` ` ` ` ` `values_of_Q(X); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

2 4 6

**Time Complexity: **O(N)

**Auxiliary Space: **O(X)

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:

- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Find any K distinct odd integers such that their sum is equal to N
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count different numbers possible using all the digits their frequency times
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Convert a number of length N such that it contains any one digit at least 'K' times
- Minimum deletions required such that any number X will occur exactly X times
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Count pairs from an array having product of their sum and difference equal to 1
- Count pairs from an array having product of their sum and difference equal to 0
- All possible values of floor(N/K) for all values of K
- Product of values of all possible non-empty subsets of given Array
- Find the first N integers such that the sum of their digits is equal to 10
- Minimum number of primes required such that their sum is equal to N
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Times required by Simple interest for the Principal to become Y times itself
- Count of pairs in an array such that the highest power of 2 that divides their product is 1

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.