From 3b1a6d581055d40a41cfaf6173df09c18db11f79 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Thu, 20 Nov 2025 16:18:55 -0800 Subject: [PATCH 1/5] Add subset_sum dynamic programming algorithm - Implements subset sum problem using DP approach - Includes comprehensive test cases with edge cases - Time complexity: O(n * sum) - Space complexity: O(n * sum) --- .../KB/ArticleDet?ID=3930\n\n/.zshenv" | 1 + src/dynamic_programming/mod.rs | 2 + src/dynamic_programming/subset_sum.rs | 79 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 "\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" create mode 100644 src/dynamic_programming/subset_sum.rs diff --git "a/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" "b/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" new file mode 100644 index 00000000000..c2f3182f53d --- /dev/null +++ "b/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" @@ -0,0 +1 @@ +. "$HOME/.cargo/env" diff --git a/src/dynamic_programming/mod.rs b/src/dynamic_programming/mod.rs index f18c1847479..2d350c6a1d2 100644 --- a/src/dynamic_programming/mod.rs +++ b/src/dynamic_programming/mod.rs @@ -16,6 +16,7 @@ mod optimal_bst; mod rod_cutting; mod snail; mod subset_generation; +mod subset_sum; mod trapped_rainwater; mod word_break; @@ -45,5 +46,6 @@ pub use self::optimal_bst::optimal_search_tree; pub use self::rod_cutting::rod_cut; pub use self::snail::snail; pub use self::subset_generation::list_subset; +pub use self::subset_sum::is_sum_subset; pub use self::trapped_rainwater::trapped_rainwater; pub use self::word_break::word_break; diff --git a/src/dynamic_programming/subset_sum.rs b/src/dynamic_programming/subset_sum.rs new file mode 100644 index 00000000000..0da4a6bc642 --- /dev/null +++ b/src/dynamic_programming/subset_sum.rs @@ -0,0 +1,79 @@ +// Subset Sum Problem in Rust +// Time Complexity: O(n * sum) where n is array length and sum is the target sum +// Space Complexity: O(n * sum) for the DP table +/// Determines if there exists a subset of the given array that sums to the target value. +/// Uses dynamic programming to solve the subset sum problem. +/// +/// # Arguments +/// * arr - A slice of integers representing the input array +/// * required_sum - The target sum to check for +/// +/// # Returns +/// * A boolean indicating whether a subset exists that sums to the target +pub fn is_sum_subset(arr: &[i32], required_sum: i32) -> bool { + let n = arr.len(); + + // Handle edge case where required sum is 0 (empty subset always sums to 0) + if required_sum == 0 { + return true; + } + + // Handle edge case where array is empty but required sum is positive + if n == 0 && required_sum > 0 { + return false; + } + // dp[i][j] stores whether sum j can be achieved using first i elements + let mut dp = vec![vec![false; required_sum as usize + 1]; n + 1]; + // Base case: sum 0 can always be achieved with any number of elements (empty subset) + for i in 0..=n { + dp[i][0] = true; + } + // Base case: with 0 elements, no positive sum can be achieved + for j in 1..=required_sum as usize { + dp[0][j] = false; + } + // Fill the DP table + for i in 1..=n { + for j in 1..=required_sum as usize { + if arr[i - 1] > j as i32 { + // Current element is too large, exclude it + dp[i][j] = dp[i - 1][j]; + } else { + // Either exclude the current element or include it + dp[i][j] = dp[i - 1][j] || dp[i - 1][j - arr[i - 1] as usize]; + } + } + } + dp[n][required_sum as usize] +} +#[cfg(test)] +mod tests { + use super::*; + // Macro to generate multiple test cases for the is_sum_subset function + macro_rules! subset_sum_tests { + ($($name:ident: $input:expr => $expected:expr,)*) => { + $( + #[test] + fn $name() { + let (arr, sum) = $input; + assert_eq!(is_sum_subset(arr, sum), $expected); + } + )* + }; + } + subset_sum_tests! { + // Common test cases + test_case_1: (&[2, 4, 6, 8], 5) => false, + test_case_2: (&[2, 4, 6, 8], 14) => true, + test_case_3: (&[3, 34, 4, 12, 5, 2], 9) => true, + test_case_4: (&[3, 34, 4, 12, 5, 2], 30) => false, + test_case_5: (&[1, 2, 3, 4, 5], 15) => true, + + // Edge test cases + test_case_empty_array_positive_sum: (&[], 5) => false, + test_case_empty_array_zero_sum: (&[], 0) => true, + test_case_zero_sum: (&[1, 2, 3], 0) => true, + test_case_single_element_match: (&[5], 5) => true, + test_case_single_element_no_match: (&[3], 5) => false, + } +} From a2fe7242644452dda021eb7202058d11138c9e81 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi Date: Thu, 20 Nov 2025 16:24:21 -0800 Subject: [PATCH 2/5] Added to DIRECTORY.md --- .../TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" | 1 - DIRECTORY.md | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 "\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" diff --git "a/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" "b/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" deleted file mode 100644 index c2f3182f53d..00000000000 --- "a/\n\nRun 'module avail' to get access to CUDA, Tensorflow and other software, then\n 'module load' your choice. Details:\n https:/sfu.teamdynamix.com/TDClient/255/ITServices/KB/ArticleDet?ID=3930\n\n/.zshenv" +++ /dev/null @@ -1 +0,0 @@ -. "$HOME/.cargo/env" diff --git a/DIRECTORY.md b/DIRECTORY.md index 46bb2a3af9f..e6a35c9e71b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -105,6 +105,7 @@ * [Rod Cutting](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/rod_cutting.rs) * [Snail](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/snail.rs) * [Subset Generation](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/subset_generation.rs) + * [Subset Sum](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/subset_sum.rs) * [Trapped Rainwater](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/trapped_rainwater.rs) * [Word Break](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/word_break.rs) * Financial From 7b50f2130dfd831c3738cdb204e84541826f68dd Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Fri, 28 Nov 2025 00:03:32 -0800 Subject: [PATCH 3/5] Update src/dynamic_programming/subset_sum.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/dynamic_programming/subset_sum.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dynamic_programming/subset_sum.rs b/src/dynamic_programming/subset_sum.rs index 0da4a6bc642..f63009038c7 100644 --- a/src/dynamic_programming/subset_sum.rs +++ b/src/dynamic_programming/subset_sum.rs @@ -5,8 +5,8 @@ /// Uses dynamic programming to solve the subset sum problem. /// /// # Arguments -/// * arr - A slice of integers representing the input array -/// * required_sum - The target sum to check for +/// * `arr` - A slice of integers representing the input array. +/// * `required_sum` - The target sum to check for. /// /// # Returns /// * A boolean indicating whether a subset exists that sums to the target From ffe32e94d6693c4478db64a2faaba993796e3e27 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Fri, 28 Nov 2025 00:03:48 -0800 Subject: [PATCH 4/5] Update src/dynamic_programming/subset_sum.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/dynamic_programming/subset_sum.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dynamic_programming/subset_sum.rs b/src/dynamic_programming/subset_sum.rs index f63009038c7..74cedb64acb 100644 --- a/src/dynamic_programming/subset_sum.rs +++ b/src/dynamic_programming/subset_sum.rs @@ -9,7 +9,7 @@ /// * `required_sum` - The target sum to check for. /// /// # Returns -/// * A boolean indicating whether a subset exists that sums to the target +/// * `bool` - A boolean indicating whether a subset exists that sums to the target. pub fn is_sum_subset(arr: &[i32], required_sum: i32) -> bool { let n = arr.len(); From b61bec38c02e6f1ff7e85830380dfd7cd5c61960 Mon Sep 17 00:00:00 2001 From: Ali Alimohammadi <41567902+AliAlimohammadi@users.noreply.github.com> Date: Fri, 28 Nov 2025 00:05:37 -0800 Subject: [PATCH 5/5] Update src/dynamic_programming/subset_sum.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/dynamic_programming/subset_sum.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/dynamic_programming/subset_sum.rs b/src/dynamic_programming/subset_sum.rs index 74cedb64acb..dfe0e809adc 100644 --- a/src/dynamic_programming/subset_sum.rs +++ b/src/dynamic_programming/subset_sum.rs @@ -1,6 +1,8 @@ -// Subset Sum Problem in Rust -// Time Complexity: O(n * sum) where n is array length and sum is the target sum -// Space Complexity: O(n * sum) for the DP table +//! This module provides a solution to the subset sum problem using dynamic programming. +//! +//! # Complexity +//! - Time complexity: O(n * sum) where n is array length and sum is the target sum +//! - Space complexity: O(n * sum) for the DP table /// Determines if there exists a subset of the given array that sums to the target value. /// Uses dynamic programming to solve the subset sum problem. ///