Given N, print the sequence of a minimum number of steps in which N can be obtained starting from 0 using addition or subtraction of the step number.
Note: At each step we can add or subtract a number equal to the step number from the current position. For example, at step 1 we can add 1 or -1. Similarily at step 2 we add 2 or -2 and so on.
Below diagram shows all possible positions that can be reached from 0 in 3 steps by performing the specified operations.
Input: n = -4 Output: Minimum number of Steps: 3 Step sequence: 1 -2 -3 Explanation: Step 1: At step 1 we add 1 to move from 0 to 1. Step 2: At step 2 we add (-2) to move from 1 to -1. Step 3: At step 3 we add (-3) to move from -1 to -4. Input: n = 11 Output: Minimum number of steps = 4 Step sequence: 1 -2 3 4 5
Approach: The approach to solve the above problem is to mark the step numbers where we have to subtract or add where if N is positive or negative respectively. If N is positive, add numbers at every step, until the sum exceeds N. Once the sum exceeds N, check if sum-N is even or not. If sum-N is even, then at step number (sum-N)/2, subtraction is to be done. If sum-N is an odd number, then check if the last step at which sum exceeded N was even or odd. If it was odd, perform one more step else perform two steps. If sum = N at any step, then additin or subtraction at every step will give the answer.
Let N = 11, then 1+2+3+4+5=15 exceeds 11. Subtract 15-11 to get 4, which is equivalent to performing subtraction at step 2. Hence the sequence of steps is 1 -2 3 4 5
Let N=12, then 1+2+3+4+5=15 exceeds 11. Subtract 15-12 to get 3, which cannot be performed at any step. So add two more steps, one is the 6th step and 7th step. The target is to make sum-N even, so perform addition at 6th step and subtraction at 7th step, which combines to subtract 1 from the sum. Now sum-N is even, 14-12=2 which is equivalent to performing subtraction at step 1. Hence the sequence of steps are -1 2 3 4 5 6 -7
Let N=20, then 1+2+3+4+5+6 exceeds 20. Subtract 21-20 to get 1, so add 7 to 21 to get 28. Performing addition at next step will do as (sum-n) is odd. sum-N gives 8 which is equivalent to performing subtraction at step 4. Hence the sequence of steps is 1 2 3 -4 5 6 7.
Below is the illustration of the above approach:
7 Minimum number of Steps: 7 Step sequence:1 2 3 -4 5 6 7
Time Complexity : O(sqrt(N))
Auxiliary Space : O(sqrt(N))
Note: sum = i*(i+1)/2 is equivalent or greater than N, which gives i as sqrt(N).
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.
- Range queries for alternatively addition and subtraction on given Array
- Number of steps to sort the array by changing order of three elements in each step
- Minimum sum obtained by choosing N number from given N pairs
- Minimum steps to reach target by a Knight | Set 2
- Find minimum number of steps to reach the end of String
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum number of steps to make all elements of array equal
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Subtraction of the alternate nodes of Linked List
- Maximum array sum that can be obtained after exactly k changes
- Find the final number obtained after performing the given operation
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
- Maximum contiguous decreasing sequence obtained by removing any one element
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Find the maximum component size after addition of each edge to the graph
- Print the DFS traversal step-wise (Backtracking also)
- Count ways to reach the Nth stair using any step from the given array
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Count ways to reach end from start stone with at most K jumps at each step
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.