1
namespace CombinationWork
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> List = new List<int>(new int[] {1,2,5,10});
            for (int noSelected=0;noSelected<4;noSelected++)
            {
                for(int noAdded=0;noAdded<4;noAdded++)
                {
                    List.Add(List[noSelected]+List[noAdded]);
                }
            }
            List = List.Distinct().ToList();
            for (int display = 0; display < List.Count; display++)
            {
                Console.WriteLine( List[display]);
            }
            Console.ReadLine();
        }
    }
}

Guys assistance needed. let say i have an array: int[] a={1,2,5,10}; I want to add all the elements with each other .Example:

  • 1
  • 2

  • 2+1=3

  • 5+1=6
  • 5+2=7
  • 5+2+1=8
  • 10
  • 10+1=11
  • 10+2=12
  • 10+2+1=13
  • 10+5=15
  • 10+5+1=16
  • 10+5+2=17
  • 10+5+2+1=18

I'm getting some inaccurate values. The error lies in using the nested for loops but i dont know what else to use

1 Answer 1

1

For n numbers, it should give you 2 pow n combinations. Use binary representation of numbers to publish all combinations.

Example, Binary - 001 decimal - 1 Binary - 010 decimal - 2 Binary - 011 decimal - 3 ... Binary - 001 decimal - 1

From above truth table, include a number to summation only when respective binary digit is 1

Example, your numbers in list [1, 2, 3]; interpret as follows, For Binary - 001, you should get 3 For Binary - 010, you should get 2 For Binary - 011, you should get 2 + 3 = 6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.