Skip to content

Fix first-window maximum in Max Subarray Sum reference - #329

Open
frezcirno wants to merge 1 commit into
AlphaGPU:mainfrom
frezcirno:fix/max-subarray-first-window
Open

frezcirno wants to merge 1 commit into
AlphaGPU:mainfrom
frezcirno:fix/max-subarray-first-window

Conversation

@frezcirno

Copy link
Copy Markdown

The Max Subarray Sum reference loses the first window when it is the unique maximum. For example, input=[9, -8], N=2, window_size=1 currently produces -8 instead of 9, so a correct solution can be rejected.

max_sum = current_sum aliases the same scalar Tensor. The first in-place sliding-window update changes both values before torch.max can preserve the original maximum. Clone the initial sum to keep it independent. Add functional regressions for positive, negative, and multi-element first-window maxima, plus the single-window boundary (window_size == N).

Validation:

  • Example, 10 functional cases, and the N=50000/window_size=25000 performance case all match an independent Python integer sliding-window oracle on CPU and CUDA (A800, PyTorch 2.5.1+cu121), with three calls per case.
  • The unmodified reference fails the three new first-window regressions on both devices; the fixed reference passes.
  • python scripts/validate_challenges.py: 101 challenges, zero errors; two existing warnings about unrelated challenge directories/starters.
  • Black 24.1.1, isort 5.13.2, flake8 7.0.0 pass for the changed file; git diff --check passes.

@frezcirno
frezcirno marked this pull request as ready for review September 29, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant