Find numbers a and b that satisfy the given conditions
Last Updated :
25 Feb, 2023
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.
Examples:
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.
Algorithm :
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:
C++
#include <bits/stdc++.h>
using namespace std;
void find( int n)
{
int b = n;
int a = b * (n - 1);
if (a * b > n && a / b < n) {
cout << "a = " << a << ", b = " << b;
}
else
cout << -1 << endl;
}
int main()
{
int n = 10;
find(n);
return 0;
}
|
Java
public class GFG{
static void find( int n)
{
int b = n;
int a = b * (n - 1 );
if (a * b > n && a / b < n) {
System.out.print( "a = " + a + ", b = " + b);
}
else
System.out.println(- 1 );
}
public static void main(String []args)
{
int n = 10 ;
find(n);
}
}
|
Python3
def find(n):
b = n
a = b * (n - 1 )
if a * b > n and a / / b < n:
print ( "a = {}, b = {}" . format (a, b))
else :
print ( - 1 )
if __name__ = = "__main__" :
n = 10
find(n)
|
C#
using System;
class GFG
{
static void find( int n)
{
int b = n;
int a = b * (n - 1);
if (a * b > n && a / b < n)
{
Console.Write( "a = " + a + ", b = " + b);
}
else
Console.WriteLine(-1);
}
public static void Main()
{
int n = 10;
find(n);
}
}
|
PHP
<?php
function find( $n )
{
$b = $n ;
$a = $b * ( $n - 1);
if ( $a * $b > $n && $a / $b < $n ) {
echo "a = " , $a , ", b = " , $b ;
}
else
echo -1 ;
}
$n = 10;
find( $n );
?>
|
Javascript
<script>
function find(n)
{
let b = n;
let a = b * (n - 1);
if (a * b > n && a / b < n)
{
document.write( "a = " + a + ", b = " + b);
}
else
document.write(-1);
}
let n = 10;
find(n);
</script>
|
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...