Advertisement
Razhagal

ZeroSubsets

Mar 20th, 2014
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class ZeroSubset
  4. {
  5.     static void Main()
  6.     {
  7.         //Input
  8.         int N = 5;
  9.         int S = 0;
  10.         long[] array = new long[N];
  11.  
  12.         for (int i = 0; i < N; i++)
  13.         {
  14.             long num = long.Parse(Console.ReadLine());
  15.             array[i] = num;
  16.         }
  17.  
  18.         //Solution:
  19.         //- Find number of all combinations;
  20.         //- Convert the current combination number to its binary representation;
  21.         //- Mirror the bits in a new string for easier use later;
  22.         //- Take the bit from the current index in the new string and compare it to the desired bit value;
  23.         //- In the end of the loop compare the accumulated sum to the initial input;
  24.         int combinations = (int)Math.Pow(2, N);
  25.         int bit;
  26.         bool subsetFound = false;
  27.  
  28.         for (int i = 1; i < combinations; i++)
  29.         {
  30.             string comBinary = Convert.ToString(i, 2);
  31.             string reverseBin = "";
  32.  
  33.             for (int j = comBinary.Length - 1; j >= 0; j--)
  34.             {
  35.                 reverseBin += comBinary[j].ToString();
  36.             }
  37.  
  38.             List<int> currentCombinationNumbers = new List<int>();
  39.             long sum = 0;
  40.             for (int k = 0; k < reverseBin.Length; k++)
  41.             {
  42.                 bit = Convert.ToInt32(Convert.ToString(reverseBin[k]));
  43.                 if (bit == 1)
  44.                 {
  45.                     currentCombinationNumbers.Add(Convert.ToInt32(array[k]));
  46.                     sum += array[k];
  47.                     if (k == (reverseBin.Length - 1) && sum == S && currentCombinationNumbers.Count > 1)
  48.                     {
  49.                         subsetFound = true;
  50.  
  51.                         //Output if found
  52.                         for (int m = 0; m < currentCombinationNumbers.Count; m++)
  53.                         {
  54.                             if (m  < currentCombinationNumbers.Count - 1)
  55.                             {
  56.                                 Console.Write("{0} + ", currentCombinationNumbers[m]);
  57.                             }
  58.                             else if (m == currentCombinationNumbers.Count - 1)
  59.                             {
  60.                                 Console.WriteLine("{0} = 0 ", currentCombinationNumbers[m]);
  61.                             }
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.         //Output if not found
  69.         if (!subsetFound)
  70.         {
  71.             Console.WriteLine("No zero subset.");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement