Open In App

Find minimum integer greater than B that is product of two values less than A

Last Updated : 02 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to Find the minimum possible
// integer C, which is greater than or
// equal to B and is product of two
// positive integers less than A
int findNumber(int A, int B)
{
 
    // Initialize variable C with INT_MAX
    // to store the final answer and
    // declare a variable X
    int C = INT_MAX, X;
 
    // Iterate i from 1 to A
    for (int i = 1; i <= A; i++) {
 
        // Compute ceil B/i
        X = (B + i - 1) / i;
 
        // Update answer if valid
        // product found
        if (X <= A) {
            C = min(C, i * X);
        }
    }
 
    // Output answer or -1 if no
    // valid product found
    if (C == INT_MAX) {
        return -1;
    }
    return C;
}
 
// Driver code
int main()
{
    int A = 5, B = 7;
 
    // Function call
    int ans = findNumber(A, B);
    cout << ans << endl;
 
    return 0;
}


Java




// Java code for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to Find the minimum possible
    // integer C, which is greater than or
    // equal to B and is product of two
    // positive integers less than A
    static int findNumber(int A, int B)
    {
 
        // Initialize variable C with Integer.MAX_VALUE
        // to store the final answer and
        // declare a variable X
        int C = Integer.MAX_VALUE, X;
 
        // Iterate i from 1 to A
        for (int i = 1; i <= A; i++) {
 
            // Compute ceil B/i
            X = (B + i - 1) / i;
 
            // Update answer if valid
            // product found
            if (X <= A) {
                C = Math.min(C, i * X);
            }
        }
 
        // Output answer or -1 if no
        // valid product found
        if (C == Integer.MAX_VALUE) {
            return -1;
        }
        return C;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 5, B = 7;
 
        // Function call
        int ans = findNumber(A, B);
        System.out.println(ans);
    }
}


Python3




import math
 
# Function to Find the minimum possible
# integer C, which is greater than or
# equal to B and is a product of two
# positive integers less than A
def findNumber(A, B):
    # Initialize variable C with float('inf')
    # to store the final answer
    C = float('inf')
 
    # Iterate i from 1 to A
    for i in range(1, A+1):
        # Compute ceil(B/i)
        X = math.ceil(B/i)
 
        # Update answer if a valid
        # product is found
        if X <= A:
            C = min(C, i * X)
 
    # Output answer or -1 if no
    # valid product is found
    if C == float('inf'):
        return -1
    return C
 
# Driver code
A = 5
B = 7
 
# Function call
ans = findNumber(A, B)
print(ans)


C#




// C# code for the above approach
 
using System;
 
class GFG
{
    // Function to Find the minimum possible
    // integer C, which is greater than or
    // equal to B and is product of two
    // positive integers less than A
    static int FindNumber(int A, int B)
    {
        // Initialize variable C with int.MaxValue
        // to store the final answer and
        // declare a variable X
        int C = int.MaxValue, X;
 
        // Iterate i from 1 to A
        for (int i = 1; i <= A; i++)
        {
            // Compute ceil B/i
            X = (B + i - 1) / i;
 
            // Update answer if valid
            // product found
            if (X <= A)
            {
                C = Math.Min(C, i * X);
            }
        }
 
        // Output answer or -1 if no
        // valid product found
        if (C == int.MaxValue)
        {
            return -1;
        }
        return C;
    }
 
    // Driver code
    static void Main(string[] args)
    {
        int A = 5, B = 7;
 
        // Function call
        int ans = FindNumber(A, B);
        Console.WriteLine(ans);
    }
}


Javascript




// Function to find the minimum possible
// integer C, which is greater than or
// equal to B and is the product of two
// positive integers less than A
function findNumber(A, B) {
  // Initialize variable C with Infinity
  // to store the final answer and
  // declare a variable X
  let C = Infinity;
  let X;
 
  // Iterate i from 1 to A
  for (let i = 1; i <= A; i++) {
    // Compute ceil B/i
    X = Math.ceil(B / i);
 
    // Update answer if a valid
    // product is found
    if (X <= A) {
      C = Math.min(C, i * X);
    }
  }
 
  // Output answer or -1 if no
  // valid product is found
  if (C === Infinity) {
    return -1;
  }
  return C;
}
 
// Driver code
const A = 5;
const B = 7;
 
// Function call
const ans = findNumber(A, B);
console.log(ans);


Output

8






Time Complexity: O(A)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads