Пример #1
0
        private void button4_Click_1(object sender, EventArgs e)
        {
            //http://www.careercup.com/question?id=5772881111810048

            //http://stackoverflow.com/questions/5534063/zero-sum-subarray

            /* ex: 2 3 -4 9 -1 -7 -5 6 5
             * sum to enter in hash table : 0 2 5 1 10 9 2 -3 3 8
             * index to enter in hash table: -1 0 1 2 3 4 5 6 7 8
             * Logic:
             * 1) Create an ht with sum as key and list of index as value
             * 2)  we add the base value in the ht as sum 0 and index -1 because if we encounter 0 in the iteration then we found the subarray of value 0
             * 3) We iterate the given array from 0 to array.length -1
             * 4) In the iteration we make the sum of the previous elements with the current element and use the sum as the key to ht
             * 5) If the ht does not contain the sum. add sum as the key and value as the index (index list)
             * 6) If the ht contains the sum, then we found the subarray having the value of zero
             * grab the index of the sum in the ht with ht[sum]. The index sum and the current item sum is equla to 0 so that mean the items from index + 1 to current index sum will be zero
             * print the values from index + 1 to current index
             * The reason we have List<int> as value is we might have multiple sum value so each key might have multiple indexes like below
             * int[] array = {0, 1, -1, 0}  we need to print every subarray the can cause the sum to 0  eg (0) (1,-1) (0,1,-1) etc
             *
             *
             */

            int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox6.Text);

            List <string> result = FindSubArraySumOfzero(input);

            this.textBox5.Text = AlgorithmHelper.ConvertStringListArrayToSpaceSeparatedString(result);
        }