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 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; } |
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.. |
Python3
# Python3 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 |
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.. |
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.. ?> |
Javascript
<script> Ā
// Javascript 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 Ā Ā Ā Ā 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 code let 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!