Created
July 12, 2017 13:27
-
-
Save MichaelaIvanova/3688c3cd927eb5d9cab12998f05e0f4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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