Given the constants of quadratic equation F(x) = Ax2 + Bx + C as A, B, and C and an integer K, the task is to find the smallest value of root x such that F(x) ? K and x > 0. If no such values exist then print “-1”. It is given that F(x) is a monotonically increasing function.
Examples:
Input: A = 3, B = 4, C = 5, K = 6
Output: 1
Explanation:
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 1, F(x) = 12, which is greater than the given value of K.
Input: A = 3, B = 4, C = 5, K = 150
Output: 7
Explanation:
For the given values F(x) = 3x2 + 4x + 5 the minimum value of x is 7, F(x) = 180, which is greater than the given value of K.
Approach: The idea is to use Binary Search to find the minimum value of x. Below are the steps:
- To get the value equal to or greater than K, the value of x must be in the range [1, sqrt(K)] as this is a quadratic equation.
- Now, basically there is a need to search the appropriate element in the range, so for this binary search is implemented.
- Computing F(mid), where mid is the middle value for the range [1, sqrt(K)]. Now the following three cases are possible:
- If F(mid) ? K && F(mid) < K: This mean the current mid is the required answer.
- If F(mid) < K: This means the current value of mid is less than the required value of x. So, move towards the right, i.e., in the second half as F(x) is an increasing function.
- If F(mid) > K: This means the current value of mid is greater than the required value of x. So, move towards the left, i.e., the first half as F(x) is an increasing function.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int func( int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
int findMinx( int A, int B, int C, int K)
{
int start = 1;
int end = ceil ( sqrt (K));
while (start <= end) {
int mid = start + (end - start) / 2;
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1);
if (x >= K && Y < K) {
return mid;
}
else if (x < K) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return -1;
}
int main()
{
int A = 3, B = 4, C = 5, K = 6;
cout << findMinx(A, B, C, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int func( int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
static int findMinx( int A, int B, int C, int K)
{
int start = 1 ;
int end = ( int )Math.ceil(Math.sqrt(K));
while (start <= end)
{
int mid = start + (end - start) / 2 ;
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1 );
if (x >= K && Y < K)
{
return mid;
}
else if (x < K)
{
start = mid + 1 ;
}
else
{
end = mid - 1 ;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int A = 3 , B = 4 , C = 5 , K = 6 ;
System.out.println(findMinx(A, B, C, K));
}
}
|
Python3
import math
def func(A, B, C, x):
return (A * x * x + B * x + C)
def findMinx(A, B, C, K):
start = 1
end = math.ceil(math.sqrt(K))
while (start < = end):
mid = start + (end - start) / / 2
x = func(A, B, C, mid)
Y = func(A, B, C, mid - 1 )
if (x > = K and Y < K):
return mid
elif (x < K):
start = mid + 1
else :
end = mid - 1
return - 1
A = 3
B = 4
C = 5
K = 6
print (findMinx(A, B, C, K))
|
C#
using System;
class GFG{
static int func( int A, int B, int C, int x)
{
return (A * x * x + B * x + C);
}
static int findMinx( int A, int B, int C, int K)
{
int start = 1;
int end = ( int )Math.Ceiling(Math.Sqrt(K));
while (start <= end)
{
int mid = start + (end - start) / 2;
int x = func(A, B, C, mid);
int Y = func(A, B, C, mid - 1);
if (x >= K && Y < K)
{
return mid;
}
else if (x < K)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return -1;
}
public static void Main(String[] args)
{
int A = 3, B = 4, C = 5, K = 6;
Console.WriteLine(findMinx(A, B, C, K));
}
}
|
Javascript
<script>
function func(A , B , C , x)
{
return (A * x * x + B * x + C);
}
function findMinx(A , B , C , K)
{
var start = 1;
var end = parseInt(Math.ceil(Math.sqrt(K)));
while (start <= end)
{
var mid = start + parseInt((end - start) / 2);
var x = func(A, B, C, mid);
var Y = func(A, B, C, mid - 1);
if (x >= K && Y < K)
{
return mid;
}
else if (x < K)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return -1;
}
var A = 3, B = 4, C = 5, K = 6;
document.write(findMinx(A, B, C, K));
</script>
|
Time Complexity: O(log(sqrt(K))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Oct, 2021
Like Article
Save Article