Find the index of the left pointer after possible moves in the array | Function that returns the index of the left pointer ; There 's only one element  in the array ; Initially both are at end ; Driver code
function getIndex ( a , n ) { if ( n == 1 ) return 0 ; let ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } let a = [ 2 , 7 , 9 , 8 , 7 ] ; let n = a . length ; document . write ( getIndex ( a , n ) ) ;