Skip to content
Programmingoneonone - Logo
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Computer System Architecture
    • Microprocessor
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone - Logo
Programmingoneonone

HackerEarth Monk’s Encounter with Polynomial problem solution

YASH PAL, 31 July 202415 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy
  • DMCA

Practice

  • Java
  • C++
  • C

Follow US

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