Skip to content
TheChatArchiveThe Chat Archive
DeepSeekDeepseek v3.2· deepseekHugging Face Frontier Reasoning Traces

Akhil gives Ramesh an array A of length N. Then he asks him to determine if there exists…

  1. User

    Akhil gives Ramesh an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero. Formally, find an i, such that, A1+A2...Ai-1 =Ai+1+Ai+2...An. Input Format

    The first line contains T, the number of test cases. For each test case, the first line contains N, the number of elements in the array A. The second line for each test case contains N space-separated integers, denoting the array A.

    Output Format

    For each test case print YES if there exists an element in the array, such that the sum of the elements on its left is equal to the sum of the elements on its right; otherwise print NO.

    Constraints

    1≤T≤10

    1≤N≤10^5

    1≤Ai ≤2×10^4

    1≤i≤N

    SAMPLE INPUT 2 3 1 2 3 4 1 2 3 3

    SAMPLE OUTPUT NO YES

    Explanation

    For the 1st test case, no such index exists.

    For the 2nd test case, A[1]+A[2]=A[4], therefore index 3 satisfies the given conditions.

  2. DeepSeek Assistant

    We are asked: For each test case, determine if there exists an index i such that sum of elements to the left (A[0]..A[i-2]? Wait careful: Given 1-indexed: A1+A2+...+A_{i-1} = A_{i+1}+...+A_{N}. For i=1, left sum = 0 (no elements), right sum = A2+...+AN. For i=N, right sum = 0. So we need to check.

    We can compute total sum S. Then iterate i from 1 to N, maintaining left sum L and right sum = S - L - A[i]. If L == S - L - A[i] then answer YES. This is O(N) per test case.

    Constraints: N up to 1e5, T up to 10, so overall up to 1e6, fine.

    Edge cases: large sums up to N*2e4 = 2e9, fits in 64-bit. So use long long.

    Implementation: read T. For each test case: read N, then read array of N ints. Compute total sum. Initialize left sum = 0. Loop i from 0 to N-1 (0-indexed). For each i, if left sum == total - left sum - A[i] then YES. Else update left sum += A[i]. After loop, if no such i, NO.

    That's it.

    Let's test with sample: Test1: [1,2,3] total=6. i0: left=0, right=6-0-1=5, 0!=5; left=1. i1: left=1, right=6-1-2=3, 1!=3; left=3. i2: left=3, right=6-3-3=0, 3!=0 => NO. Test2: [1,2,3,3] total=9. i0: left=0, right=9-0-1=8; no; left=1. i1: left=1, right=9-1-2=6; no; left=3. i2: left=3, right=9-3-3=3; yes => YES.

    Thus correct.

    We must output YES/NO for each test case.

    Time complexity: O(N) per test.

    Implement with C++.

    Potential pitfalls: using int for sum may overflow, use long long.

    The problem statement: "1≤Ai ≤2×10^4", N up to 1e5 => sum up to 2e9, within 32-bit signed? Actually 2e9 is near max of signed 32-bit (2^31-1 = 2.147e9). Could exceed if N=1e5, each 20000 => 2e9 exactly, but safe within signed 32-bit if we treat as long. But to be safe, use long long.

    Implementation:

    #include #include

    using namespace std;

    int main() { int T; cin >> T; while (T--) { int N; cin >> N; vector A(N); long long total = 0; for (int i=0; i<N; ++i) { cin >> A[i]; total += A[i]; } long long left = 0; bool found = false; for (int i=0; i<N; ++i) { long long right = total - left - A[i]; if (left == right) { found = true; break; } left += A[i]; } if (found) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }

    That's it.

    We could also use prefix sums but same.

    I think this is correct.

    Now, the user asks "solve this problem". Provide explanation and code. They want final answer: solution with code in C++.

    Thus produce final output: explanation and code.

    We should include brief explanation of algorithm: compute total sum, iterate while maintaining left sum; at each index, compute right sum = total - left - current; check equality.

    Thus answer. ```cpp

    #include #include

    using namespace std;

    int main() { int T; cin >> T; while (T--) { int N; cin >> N; vector A(N); long long total = 0; for (int i = 0; i < N; ++i) { cin >> A[i]; total += A[i]; }

        long long left = 0;
        bool found = false;
        for (int i = 0; i < N; ++i) {
            long long right = total - left - A[i];
            if (left == right) {
                found = true;
                break;
            }
            left += A[i];
        }
    
        if (found)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
    

    }