Find minimum integer greater than B that is product of two values less than A
Last Updated :
02 Aug, 2023
Given 2 positive integers A and B, the task is to find the minimum possible integer C, which is greater than or equal to B and is a product of two positive integers, both of which are less than or equal to A.
Examples:
Input: A = 5, B = 7
Output: 8
Explanation: 8 can be represented as a product of 2 positive integers less than 5. i.e. 2*4 = 8. Also, 8 is greater than B(=7). Note that 7 is not a possible answer as it can not be represented as a product of 2 integers. Hence, 8 is the minimum possible answer.
Input: A = 2, B = 5
Output: -1
Explanation: No such integer exists.
Approach: The problem can be solved based on the following concept:
For any integer less than A, we can find another integer (say x) upon mulitplying which we get a product of at least B. If any of such x lie in the range of [1, A], then we can get a possible porduct.
Follow the steps mentioned below to implement the idea:
- Iterate from i = 1 to A:
- In each iteration let X be ceil of (B/i).
- If the X is also less than A, then integers i and X satisfy both conditions.
- Since we need to find the minimum C, the C would be the minimum of all valid i*X.
- If no valid product was found, return -1, else return the minimum C.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findNumber( int A, int B)
{
int C = INT_MAX, X;
for ( int i = 1; i <= A; i++) {
X = (B + i - 1) / i;
if (X <= A) {
C = min(C, i * X);
}
}
if (C == INT_MAX) {
return -1;
}
return C;
}
int main()
{
int A = 5, B = 7;
int ans = findNumber(A, B);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int findNumber( int A, int B)
{
int C = Integer.MAX_VALUE, X;
for ( int i = 1 ; i <= A; i++) {
X = (B + i - 1 ) / i;
if (X <= A) {
C = Math.min(C, i * X);
}
}
if (C == Integer.MAX_VALUE) {
return - 1 ;
}
return C;
}
public static void main(String[] args)
{
int A = 5 , B = 7 ;
int ans = findNumber(A, B);
System.out.println(ans);
}
}
|
Python3
import math
def findNumber(A, B):
C = float ( 'inf' )
for i in range ( 1 , A + 1 ):
X = math.ceil(B / i)
if X < = A:
C = min (C, i * X)
if C = = float ( 'inf' ):
return - 1
return C
A = 5
B = 7
ans = findNumber(A, B)
print (ans)
|
C#
using System;
class GFG
{
static int FindNumber( int A, int B)
{
int C = int .MaxValue, X;
for ( int i = 1; i <= A; i++)
{
X = (B + i - 1) / i;
if (X <= A)
{
C = Math.Min(C, i * X);
}
}
if (C == int .MaxValue)
{
return -1;
}
return C;
}
static void Main( string [] args)
{
int A = 5, B = 7;
int ans = FindNumber(A, B);
Console.WriteLine(ans);
}
}
|
Javascript
function findNumber(A, B) {
let C = Infinity;
let X;
for (let i = 1; i <= A; i++) {
X = Math.ceil(B / i);
if (X <= A) {
C = Math.min(C, i * X);
}
}
if (C === Infinity) {
return -1;
}
return C;
}
const A = 5;
const B = 7;
const ans = findNumber(A, B);
console.log(ans);
|
Time Complexity: O(A)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...