Given an array of size
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++ 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 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 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# 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 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.. ?> |
<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> |
Output
2
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)