Skip to content

Instantly share code, notes, and snippets.

@MichaelaIvanova
Created July 12, 2017 13:27
Show Gist options
  • Save MichaelaIvanova/3688c3cd927eb5d9cab12998f05e0f4a to your computer and use it in GitHub Desktop.
Save MichaelaIvanova/3688c3cd927eb5d9cab12998f05e0f4a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace algos
{
class Program
{
static void Main(string[] args)
{
int[] input = new int[] { 3, 1, 2, 4, 3 };
solution(input);
}
public static int solution(int[] A)
{
var sums = new List<int>();
var allSum = new List<int>(A).Sum();
var sumLPart = 0;
for (int i = 0; i < A.Length; i++)
{
var currSum = sumLPart + A[i];
sumLPart = currSum;
var sumR = allSum - currSum;
var absDiff = Math.Abs(sumLPart - sumR);
sums.Add(absDiff);
}
var smallest = sums.Min();
return smallest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment