Eating Candies (Codeforces 1669F)
Tags: Greedy, two pointers

There are $$$n$$$ candies put from left to right on a table. The candies are numbered from left to right. The $$$i$$$-th candy has weight $$$w_i$$$. Alice and Bob eat candies. Alice can eat any number of candies from the left (she can't skip candies, she eats them in a row). Bob can eat any number of candies from the right (he can't skip candies, he eats them in a row). Of course, if Alice ate a candy, Bob can't eat it (and vice versa).They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?

Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the number of candies on the table.The second line of each test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \leq w_i \leq 10^4$$$) — the weights of candies from left to right.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

Output: For each test case, print a single integer — the maximum number of candies Alice and Bob can eat in total while satisfying the condition.

Note: For the first test case, Alice will eat one candy from the left and Bob will eat one candy from the right. There is no better way for them to eat the same total amount of weight. The answer is $$$2$$$ because they eat two candies in total.For the second test case, Alice will eat the first three candies from the left (with total weight $$$7$$$) and Bob will eat the first three candies from the right (with total weight $$$7$$$). They cannot eat more candies since all the candies have been eaten, so the answer is $$$6$$$ (because they eat six candies in total).For the third test case, there is no way Alice and Bob will eat the same non-zero weight so the answer is $$$0$$$.For the fourth test case, Alice will eat candies with weights $$$[7, 3, 20]$$$ and Bob will eat candies with weights $$$[10, 8, 11, 1]$$$, they each eat $$$30$$$ weight. There is no better partition so the answer is $$$7$$$.

Sample Inputs:
4
3
10 20 10
6
2 1 4 2 4 1
5
1 2 4 8 16
9
7 3 20 5 15 1 11 8 10

Sample Outputs:
2
6
0
7

Solution (Python):
import sys
MOD = 10 ** 9 + 7
INF = int(1e+18)
DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
input = lambda : sys.stdin.readline().rstrip()

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    (A, B) = (0, 0)
    (l, r) = (0, n - 1)
    while l <= r:
        if A <= B:
            A += a[l]
            l += 1
        else:
            B += a[r]
            r -= 1
        if A == B:
            ans = l + (n - r - 1)
    print(ans)
for _ in range(int(input())):
    solve()

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
    n = int(input())
    arr = deque(list(map(int, input().split())))
    (alice, bob) = (arr.popleft(), 0)
    (alice_count, bob_count) = (1, 0)
    answer = 0
    while arr:
        if alice > bob:
            bob += arr.pop()
            bob_count += 1
        elif bob > alice:
            alice += arr.popleft()
            alice_count += 1
        else:
            answer = alice_count + bob_count
            alice += arr.popleft()
            alice_count += 1
    if alice == bob:
        answer = alice_count + bob_count
    print(answer)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline
t = int(input())
for _ in range(t):
    n = int(input())
    arr = deque(list(map(int, input().split())))
    (alice, bob) = (arr.popleft(), 0)
    (alice_count, bob_count) = (1, 0)
    answer = 0
    while arr:
        if alice > bob:
            bob += arr.pop()
            bob_count += 1
        elif bob > alice:
            alice += arr.popleft()
            alice_count += 1
        else:
            answer = alice_count + bob_count
            alice += arr.popleft()
            alice_count += 1
    if alice == bob:
        answer = alice_count + bob_count
    print(answer)