给定大小数组 。移动两个指针,一个指针从数组的左侧移出,一个指针从数组的右侧移开,仅当指针已经通过的所有数字的总和小于另一个指针已经经过的数字的总和时,指针才会向前移动通过。当左指针小于右指针或无法移动时,继续执行该过程。最后打印左指针的位置。
注意:数组考虑从0开始的索引。
例子:
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
方法:一种有效的方法是同时从左侧和右侧移动,并保持两个指针的总和。
下面是上述方法的实现:
C++
// C++ program to find the index of the left pointer
#include
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
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
输出:
2