Given an array of size . Move two pointers, one from the left side and one from the right side of the array, a pointer will only move forward if the sum of all the numbers it has already gone through is less than the sum of the numbers the other pointer has gone through. Continue the process while left pointer is less than the right pointer or no move is possible. Print the position of the left pointer in the end.

**Note**: 0-based indexing is considered for the array.

**Examples:**

Input:arr[] = {2, 7, 9, 8, 7}

Output:2

Initial position : ptrL = 0, ptrR = 4 with sum 2 and 7 respectively

Move 1 : ptrL = 1, ptrR = 4 with sum 9 and 7

Move 2 : ptrL = 1, ptrR = 3 with sum 9 and 15

Move 3 : ptrL = 2, ptrR = 3 with sum 18 and 7

Input:arr[] = {1, 2, 3, 1, 2}

Output:1

**Approach:** An efficient approach is to move from the left and from the right at the same time and maintaining the running sum for both the pointers.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java program to find the index of the left pointer ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns the index of the left pointer ` `static` `int` `getIndex(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// there's only one element in the array ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `// initially both are at end ` ` ` `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; ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `a[] = { ` `2` `, ` `7` `, ` `9` `, ` `8` `, ` `7` `}; ` ` ` ` ` `int` `n =a.length; ` ` ` ` ` `System.out.println ( getIndex(a, n)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the ` `# index of the left pointer ` ` ` `# Function that returns the ` `# index of the left pointer ` `def` `getIndex(a, n): ` ` ` ` ` `# there's only one element ` ` ` `# in the array ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `0` ` ` ` ` `# initially both are at end ` ` ` `ptrL ` `=` `0` ` ` `ptrR ` `=` `n` `-` `1` ` ` `sumL ` `=` `a[` `0` `] ` ` ` `sumR ` `=` `a[n` `-` `1` `] ` ` ` ` ` `while` `(ptrR ` `-` `ptrL > ` `1` `) : ` ` ` `if` `(sumL < sumR) : ` ` ` `ptrL ` `+` `=` `1` ` ` `sumL ` `+` `=` `a[ptrL] ` ` ` ` ` `elif` `(sumL > sumR) : ` ` ` `ptrR ` `-` `=` `1` ` ` `sumR ` `+` `=` `a[ptrR] ` ` ` ` ` `else` `: ` ` ` `break` ` ` `return` `ptrL ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[ ` `2` `, ` `7` `, ` `9` `, ` `8` `, ` `7` `] ` ` ` ` ` `n ` `=` `len` `(a) ` ` ` ` ` `print` `(getIndex(a, n)) ` ` ` `# This code is contributed by ` `# ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`//C# program to find the index of the left pointer ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function that returns the index of the left pointer ` `static` `int` `getIndex(` `int` `[]a, ` `int` `n) ` `{ ` ` ` `// there's only one element in the array ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` ` ` `// initially both are at end ` ` ` `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; ` `} ` ` ` `// Driver code ` ` ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `[]a = { 2, 7, 9, 8, 7 }; ` ` ` ` ` `int` `n =a.Length; ` ` ` ` ` `Console.WriteLine( getIndex(a, n)); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the index ` `// of the left pointer ` ` ` `// Function that returns the index ` `// of the left pointer ` `function` `getIndex(` `$a` `, ` `$n` `) ` `{ ` ` ` `// there's only one element ` ` ` `// in the array ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `0; ` ` ` ` ` `// initially both are at end ` ` ` `$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` `; ` `} ` ` ` `// Driver code ` `$a` `= ` `array` `( 2, 7, 9, 8, 7 ); ` ` ` `$n` `= ` `count` `(` `$a` `); ` ` ` `echo` `getIndex(` `$a` `, ` `$n` `); ` ` ` `// This code is contributed by anuj_67.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Minimum index i such that all the elements from index i to given index are equal
- Count number of 1s in the array after N moves
- Maximize Sum possible from an Array by the given moves
- Count of contiguous subarrays possible for every index by including the element at that index
- Check if it is possible to reach to the index with value K when start index is given
- Find the Mth element of the Array after K left rotations
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Number of triangles after N moves
- Check if the robot is within the bounds of the grid after given moves
- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Count possible moves in the given direction in a grid
- Find the minimum number of rectangles left after inserting one into another
- Queries for number of distinct elements from a given index till last index in an array
- Find an index such that difference between product of elements before and after it is minimum
- Find the index which is the last to be reduced to zero after performing a given operation
- Most frequent element in Array after replacing given index by K for Q queries
- Number of chocolates left after k iterations
- Position after taking N steps to the right and left in an alternate manner
- Find minimum moves to reach target on an infinite line
- Find the minimum number of preprocess moves required to make two strings equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.