Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • 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
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Ratio – TIE BREAKER problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarthRatio – TIE BREAKER problem solution You are given N ratios in the form of A and B that is represented as A/B. The values of A and B are represented as double data type values. The values of B are incorrect. The actual values of B are B + R. You know the actual sum of all the ratios that is available in variable K.
 
 
HackerEarth Ratio - TIE BREAKER problem solution

 

 

HackerEarth Ratio – TIE BREAKER problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll n;
double c,t,lo,hi,mid,d[1005],s[1005];
int main()
{
freopen("samp.txt","r",stdin);
freopen("sout.txt","w",stdout);
ll i,j,k=0;
cin>>n;
lo=-1000000.0;
for(i=1;i<=n;i++)
{
cin>>d[i]>>s[i];
lo=max(lo,-1.0*s[i]);
}
cin>>t;
hi=10000000.0;
k=0;
while(k<=5000&&lo<=hi)
{
mid=(lo+hi)/2.0;
k++;
double cur=0.0;
for(i=1;i<=n;i++)
{
cur+=(d[i]/(s[i]+mid+0.0));
}
if(cur>=t)
lo=mid;
else
hi=mid;
}
cout<<fixed<<setprecision(8)<<mid;
return 0;
}
 

Second solution

#include<bits/stdc++.h>
#define eps 0.000001
using namespace std;
int n;
double a[1005],b[1005];
double f(double r)
{
double val=0.0;
for(int i=0;i<n;i++)
val+=a[i]/(b[i]+r);
return val;
}
int main()
{
int col;
cin>>n>>col;
assert(n>=1 && n<=1000);
assert(col==2);
double l=-2000.0,r=2000.0;
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
assert(a[i]>=1 && a[i]<=1000);
assert(abs(b[i])<=1000);
l=max(l,-1.0*b[i]);
}
double k,ans=0;l+=1e-7;
cin>>k;
assert(k>=1 && k<=1e6);
while(l<=r)
{
double mid=(l+r)/2.0;
double val=f(mid);
if(val>=k){ans=mid;l=mid+1e-7;}
else if(val<k) {r=mid-1e-7;}
}
cout<<fixed<<setprecision(7)<<ans<<"n";
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

Practice

  • Java
  • C++
  • C

Follow US

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