Advertisement
d0ntth1nc

Untitled

Jan 20th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ZeroSubset
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             int[] inputNumbers = { 1, 3, -4, -2, -1 };
  12.             int combinationsCount = (1 << inputNumbers.Length) - 1;
  13.             for (int i = 1; i < combinationsCount; i++)
  14.             {
  15.                 var subset = new List<int>();
  16.                 for (int j = 0; j < inputNumbers.Length; j++)
  17.                 {
  18.                     if ((((1 << j) & i) >> j) == 1)
  19.                     {
  20.                         subset.Add(inputNumbers[j]);
  21.                     }
  22.                 }
  23.                 if (subset.Sum() == 0)
  24.                 {
  25.                     Console.WriteLine(string.Join(", ", subset));
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement