tinycodinglm / cp2 /cp-0003.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
10.8 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation. Tools: write_file(path, content) writes...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-07T00:20:03.537Z

Balanced Ledger Stretch

Topic: Arrays and prefix sums | Difficulty: easy

Problem

A shop records n consecutive daily balance changes a[1..n]. Find the length of the longest contiguous stretch of days whose changes add up to exactly zero. If no non-empty stretch sums to zero, the answer is 0.

Input

The first line contains n. The second line contains n integers.

Output

Print the maximum length of a contiguous subarray with sum 0, or 0 if none exists.

Constraints

  • 1 <= n <= 2*10^5
  • |a[i]| <= 10^9

Examples

Example 1

Input:

markdown
6
1 -1 3 2 -2 -3

Output:

markdown
6

Note: The whole array sums to 0.

Example 2

Input:

markdown
3
1 2 3

Output:

markdown
0

Example 3

Input:

markdown
5
4 0 7 7 -7

Output:

markdown
2

Note: Both [0] (length 1) and [7,-7] (length 2) sum to zero; the longer one wins.

Solve this task. Write solutions in C#, JavaScript (Node.js), Python 3, test what you can run on the examples, and send back the final programs. Final answer: code only, no explanation.

Assistant 2026-11-07T00:20:05.511Z

Writing the C# solution and testing it on the examples.

Assistant 2026-11-07T00:20:09.870Z

Saving the example inputs.

3 tool calls (write_file)
Assistant 2026-11-07T00:20:18.432Z

Compiling the C# solution.

Assistant 2026-11-07T00:20:22.635Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-07T00:20:31.785Z

Cross-checking with the JavaScript (Node.js) version.

Assistant 2026-11-07T00:20:35.832Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-07T00:20:44.670Z

Cross-checking with the Python 3 version.

Assistant 2026-11-07T00:20:48.561Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-07T00:20:57.987Z

All solutions below passed every example.

C#

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine().Trim());
        string[] a = Console.ReadLine().Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
        var first = new Dictionary<long, int>();
        first[0] = 0;
        long s = 0;
        int best = 0;
        for (int i = 1; i <= n; i++)
        {
            s += long.Parse(a[i - 1]);
            if (first.TryGetValue(s, out int f)) best = Math.Max(best, i - f);
            else first[s] = i;
        }
        Console.WriteLine(best);
    }
}

JavaScript (Node.js)

javascript
const t = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x.length).map(Number);
const n = t[0];
const first = new Map();
first.set(0, 0);
let s = 0, best = 0;
for (let i = 1; i <= n; i++) {
  s += t[i];
  if (first.has(s)) best = Math.max(best, i - first.get(s));
  else first.set(s, i);
}
console.log(best);

Python 3

python
import sys
def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    first = {0: 0}
    s = 0
    best = 0
    for i in range(1, n + 1):
        s += int(data[i])
        if s in first:
            best = max(best, i - first[s])
        else:
            first[s] = i
    print(best)
main()