Open In App

Length of smallest sequence having sum X and product Y

Last Updated : 04 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers X and Y, the task is to find the length of the smallest sequence consisting of positive integers having sum X and product Y. If no such sequence can be generated, print -1.

Examples:

Input: X = 5, Y = 5
Output: 1
Explanation:
The smallest possible sequence having sum 5 and product 5 is {5}. Therefore, the required answer is length of the sequence(= 1).

Input: X = 9, Y = 8
Output: 2
Explanation:
The smallest possible sequence is {1, 8} with sum X (= 1 + 8 = 9) and product Y(= 1 * 8 = 8).

Approach: The idea is to implement Binary Search to solve the given problem on the required size in the range [1, floor(x/e)] where e is the Euler constant.

  • Initialize two variables low and high to 1 and floor(x/e) respectively.
  • If X is equal to Y, then the maximum size of the sequence can always be 1. Therefore, print it.
  • Otherwise, iterate until the difference between high and low exceeds 1 and calculate mid each time.
  • Reset the values of high and low as per the boundary conditions at each step and print the final value of high at the end.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define pb push_back
 
// Function for checking valid or not
double temp(int n, int x)
{
    return pow(x * 1.0 / n, n);
}
 
// Function for checking boundary
// of binary search
bool check(int n, int y, int x)
{
    double v = temp(n, x);
    return (v >= y);
}
 
// Function to calculate the
// minimum sequence size
// using binary search
void find(int x, int y)
{
    // Initialize high and low
    int high = (int)floor(x / exp(1.0));
    int low = 1;
 
    // Base case
    if (x == y)
        cout << 1 << endl;
 
    // Print -1 if a sequence
    // cannot be generated
    else if (!check(high, y, x))
        cout << -1 << endl;
 
    // Otherwise
    else {
 
        // Iterate until difference
        // between high and low exceeds 1
        while (high - low > 1) {
 
            // Calculate mid
            int mid
                = (high + low) / 2;
 
            // Reset values of high
            // and low accordingly
            if (check(mid, y, x))
                high = mid;
            else
                low = mid;
        }
 
        // Print the answer
        cout << high << endl;
    }
}
 
// Driver Code
int main()
{
 
    int x = 9, y = 8;
 
    // Function call
    find(x, y);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function for checking valid or not
static double temp(int n, int x)
{
    return Math.pow(x * 1.0 / n, n);
}
 
// Function for checking boundary
// of binary search
static boolean check(int n, int y, int x)
{
    double v = temp(n, x);
    return (v >= y);
}
 
// Function to calculate the
// minimum sequence size
// using binary search
static void find(int x, int y)
{
     
    // Initialize high and low
    int high = (int)Math.floor(x /
                    Math.exp(1.0));
    int low = 1;
 
    // Base case
    if (x == y)
        System.out.print(1 + "\n");
 
    // Print -1 if a sequence
    // cannot be generated
    else if (!check(high, y, x))
        System.out.print(-1 + "\n");
 
    // Otherwise
    else
    {
 
        // Iterate until difference
        // between high and low exceeds 1
        while (high - low > 1)
        {
             
            // Calculate mid
            int mid = (high + low) / 2;
 
            // Reset values of high
            // and low accordingly
            if (check(mid, y, x))
                high = mid;
            else
                low = mid;
        }
 
        // Print the answer
        System.out.print(high + "\n");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 9, y = 8;
 
    // Function call
    find(x, y);
}
}
 
// This code is contributed by Amit Katiyar


Python3




# Python3 program for the above approach
from math import floor, exp
 
# Function for checking valid or not
def temp(n, x):
     
    return pow(x * 1 / n, n)
 
# Function for checking boundary
# of binary search
def check(n, y, x):
     
    v = temp(n, x)
    return (v >= y)
 
# Function to calculate the
# minimum sequence size
# using binary search
def find(x, y):
     
    # Initialize high and low
    high = floor(x / exp(1.0))
    low = 1
 
    # Base case
    if (x == y):
        print(1)
 
    # Print -1 if a sequence
    # cannot be generated
    elif (not check(high, y, x)):
        print(-1)
 
    # Otherwise
    else:
 
        # Iterate until difference
        # between high and low exceeds 1
        while (high - low > 1):
 
            # Calculate mid
            mid = (high + low) // 2
 
            # Reset values of high
            # and low accordingly
            if (check(mid, y, x)):
                high = mid
            else:
                low = mid
 
        # Print the answer
        print(high)
 
# Driver Code
if __name__ == '__main__':
 
    x = 9
    y = 8
 
    # Function call
    find(x, y)
 
# This code is contributed by mohit kumar 29


C#




// C# program for
// the above approach
using System;
class GFG{
 
// Function for checking
// valid or not
static double temp(int n,
                   int x)
{
  return Math.Pow(x * 1.0 / n, n);
}
 
// Function for checking
// boundary of binary search
static bool check(int n,
                  int y, int x)
{
  double v = temp(n, x);
  return (v >= y);
}
 
// Function to calculate the
// minimum sequence size
// using binary search
static void find(int x, int y)
{
  // Initialize high and low
  int high = (int)Math.Floor(x /
                  Math.Exp(1.0));
  int low = 1;
 
  // Base case
  if (x == y)
    Console.Write(1 + "\n");
 
  // Print -1 if a sequence
  // cannot be generated
  else if (!check(high, y, x))
    Console.Write(-1 + "\n");
 
  // Otherwise
  else
  {
    // Iterate until difference
    // between high and low exceeds 1
    while (high - low > 1)
    {
      // Calculate mid
      int mid = (high + low) / 2;
 
      // Reset values of high
      // and low accordingly
      if (check(mid, y, x))
        high = mid;
      else
        low = mid;
    }
 
    // Print the answer
    Console.Write(high + "\n");
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int x = 9, y = 8;
 
  // Function call
  find(x, y);
}
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// JavaScript program for the above approach
 
// Function for checking valid or not
function temp(n, x)
{
    return Math.pow((x * 1.0) / n, n);
}
 
// Function for checking boundary
// of binary search
function check(n, y, x)
{
    var v = temp(n, x);
    return v >= y;
}
 
// Function to calculate the
// minimum sequence size
// using binary search
function find(x, y)
{
     
    // Initialize high and low
    var high = parseInt(Math.floor(x /
                        Math.exp(1.0)));
    var low = 1;
     
    // Base case
    if (x == y)
        document.write(1 + "<br>");
     
    // Print -1 if a sequence
    // cannot be generated
    else if (!check(high, y, x))
        document.write(-1 + "<br>");
         
    // Otherwise
    else
    {
         
        // Iterate until difference
        // between high and low exceeds 1
        while (high - low > 1)
        {
             
            // Calculate mid
            var mid = (high + low) / 2;
             
            // Reset values of high
            // and low accordingly
            if (check(mid, y, x))
                high = mid;
            else
                low = mid;
        }
         
        // Print the answer
        document.write(high + "<br>");
    }
}
 
// Driver Code
var x = 9,
y = 8;
 
// Function call
find(x, y);
 
// This code is contributed by rdtank
 
</script>


Output

2

Time Complexity: O(log2(X/e)) 
Auxiliary Space: O(1) 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads