patterncsharpMinor
Split the array so that the sum of the numbers on one side of the split equals the sum of the numbers on the other side
Viewed 0 times
thearraysidenumberssplitonethatequalssumother
Problem
I would really appreciate if you can help me to find a better way to do it.
public class SplitArray
{
public SplitArray() { }
public bool CanArrayBeSplit(int[] intArray)
{
foreach(var pair in GeneratePairs(intArray))
{
if (pair.Item1 == pair.Item2)
return true;
}
return false;
}
private List> GeneratePairs(int[] integerArray)
{
var output = new List>();
for (int i = 0; i < integerArray.Length -1; i++)
{
output.Add(Tuple.Create(GetSum(0, i+1, integerArray), GetSum(i + 1, integerArray.Length ,integerArray)));
}
return output;
}
private int GetSum(int startIndex, int endIndex , int[] array)
{
int sum = 0;
for (int i = startIndex; i < endIndex; i++)
{
sum += array[i];
}
return sum;
}
}Solution
The solution to this problem is easy, once you know the trick.
The trick, is to calculate the sum of all values then do add/subtract until they match...
This is very different to what you have done, so it does not really make sense to review your algorithm, and then say 'you did it wrong'.
What I will do, is say your code is neat enough, and the logic you have done is visible enough, to make the algorithm clear. Even though it is not the best algorithm, you have implemented it well.
The problems with your algorithm are that you are doing the following:
So, the following code (on ideone too) makes the 'simple' solution 'obvious'....
The trick, is to calculate the sum of all values then do add/subtract until they match...
This is very different to what you have done, so it does not really make sense to review your algorithm, and then say 'you did it wrong'.
What I will do, is say your code is neat enough, and the logic you have done is visible enough, to make the algorithm clear. Even though it is not the best algorithm, you have implemented it well.
The problems with your algorithm are that you are doing the following:
- you are calculating the sums of each side of the split each time
- you are calculating all possible splits, even if you could identify a successful split sooner
- The Pairs and other classes are overkill for the solution you could have....
So, the following code (on ideone too) makes the 'simple' solution 'obvious'....
public static bool CanArrayBeSplit(int[] intArray) {
int sum = 0;
foreach (int v in intArray) {
sum += v;
}
int left = 0;
int right = sum;
for (int i = 0; left != right && i < intArray.Length; i++) {
left += intArray[i];
right -= intArray[i];
}
return left == right;
}
public static void Main()
{
Console.WriteLine("Testing: " + CanArrayBeSplit(new int[]{1,2,3,4,5,6,7,8,9}));
Console.WriteLine("Testing: " + CanArrayBeSplit(new int[]{1,2,3,4,5,6,7,8,9,45}));
}Code Snippets
public static bool CanArrayBeSplit(int[] intArray) {
int sum = 0;
foreach (int v in intArray) {
sum += v;
}
int left = 0;
int right = sum;
for (int i = 0; left != right && i < intArray.Length; i++) {
left += intArray[i];
right -= intArray[i];
}
return left == right;
}
public static void Main()
{
Console.WriteLine("Testing: " + CanArrayBeSplit(new int[]{1,2,3,4,5,6,7,8,9}));
Console.WriteLine("Testing: " + CanArrayBeSplit(new int[]{1,2,3,4,5,6,7,8,9,45}));
}Context
StackExchange Code Review Q#51361, answer score: 8
Revisions (0)
No revisions yet.