Skip to content

Conversation

@MNnazrul
Copy link

Description

This PR adds an implementation of the "Subarray Sum Equals K" algorithm to the general module. The algorithm counts the number of contiguous subarrays that sum to exactly k.

Implementation Method:

  • Uses prefix sum technique with HashMap for O(n) time complexity
  • Space complexity: O(n) for the HashMap
  • Handles edge cases including empty arrays, negative numbers, and zero sums

Algorithm Reference:
This is a classic LeetCode problem (Problem 560). The solution uses the prefix sum approach where we track cumulative sums and check if prefix_sum - k exists in our map, which indicates a subarray with sum k exists.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

@MNnazrul MNnazrul requested a review from imp2002 as a code owner November 17, 2025 04:26
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.44%. Comparing base (21f5615) to head (5ef7b3d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #949      +/-   ##
==========================================
+ Coverage   95.39%   95.44%   +0.05%     
==========================================
  Files         332      333       +1     
  Lines       21496    21532      +36     
==========================================
+ Hits        20506    20552      +46     
+ Misses        990      980      -10     

☔ 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.

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.

2 participants