Length of smallest sequence having sum X and product Y
Last Updated :
04 May, 2021
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
double temp( int n, int x)
{
return pow (x * 1.0 / n, n);
}
bool check( int n, int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
void find( int x, int y)
{
int high = ( int ) floor (x / exp (1.0));
int low = 1;
if (x == y)
cout << 1 << endl;
else if (!check(high, y, x))
cout << -1 << endl;
else {
while (high - low > 1) {
int mid
= (high + low) / 2;
if (check(mid, y, x))
high = mid;
else
low = mid;
}
cout << high << endl;
}
}
int main()
{
int x = 9, y = 8;
find(x, y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static double temp( int n, int x)
{
return Math.pow(x * 1.0 / n, n);
}
static boolean check( int n, int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
static void find( int x, int y)
{
int high = ( int )Math.floor(x /
Math.exp( 1.0 ));
int low = 1 ;
if (x == y)
System.out.print( 1 + "\n" );
else if (!check(high, y, x))
System.out.print(- 1 + "\n" );
else
{
while (high - low > 1 )
{
int mid = (high + low) / 2 ;
if (check(mid, y, x))
high = mid;
else
low = mid;
}
System.out.print(high + "\n" );
}
}
public static void main(String[] args)
{
int x = 9 , y = 8 ;
find(x, y);
}
}
|
Python3
from math import floor, exp
def temp(n, x):
return pow (x * 1 / n, n)
def check(n, y, x):
v = temp(n, x)
return (v > = y)
def find(x, y):
high = floor(x / exp( 1.0 ))
low = 1
if (x = = y):
print ( 1 )
elif ( not check(high, y, x)):
print ( - 1 )
else :
while (high - low > 1 ):
mid = (high + low) / / 2
if (check(mid, y, x)):
high = mid
else :
low = mid
print (high)
if __name__ = = '__main__' :
x = 9
y = 8
find(x, y)
|
C#
using System;
class GFG{
static double temp( int n,
int x)
{
return Math.Pow(x * 1.0 / n, n);
}
static bool check( int n,
int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
static void find( int x, int y)
{
int high = ( int )Math.Floor(x /
Math.Exp(1.0));
int low = 1;
if (x == y)
Console.Write(1 + "\n" );
else if (!check(high, y, x))
Console.Write(-1 + "\n" );
else
{
while (high - low > 1)
{
int mid = (high + low) / 2;
if (check(mid, y, x))
high = mid;
else
low = mid;
}
Console.Write(high + "\n" );
}
}
public static void Main(String[] args)
{
int x = 9, y = 8;
find(x, y);
}
}
|
Javascript
<script>
function temp(n, x)
{
return Math.pow((x * 1.0) / n, n);
}
function check(n, y, x)
{
var v = temp(n, x);
return v >= y;
}
function find(x, y)
{
var high = parseInt(Math.floor(x /
Math.exp(1.0)));
var low = 1;
if (x == y)
document.write(1 + "<br>" );
else if (!check(high, y, x))
document.write(-1 + "<br>" );
else
{
while (high - low > 1)
{
var mid = (high + low) / 2;
if (check(mid, y, x))
high = mid;
else
low = mid;
}
document.write(high + "<br>" );
}
}
var x = 9,
y = 8;
find(x, y);
</script>
|
Time Complexity: O(log2(X/e))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...