Possible values of Q such that, for any value of R, their product is equal to X times their sum
Last Updated :
19 Mar, 2021
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 < 107. Print the total count of such values of Q along with values.
Examples:
Input: X = 3
Output:
2
4, 6
Explanation:
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 = 16
Output:
5
17, 18, 20, 24, 32
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
void values_of_Q( int X)
{
vector< int > val_Q;
for ( int i = 1; i <= X; i++) {
if ((((X + i) * X)) % i == 0) {
val_Q.push_back(X + i);
}
}
cout << val_Q.size() << endl;
for ( int i = 0; i < val_Q.size(); i++) {
cout << val_Q[i] << " " ;
}
}
int main()
{
int X = 3;
values_of_Q(X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void values_of_Q( int X)
{
ArrayList<Integer> val_Q = new ArrayList<Integer>();
for ( int i = 1 ; i <= X; i++)
{
if ((((X + i) * X)) % i == 0 )
{
val_Q.add(X + i);
}
}
System.out.println(val_Q.size());
for ( int i = 0 ; i < val_Q.size(); i++)
{
System.out.print(val_Q.get(i)+ " " );
}
}
public static void main(String[] args)
{
int X = 3 ;
values_of_Q(X);
}
}
|
Python3
def values_of_Q(X):
val_Q = []
for i in range ( 1 , X + 1 ):
if ((((X + i) * X)) % i = = 0 ):
val_Q.append(X + i)
print ( len (val_Q))
for i in range ( len (val_Q)):
print (val_Q[i], end = " " )
X = 3
values_of_Q(X)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void values_of_Q( int X)
{
List< int > val_Q = new List< int >();
for ( int i = 1; i <= X; i++)
{
if ((((X + i) * X)) % i == 0)
{
val_Q.Add(X + i);
}
}
Console.WriteLine(val_Q.Count);
for ( int i = 0; i < val_Q.Count; i++)
{
Console.Write(val_Q[i] + " " );
}
}
public static void Main(String[] args)
{
int X = 3;
values_of_Q(X);
}
}
|
Javascript
<script>
function values_of_Q(X)
{
let val_Q = [];
for (let i = 1; i <= X; i++)
{
if ((((X + i) * X)) % i == 0)
{
val_Q.push(X + i);
}
}
document.write(val_Q.length + "</br>" );
for (let i = 0; i < val_Q.length; i++) {
document.write(val_Q[i] + " " );
}
}
let X = 3;
values_of_Q(X);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(X)
Share your thoughts in the comments
Please Login to comment...