 < Back to forum

### 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.

Preview

##### Enter your comment details below:

Preview

sorry sorry, i have solved this one. its is showing correct after i removed the 'double n' line.

Samrat_De last updated on April 7, 2019, 6:34 p.m.

##### Instruction to write good question
1. 1. Write a title that summarizes the specific problem
2. 2. Pretend you're talking to a busy colleague
3. 3. Spelling, grammar and punctuation are important!