Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Monk’s Encounter with Polynomial problem solution

YASH PAL, 31 July 2024
In this HackerEarth Monk’s Encounter with Polynomial problem solution Our monk, while taking a stroll in the park, stumped upon a polynomial ( A X2 + B X +C ) lying on the ground. The polynomial was dying! Being considerate, our monk tried to talk and revive the polynomial. The polynomial said:
I have served my purpose, and shall not live anymore. Please fulfill my dying wish. Find me the least non-negative integer Xo, that shall make my value atleast K i.e., A Xo2 + B Xo + C >= K .
Help our Monk fulfill the polynomial’s dying wish!
HackerEarth Monk's Encounter with Polynomial problem solution

HackerEarth Monk’s Encounter with Polynomial problem solution.

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int test;
cin>>test;
while(test--)
{
ll A,B,C,K;
cin>>A>>B>>C>>K;
ll lb=0, ub =100000;
ll ans = -1;
while(lb<=ub)
{
ll mid = (lb+ub)/2;
ll val = A*(mid*mid) + B*mid + C;
if(val >= K){
ans = mid;
ub = mid-1;
}
else
lb = mid+1;
}
cout<<ans<<endl;
}
return 0;
}

Second solution

#include<bits/stdc++.h>

using namespace std;

long long a,b,c;

long long f(long long x)
{
return a*x*x + b*x + c;
}

int main()
{
int t;
long long k;
cin>>t;
assert(1 <= t && t <= 100000);
while(t--)
{
cin>>a>>b>>c;
cin>>k;
assert(1 <= a && a <= 1000000);
assert(1 <= b && b <= 1000000);
assert(1 <= c && c <= 1000000);
assert(1 <= k && k <= (long long)1e10);
int ans = 100000000;
int l = 0 , h = -b/(2*a) , m;
while(l <= h)
{
m = (l+h)/2;
if(f(m) >= k)
{
ans = min(ans,m);
l = m + 1;
}
else
{
h = m - 1;
}
}
l = max(0LL,-b/(2*a)) , h = 10000000;
while(l <= h)
{
m = (l+h)/2;
if(f(m) >= k)
{
ans = min(ans,m);
h = m - 1;
}
else
{
l = m + 1;
}
}
cout<<ans<<endl;
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes