problem id:--http://practice.geeksforgeeks.org/problems/min-value-of-x/0
In this problem you need to find the Positive integer root of the given equation
Since given equation is this ax^2+bx+c>=k
First convert it into normal form
ax^+bx+(c-k)>=0
Now find the +ve root of the quadratic solution . and if it's a decimal number Then just take ceil
of that value. That's all . Happy coding :)
code:--
#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
int main()
{
int t;
sc(t);
while(t--)
{
double a,b,c,k;
cin>>a>>b>>c>>k;
double x1;
x1=(-b+sqrt(b*b-4*a*(c-k)))/(2*a);
cout<<ceil(x1)<<'\n';
}
return 0;
}
In this problem you need to find the Positive integer root of the given equation
Since given equation is this ax^2+bx+c>=k
First convert it into normal form
ax^+bx+(c-k)>=0
Now find the +ve root of the quadratic solution . and if it's a decimal number Then just take ceil
of that value. That's all . Happy coding :)
code:--
#include <bits/stdc++.h>
using namespace std;
#define sc(x) scanf("%d",&x)
int main()
{
int t;
sc(t);
while(t--)
{
double a,b,c,k;
cin>>a>>b>>c>>k;
double x1;
x1=(-b+sqrt(b*b-4*a*(c-k)))/(2*a);
cout<<ceil(x1)<<'\n';
}
return 0;
}
You should also give its complexity and what if I entered some values for which no positive roots are available. As the GFG approach is also not optimal for negative roots.
ReplyDeleteBut yeah its better than the GFG approach.
Since GFG problem have positive values that's why i have only considered positive values. and for the complexity you can see my solution it's taking constant time complexity.
DeleteHappy coding!