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 7Input: 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 pointerint 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 codeint main(){Ā Ā Ā Ā int a[] = { 2, 7, 9, 8, 7 };Ā
Ā Ā Ā Ā int n = sizeof(a) / sizeof(a[0]);Ā
Ā Ā Ā Ā cout << getIndex(a, n);Ā
Ā Ā Ā Ā return 0;} |
Java
// Java program to find the index of the left pointerĀ
import java.io.*;Ā
class GFG {Ā
Ā
// Function that returns the index of the left pointerstatic 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.. |
Python3
# Python3 program to find the # index of the left pointerĀ
# Function that returns the # index of the left pointerdef 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 codeif __name__ == "__main__":Ā Ā Ā Ā Ā Ā Ā Ā Ā a = [ 2, 7, 9, 8, 7 ]Ā
Ā Ā Ā Ā n = len(a)Ā
Ā Ā Ā Ā print(getIndex(a, n))Ā Ā Ā Ā Ā # This code is contributed by# ChitraNayal |
C#
// C# program to find the index of the left pointerĀ
using System;Ā
class GFG {Ā
Ā
// Function that returns the index of the left pointerstatic 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.. |
PHP
<?php// PHP program to find the index // of the left pointerĀ
// Function that returns the index// of the left pointerfunction 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..?> |
Javascript
<script>Ā
// Javascript program to find // the index of the left pointerĀ
// Function that returns the// index of the left pointerfunction getIndex(a, n){Ā Ā Ā Ā Ā Ā Ā Ā Ā // There's only one elementĀ Ā Ā Ā // in the arrayĀ Ā Ā Ā if(n == 1)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā // Initially both are at endĀ Ā Ā Ā 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;}Ā
// Driver codelet a = [ 2, 7, 9, 8, 7 ];let n = a.length;Ā
document.write(getIndex(a, n));Ā
// This code is contributed by Mayank TyagiĀ Ā Ā Ā Ā </script> |
2
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
