Given three integers N, A, and B. A person is standing at 0-th coordinate and moves A steps to the right in the first step, B steps to the left at the second step and so on.. The task is to find out at which coordinate he will be after N steps.
Input: N = 3, A = 5 and B = 2
5 to the right, 2 to the left and 5 to the right, hence the person will end at 8.
Input: N = 5, A = 1 and B = 10
Approach: Since the person takes the odd number step to right and even number of steps to the left, we have to find out the number difference in steps in either direction. Hence the formula obtained will be thus:
[((n+1)/2)*a – (n/2)*b]
Below is the implementation of the above approach:
- Taking input from console in Python
- Program to multiply two Matrix by taking data from user
- Sum of first N natural numbers by taking powers of 2 as negative number
- Number of ways to arrange K different objects taking N objects at a time
- Alternate Primes till N
- Alternate Fibonacci Numbers
- Check if it is possible to move from (0, 0) to (X, Y) in exactly K steps
- Check if it is possible to move from (0, 0) to (x, y) in N steps
- Sum of Fibonacci Numbers with alternate negatives
- Alternate XOR operations on sorted array
- Sum of series with alternate signed squares of AP
- Find the number of stair steps
- Largest number N which can be reduced to 0 in K steps
- Maximum money that can be withdrawn in two steps
- Print steps to make a number in form of 2^X - 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.