Skip to content

Commit 5ef7b3d

Browse files
committed
feat: add subarray sum equals k algorithm
1 parent 21f5615 commit 5ef7b3d

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/general/permutations/heap.rs)
129129
* [Naive](https://github.com/TheAlgorithms/Rust/blob/master/src/general/permutations/naive.rs)
130130
* [Steinhaus Johnson Trotter](https://github.com/TheAlgorithms/Rust/blob/master/src/general/permutations/steinhaus_johnson_trotter.rs)
131+
* [Subarray Sum Equals K](https://github.com/TheAlgorithms/Rust/blob/master/src/general/subarray_sum_equals_k.rs)
131132
* [Two Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/general/two_sum.rs)
132133
* Geometry
133134
* [Closest Points](https://github.com/TheAlgorithms/Rust/blob/master/src/geometry/closest_points.rs)

src/general/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod kadane_algorithm;
77
mod kmeans;
88
mod mex;
99
mod permutations;
10+
mod subarray_sum_equals_k;
1011
mod two_sum;
1112

1213
pub use self::convex_hull::convex_hull_graham;
@@ -22,4 +23,5 @@ pub use self::mex::mex_using_sort;
2223
pub use self::permutations::{
2324
heap_permute, permute, permute_unique, steinhaus_johnson_trotter_permute,
2425
};
26+
pub use self::subarray_sum_equals_k::subarray_sum_equals_k;
2527
pub use self::two_sum::two_sum;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::collections::HashMap;
2+
3+
/// Counts the number of contiguous subarrays that sum to exactly k.
4+
///
5+
/// # Parameters
6+
///
7+
/// - `nums`: A slice of integers
8+
/// - `k`: The target sum
9+
///
10+
/// # Returns
11+
///
12+
/// The number of contiguous subarrays with sum equal to k.
13+
///
14+
/// # Complexity
15+
///
16+
/// - Time: O(n)
17+
/// - Space: O(n)
18+
19+
pub fn subarray_sum_equals_k(nums: &[i32], k: i32) -> i32 {
20+
let mut prefix_sum_count: HashMap<i64, i32> = HashMap::new();
21+
prefix_sum_count.insert(0, 1);
22+
23+
let mut prefix_sum: i64 = 0;
24+
let mut count = 0;
25+
26+
for &num in nums {
27+
prefix_sum += num as i64;
28+
let target = prefix_sum - k as i64;
29+
30+
if let Some(&freq) = prefix_sum_count.get(&target) {
31+
count += freq;
32+
}
33+
34+
*prefix_sum_count.entry(prefix_sum).or_insert(0) += 1;
35+
}
36+
37+
count
38+
}
39+
40+
#[cfg(test)]
41+
mod test {
42+
use super::*;
43+
44+
#[test]
45+
fn test_basic() {
46+
assert_eq!(subarray_sum_equals_k(&[1, 1, 1], 2), 2);
47+
assert_eq!(subarray_sum_equals_k(&[1, 2, 3], 3), 2);
48+
}
49+
50+
#[test]
51+
fn test_single_element() {
52+
assert_eq!(subarray_sum_equals_k(&[1], 1), 1);
53+
assert_eq!(subarray_sum_equals_k(&[1], 0), 0);
54+
}
55+
56+
#[test]
57+
fn test_empty() {
58+
assert_eq!(subarray_sum_equals_k(&[], 0), 0);
59+
assert_eq!(subarray_sum_equals_k(&[], 5), 0);
60+
}
61+
62+
#[test]
63+
fn test_negative_numbers() {
64+
assert_eq!(subarray_sum_equals_k(&[-1, -1, 1], 0), 1);
65+
assert_eq!(subarray_sum_equals_k(&[1, -1, 0], 0), 3);
66+
}
67+
68+
#[test]
69+
fn test_no_match() {
70+
assert_eq!(subarray_sum_equals_k(&[1, 2, 3], 10), 0);
71+
}
72+
73+
#[test]
74+
fn test_multiple_matches() {
75+
assert_eq!(subarray_sum_equals_k(&[1, 0, 1, 0, 1], 1), 8);
76+
}
77+
}

0 commit comments

Comments
 (0)