Created
January 13, 2017 21:18
-
-
Save sachintha81/f906bcb3f067e5db33517b9eb1655abd to your computer and use it in GitHub Desktop.
All Possible Combinations of a list of Values
This file contains 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
namespace Combinations | |
{ | |
public class Combinations | |
{ | |
public static void Main(string[] args) | |
{ | |
GetCombination(new List<int> { 1, 2, 3 }); | |
} | |
public static void GetCombination(List<int> list) | |
{ | |
double count = Math.Pow(2, list.Count); | |
for (int i = 1; i <= count - 1; i++) | |
{ | |
string str = Convert.ToString(i, 2).PadLeft(list.Count, '0'); | |
for (int j = 0; j < str.Length; j++) | |
{ | |
if (str[j] == '1') | |
{ | |
Console.Write(list[j]); | |
} | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PROBLEM STATEMENT
I have a list of integers in my C# program. However, I know the number of items I have in my list only at run-time.
Let us say, for the sake of simplicity, my list is {1, 2, 3}
Now I need to generate all possible combinations as follows.