
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the unique combination of sum from the given number C#?
Create an output list to store the valid sequences, create a current list that will store the current sequence found in the path of the recursion tree. A backtrack function that will go into the recursion until the target is achieved, otherwise, it should backtrack to the previous phase as target becomes less than 0. At any point in time, if target becomes 0 then add the candidate array to the result as the values in the candidate array must be sum up to the given target.
If those are not the cases then, one by one add the elements in the candidate array and recursively move forward.
Say, the number is 5 so we need to find the numbers that forms 5. The output will be “1,4”,”2,3”,5. From the Output 014,.023 and 05 can be discarded
Example
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConsoleApplication{ public class BackTracking{ public void UniqueCombinationOfNumbersCorrespondsToSum(int n){ int[] array = new int[n + 1]; for (int i = 1; i <= n; i++){ array[i] = i;} List<int> currentList = new List<int>(); List<List<int>> output = new List<List<int>>(); UniqueCombinationSum(array, n, 0, 0, currentList, output); foreach (var item in output){ StringBuilder s = new StringBuilder(); foreach (var item1 in item){ s.Append(item1.ToString()); } Console.WriteLine(s); s = null; } } private void UniqueCombinationSum(int[] array, int target, int sum, int index, List<int> currentList, List<List<int>> output){ if (sum == target){ List<int> newList = new List<int>(); newList.AddRange(currentList); output.Add(newList); return; } else if (sum > target){ return; } else{ for (int i = index; i < array.Length; i++){ currentList.Add(array[i]); UniqueCombinationSum(array, target, sum + array[i], i + 1, currentList, output); currentList.Remove(array[i]); } } } } class Program{ static void Main(string[] args){ BackTracking b = new BackTracking(); b.UniqueCombinationOfNumbersCorrespondsToSum(5); } } } }
Output
14 23 05
- Related Articles
- How to find the unique combination k sum that corresponds to k sum using C#?
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- Find Sum of all unique subarray sum for a given array in C++
- How to find the target sum from the given array by backtracking using C#?
- Find number of unique triangles among given N triangles in C++
- C++ Program to Sum the digits of a given number
- Find the Largest number with given number of digits and sum of digits in C++
- How to find the unique triplet that is close to the given target using C#?
- C++ program to perform unique factorization of a Given Number
- C/C++ Program to find the Product of unique prime factors of a number?
- Find the Number of Quadrilaterals Possible from the Given Points using C++
- Find the Number of Prefix Sum Prime in Given Range Query using C++
- Find the Number Of Subarrays Having Sum in a Given Range in C++
- Find the Number Of Subarrays Having Sum in a Given Range using C++
- Program to find the sum of all digits of given number in Python

Advertisements