14,920,714 members
1.00/5 (1 vote)
See more:
It's a lockdown. You’re bored in your house and are playing golf in the hallway.

The hallway has N+2 tiles numbered from 0 to N+1 from left to right. There is a hole on tile number x. You hit the ball standing on tile 0. When you hit the ball, it bounces at lengths of k, i.e. the tiles covered by it are 0,k,2k,…, and so on until the ball passes tile N+1.

If the ball doesn't enter the hole in the first trial, you try again but this time standing on the tile N+1. When you hit the ball, it bounces at lengths of k, i.e. the tiles covered by it are (N+1),(N+1−k),(N+1−2k),…, and so on until the ball passes tile 0.

Find if the ball will enter the hole, either in its forward journey or backward journey.

Note: The input and output of this problem are large, so prefer using fast input/output methods.

Input
The first line contains an integer T, the number of test cases. Then the test cases follow.
The only line of each test case contains three integers N,x,k.
Output
Output in a single line, the answer, which should be "YES" if the ball enters the hole either in the forward or backward journey and "NO" if not.

You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical).

Constraints
1≤T≤105
1≤x,k≤N≤109
Subtask #1 (10 points): N≤102
Subtask #2 (90 points): original constraints

Sample Input
3
5 4 2
5 3 2
5 5 2

Sample Output
YES
NO
NO

What I have tried:

Python
```t = int(input())
arr = []
for _ in range(t):
n, x, k = input().split()
i = 0
while int(k) * i <= int(x):
arr.append(int(k) * i)
i += 1
j = 0

while (int(n) + 1) - int(k) * j >= int(x):
arr.append(int((int(n) + 1) - int(k) * j))
j += 1
if int(x) not in arr:
print("NO")
else:
print("YES")```

my code is displaying the right answer for sample input but not for other subtasks :(
Posted
Updated 19-May-21 8:09am
Muskan Verma 2021 8-May-21 4:59am

URL for this ques is
https://www.codechef.com/MAY21C/problems/LKDNGOLF
Richard MacCutchan 8-May-21 5:20am

Convert your input values to `int` types as you read them, not in every calculation. That will affect the time it takes.
Muskan Verma 2021 8-May-21 7:45am

ok sir, thank you

Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

How do you expect to learn how to do this, if every time your code fails you ask us to fix it for you? Ask yourself: is there any point in my trying to do "code challenges" if someone else does the complicated bit?
Muskan Verma 2021 8-May-21 8:07am

Thanks a lot, sir but, I have tried it on the debugger, and it's not throwing any error!
can u please correct this code of mine?
OriginalGriff 8-May-21 8:34am

Debuggers don't throw errors: they let you look at what your code is doing while it is running, see what it's variables contain, work out what it should be doing, and compare that to what it actually does.

You then use that information to fix - or at least change - your code to make it work closer to what you intended.

Compilers throw errors when you type the wrong syntax: applications throw errors when they go wildly wrong. Debuggers help you find out why - but they only start working once code compiles!

So whatever you used, either it isn't a debugger, or you didn't use it properly.
Try again.
Dave Kreskowiak 8-May-21 20:30pm

Debuggers are not there to debug your code for you. They can't do that. They exist to help you understand the code better and see what it's doing, one statement at a time if you want.

Solution 2

Quote:
time limit exceed.

This usually mean that your code is too simple minded and that is at the expense of runtime.
Think of a multiplication x*y, you can add x, y times to a total, it works, but it is not efficient.
You have the same kind of problem with your algorithm.
-----
As programmer, your job is also to debug your code.
Quote:
my code is displaying the right answer for sample input but not for other subtasks

Rather simple: Either your algorithm is wrong or incomplete, either your do not translate correctly your algorithm.

First, you need to check if the code is correct but your code is a black box, the debugger allow you to execute your code step by step and to check every variable as your code execute. At every step, check if variables evolve as expected.

If the code is correct, this means that the algorithm is not, or incomplete.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
v3
CPallini 13-May-21 7:25am

5.
Patrice T 13-May-21 7:32am

Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 116 Greg Utas 85 Richard MacCutchan 66 A-Games 43 Patrice T 41
 OriginalGriff 2,946 Richard MacCutchan 1,069 Richard Deeming 1,045 CPallini 835 Patrice T 550

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900