Minimize steps required to reach the value N | Java program for the above approach ; Function to find the minimum steps required to reach N by either moving i steps forward or 1 steps backward ; Stores the required count ; IF total moves required is less than double of N ; Update steps ; Steps required to reach N ; Update steps ; Driver code ; Given value of N
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 ) ; } }