Minimize steps required to reach the value N | 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 ; Given value of N
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 ) ;