Find the index of the left pointer after possible moves in the array | C ++ program to find the index of the left pointer ; Function that returns the index of the left pointer ; there 's only one element in the array ; initially both are at end ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getIndex ( int a [ ] , int n ) { if ( n == 1 ) return 0 ; int 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 ; } int main ( ) { int a [ ] = { 2 , 7 , 9 , 8 , 7 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getIndex ( a , n ) ; return 0 ; }