< Back to forum

CO92SUBW

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.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

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. 0    Reply    Upvote   

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!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.