Question link: https://www.codechef.com/COOK92B/problems/CO92SUBW
In this question what i did is, atfirst found out the expression of t(n) for the series: 1,3,6,10,15... it is coming: n(n+1)/2. Now equating it with x(input given), i get the n(maybe float/int). If n is float,after converting it into int, i am calculating n(n+1)/2 and (n+1)(n+2)/2, and x must lie between this 2 value. Now i will check which will take shorter time i.e going from n(n+1)/2 to x or going from (n+1)(n+2)/2 to x.
this code is giving correct output for the test cases, but showing wrong answer in codechef, can u please tell me where i am wrong..??
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { long long int x,r,min,p,q; double n; cin>>x; n=((int)pow((1+8*x),0.5)-1)/2.0; r=(int)n; if(r!=n) { p=r*(r+1)/2; q=(r+1)*(r+2)/2; min=((r+x-p)<(r+1+q-x))?(r+x-p):(r+1+q-x); } else min=r; cout<<min<<"\n"; } return 0; }
Asked by: Samrat_De on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.