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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- Program to print right and left arrow patterns
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Longest subsequence of a number having same left and right rotation
- Taking input from console in Python
- Sum of first N natural numbers by taking powers of 2 as negative number
- Program to multiply two Matrix by taking data from user
- Number of ways to arrange K different objects taking N objects at a time
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Maximize sum of K elements in Array by taking only corner elements
- Min operations to reduce N by multiplying by any number or taking square root
- Count of right shifts for each array element to be in its sorted position
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count all possible paths from top left to bottom right of a mXn matrix
- Reorder an array such that sum of left half is not equal to sum of right half
- Maximum sum path in a matrix from top-left to bottom-right
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
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.