patterncsharpMinor
Sum of subset of 5 numbers equals 0
Viewed 0 times
numberssubsetequalssum
Problem
I have a task to print all zero subsets of 5 numbers, input from the console.
I have succeeded in implementing a working code, but it seems it is quite complex (it is like inception) and if the count of the numbers is to be greater, it would be rather pointless to use such a method.
```
using System;
using System.Collections.Generic;
using System.Linq;
class ZeroSubset
{
static void Main()
{
int number;
int[] Numbers = new int[5];
bool result = false;
for (int i = 0;i <=4; i++)
{
here: ;
Console.WriteLine("Input number {0}", i + 1);
if (int.TryParse(Console.ReadLine(), out number))
{
Numbers[i] = number;
}
else
{
goto here;
}
}
if (Numbers[0] == 0 && Numbers[1] == 0 && Numbers[2] == 0 && Numbers[3] == 0 && Numbers[4] == 0)
{
result = true;
Console.WriteLine(String.Join("+", Numbers) + " = 0");
return;
}
for (int firstNum = 0; firstNum <= 3; firstNum++)
{
for (int secondNum = firstNum + 1; secondNum <= 4; secondNum++)
{
if (Numbers[firstNum] + Numbers[secondNum] == 0)
{
result = true;
Console.WriteLine("{0} + {1} = 0", Numbers[firstNum], Numbers[secondNum]);
}
}
}
for (int firstNum = 0; firstNum <= 2; firstNum++)
{
for (int secondNum = firstNum + 1; secondNum <= 3; secondNum++)
{
for (int thirdNum = secondNum + 1; thirdNum <= 4; thirdNum++)
{
if (Numbers[firstNum] + Numbers[secondNum] + Numbers[thirdNum]== 0)
{
result = true;
Console.WriteLine("{0} + {1} + {2} = 0", Numbers[firstNum], Numbers[secondNum], Numbers[thirdN
I have succeeded in implementing a working code, but it seems it is quite complex (it is like inception) and if the count of the numbers is to be greater, it would be rather pointless to use such a method.
```
using System;
using System.Collections.Generic;
using System.Linq;
class ZeroSubset
{
static void Main()
{
int number;
int[] Numbers = new int[5];
bool result = false;
for (int i = 0;i <=4; i++)
{
here: ;
Console.WriteLine("Input number {0}", i + 1);
if (int.TryParse(Console.ReadLine(), out number))
{
Numbers[i] = number;
}
else
{
goto here;
}
}
if (Numbers[0] == 0 && Numbers[1] == 0 && Numbers[2] == 0 && Numbers[3] == 0 && Numbers[4] == 0)
{
result = true;
Console.WriteLine(String.Join("+", Numbers) + " = 0");
return;
}
for (int firstNum = 0; firstNum <= 3; firstNum++)
{
for (int secondNum = firstNum + 1; secondNum <= 4; secondNum++)
{
if (Numbers[firstNum] + Numbers[secondNum] == 0)
{
result = true;
Console.WriteLine("{0} + {1} = 0", Numbers[firstNum], Numbers[secondNum]);
}
}
}
for (int firstNum = 0; firstNum <= 2; firstNum++)
{
for (int secondNum = firstNum + 1; secondNum <= 3; secondNum++)
{
for (int thirdNum = secondNum + 1; thirdNum <= 4; thirdNum++)
{
if (Numbers[firstNum] + Numbers[secondNum] + Numbers[thirdNum]== 0)
{
result = true;
Console.WriteLine("{0} + {1} + {2} = 0", Numbers[firstNum], Numbers[secondNum], Numbers[thirdN
Solution
Using a
Creating the list would look something like this:
The sub routine to get the subsets could look something like this:
This will return a list of all the subsets that add up to the target sum.
List instead of int[] allows your code to not only be more dynamic but also to leverage the GetRange method of the List. This simplifies your code to only 2 loops.Creating the list would look something like this:
int number;
List Numbers = new List();
for(int i = 0; i <= 4; i++)
{
number = 0;
bool good = false;
while(!good)
{
Console.WriteLine("Input number {0}", i + 1);
if(int.TryParse(Console.ReadLine(), out number))
{
Numbers.Add(number);
good = true;
}
else
Console.WriteLine("Invalid input, try again");
}
}The sub routine to get the subsets could look something like this:
public static List> AllSubsets2(List mainset, int targetsum)
{
List> outval = new List>();
for(int num2 = 0; num2 {mainset[num2]});
int sum = mainset[num2];
for(int num = num2+1; num < mainset.Count; num++)
{
sum += mainset[num];
if(sum == 0)
outval.Add(mainset.GetRange(num2, num - num2));
}
}
return outval;
}This will return a list of all the subsets that add up to the target sum.
Code Snippets
int number;
List<int> Numbers = new List<int>();
for(int i = 0; i <= 4; i++)
{
number = 0;
bool good = false;
while(!good)
{
Console.WriteLine("Input number {0}", i + 1);
if(int.TryParse(Console.ReadLine(), out number))
{
Numbers.Add(number);
good = true;
}
else
Console.WriteLine("Invalid input, try again");
}
}public static List<List<int>> AllSubsets2(List<int> mainset, int targetsum)
{
List<List<int>> outval = new List<List<int>>();
for(int num2 = 0; num2 < mainset.Count; num2++)
{
if(mainset[num2] == 0)
outval.Add(new List<int>{mainset[num2]});
int sum = mainset[num2];
for(int num = num2+1; num < mainset.Count; num++)
{
sum += mainset[num];
if(sum == 0)
outval.Add(mainset.GetRange(num2, num - num2));
}
}
return outval;
}Context
StackExchange Code Review Q#47562, answer score: 8
Revisions (0)
No revisions yet.