Find numbers a and b that satisfy the given conditions
Given an integer n, the task is to find two integers a and b which satisfy the below conditions:
- a % b = 0
- a * b > n
- a / b < n
If no pair satisfies the above conditions then print -1.
Note: There can be multiple (a, b) pairs satisfying the above conditions for n.
Input: n = 10 Output: a = 90, b = 10 90 % 10 = 0 90 * 10 = 900 > 10 90 / 10 = 9 < 10 All three conditions are satisfied. Input: n = 1 Output: -1
Approach: Let’s suppose b = n, by taking this assumption a can be found based on the given conditions:
- (a % b = 0) => a should be multiple of b.
- (a / b < n) => a / b = n – 1 which is < n.
- (a * b > n) => a = n.
Step 1: Start
Step 2: Create a static function called to find which takes an integer value as input.
Step 3: Create a variable called b and initialize it with the input integer n.
Step 4: Now, create another variable called a and store the multiplication of b and (n-1) in it.
Step 5: Now, set the condition If (a * b > n) and (a / b < n), then print the values of a and b else print -1.
Step 6: End
Below is the implementation of the above approach:
a = 90, b = 10
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Please Login to comment...