Minimize steps required to reach the value N
Last Updated :
10 Jun, 2021
Given an infinite number line from the range [-INFINITY, +INFINITY] and an integer N, the task is to find the minimum count of moves required to reach the N, starting from 0, by either moving i steps forward or 1 steps backward in every ith move.
Examples:
Input: N = 18
Output: 6
Explanation:
To reach to the given value of N, perform the operations in the following sequence: 1 – 1 + 3 + 4 + 5 + 6 = 18
Therefore, a total of 6 operations are required.
Input: N = 3
Output: 2
Explanation:
To reach to the given value of N, perform the operations in the following sequence: 1 + 2 = 3
Therefore, a total of 2 operations are required.
Approach: The idea is to initially, keep adding 1, 2, 3 . . . . K, until it is greater than or equal to the required value N. Then, calculate the required number to be subtracted from the current sum. Follow the steps below to solve the problem:
- Initially, increment by K until N is greater than the current value. Now, stop at some position
pos = 1 + 2 + …………. + steps = steps ? (steps + 1) / 2 ? N.
Note: 0 ? pos – N < steps. Otherwise, the last step wasn’t possible.
- Case 1: If pos = N then, ‘steps’ is the required answer.
- Case 2: If pos ? N, then replace any iteration of K with -1.
- By replacing any K with -1, the modified value of pos = pos – (K + 1). Since K ? [1, steps], then pos ? [pos – steps – 1, pos – 2].
- It is clear that pos – step < N. If N < pos – 1, then choose the corresponding K = pos – N – 1 and replace K with -1 and get straight to the point N.
- If N + 1 = pos, only one -1 operation is required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumsteps( int N)
{
int steps = 0;
while (steps * (steps + 1) < 2 * N) {
steps++;
}
if (steps * (steps + 1) / 2 == N + 1) {
steps++;
}
cout << steps;
}
int main()
{
int N = 18;
minimumsteps(N);
}
|
Java
import java.util.*;
class GFG{
static void minimumsteps( int N)
{
int steps = 0 ;
while (steps * (steps + 1 ) < 2 * N)
{
steps++;
}
if (steps * (steps + 1 ) / 2 == N + 1 )
{
steps++;
}
System.out.println(steps);
}
public static void main(String[] args)
{
int N = 18 ;
minimumsteps(N);
}
}
|
Python3
def minimumsteps(N) :
steps = 0
while (steps * (steps + 1 ) < 2 * N) :
steps + = 1
if (steps * (steps + 1 ) / 2 = = N + 1 ) :
steps + = 1
print (steps)
N = 18 ;
minimumsteps(N)
|
C#
using System;
class GFG {
static void minimumsteps( int N)
{
int steps = 0;
while (steps * (steps + 1) < 2 * N) {
steps++;
}
if (steps * (steps + 1) / 2 == N + 1) {
steps++;
}
Console.WriteLine(steps);
}
static public void Main()
{
int N = 18;
minimumsteps(N);
}
}
|
Javascript
<script>
function minimumsteps(N)
{
let steps = 0;
while (steps * (steps + 1) < 2 * N)
{
steps++;
}
if (steps * Math.floor((steps + 1) / 2) == N + 1)
{
steps++;
}
document.write(steps);
}
let N = 18;
minimumsteps(N);
</script>
|
Time CompleNity: O(sqrt(N))
AuNiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...