< Back to forum

having trouble in the problem HILLS (february lunchtime 2018, Codechef)

question link: https://www.codechef.com/LTIME57/problems/HILLS

the code is giving WA in codechef, but giving correct output in dev c++. can u please tell me where i am wrong ? 

#include<iostream>
using namespace std;

int main()
{	
	long long int a[103],u,d;
	int t,n,c=0,i;
	cin>>t;
	while(t--)
	{	cout<<"\n";
		cin>>n>>u>>d;
		for(i=0;i<n;i++)
			cin>>a[i];
		i=0;
		while(i<(n-1))
		{	if((a[i+1]==(a[i]+u)) || (a[i+1]==(a[i]-d)) || (a[i+1]==a[i]))
				i++;
			else if((a[i+1]<a[i]) && c==0)
				{	c=1;
					i++;
				}
			else
			    break;
		}
		cout<<"\n"<<(i+1);	
		c=0;	
	}
	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:




2 Answer(s)

avatar

You are considering jumps in pnly those cases in which the jump is equal to U or D..the question states that he can jump upto U height...

consider the case :

5 2 2

1 2 3 2 1

In this case ,he can reach the fifth hill....

Try it with this approach...

Shubham_Gupta last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

avatar

Your output format is wrong. You are printing a newline in the beginning of your output and two newlines between the answers to each test case. You should remove the cout in the first statement of your while loop and give \n after i+1 in your second cout.

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