forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from TheAlgorithms:master #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
src/dynamic_programming/subset_sum.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //! 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. | ||
| /// | ||
| /// # Arguments | ||
| /// * `arr` - A slice of integers representing the input array. | ||
| /// * `required_sum` - The target sum to check for. | ||
| /// | ||
| /// # Returns | ||
| /// * `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(); | ||
|
|
||
| // 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, | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.