C# || How To Get Array Combination Sum Equal To Target Value Using C#
The following is a module with functions which demonstrates how to get an array combination sum equal to a target value using C#.
1. Combination Sum – Problem Statement
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
Example 2:
Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input: candidates = [2], target = 1
Output: []
Example 4:
Input: candidates = [1], target = 1
Output: [[1]]
Example 5:
Input: candidates = [1], target = 2
Output: [[1,1]]
2. Combination Sum – Solution
The following is a solution which demonstrates how to get an array combination sum equal to a target value.
The main idea of this solution is to generate subsets of the different combinations that can be matched together, checking to see if any sum up to equal the target value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 26, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get combination sum equal to target // ============================================================================ public class Solution { List<IList<int>> result = new List<IList<int>>(); public IList<IList<int>> CombinationSum(int[] candidates, int target) { Search(candidates, target, 0, 0, new List<int>()); return result; } private void Search(int[] candidates, int target, int currentSum, int currentIndex, List<int> combination) { // Sum is greater than target, exit if (currentSum > target) { return; } // Sum equals target if (currentSum == target) { result.Add(new List<int>(combination)); return; } // Generate subsets for (int index = currentIndex; index < candidates.Length; ++index) { // Add number to combination combination.Add(candidates[index]); // Keep searching for matches Search(candidates, target, currentSum + candidates[index], index, combination); // Remove last item as its already been explored combination.RemoveAt(combination.Count - 1); } } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
[[2,2,3],[7]]
[[2,2,2,2],[2,3,3],[3,5]]
[]
[[1]]
[[1,1]]
Leave a Reply