Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Given value of N & B ; Function call to find maximum index the pointer can reach
function maximumIndex ( N , B ) { let i = 0 , j = 1 ; let cnt = 0 ; let sum = Math . floor ( N * ( N + 1 ) / 2 ) ; let flag = false ; while ( cnt < N ) { i += j ; j ++ ; cnt ++ ; if ( i == B ) { flag = true ; break ; } } if ( ! flag ) { document . write ( sum ) ; } else document . write ( sum - 1 ) ; } let N = 4 , B = 6 ; maximumIndex ( N , B ) ;