Skip to content

Conversation

@singhc7
Copy link
Contributor

@singhc7 singhc7 commented Jan 19, 2026

Fixes #7209

This PR introduces a new package prefixsum as requested, providing a robust foundation for prefix-based algorithms.

Changes:

  1. PrefixSum.java (1D): Implements standard prefix sum for efficient range queries ($O(1)$).
  2. PrefixSum2D.java: Implements 2D prefix sum for efficient sub-matrix sum queries using the inclusion-exclusion principle.

Key Features:

  • Overflow Protection: Both implementations internally use long[] to store sums, preventing integer overflow when the sum of elements exceeds Integer.MAX_VALUE.
  • Comprehensive Testing: Added JUnit 5 tests covering:
    • Basic range queries.
    • Edge cases (single element, empty arrays, mixed signs).
    • Boundary checks (invalid indices, rectangular matrices).
    • Large number overflow scenarios.
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Jan 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.08%. Comparing base (1b9373e) to head (c92da2d).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7220      +/-   ##
============================================
+ Coverage     79.06%   79.08%   +0.02%     
- Complexity     6951     6971      +20     
============================================
  Files           780      782       +2     
  Lines         22921    22947      +26     
  Branches       4506     4512       +6     
============================================
+ Hits          18122    18148      +26     
  Misses         4077     4077              
  Partials        722      722              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@singhc7 singhc7 marked this pull request as ready for review January 19, 2026 04:26
@DenizAltunkapan DenizAltunkapan enabled auto-merge (squash) January 19, 2026 10:13
@DenizAltunkapan DenizAltunkapan merged commit 109ed2e into TheAlgorithms:master Jan 19, 2026
7 checks passed
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.

[FEATURE REQUEST] <title>Add prefix sum algorithm category in java

3 participants