Skip to content

Instantly share code, notes, and snippets.

@sholfen
Last active November 8, 2015 14:38
Show Gist options
  • Save sholfen/247b4351823e932db929 to your computer and use it in GitHub Desktop.
Save sholfen/247b4351823e932db929 to your computer and use it in GitHub Desktop.
for Codility
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TapeEquilibrium
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[] { 1, 3, 4, 55, 6123, 5456, 43, 324, 566, 32, 4, 53, 1266, -12, -192, 4, 0, -941204 };
int[] B = new int[] { 3, 1, 2, 4, 3 };
int result = solution(A);
Console.WriteLine(result);
}
static int solution(int[] input)
{
int total = 0;
for (int i = 0; i < input.Length; i++)
{
total += input[i];
}
int p = 0;
int left_total = 0;
int right_totla = total;
int final_result = int.MaxValue;
for (int i = 1; i <= input.Length; i++)
{
left_total += input[i-1];
right_totla -= input[i-1];
int difference = Math.Abs(left_total - right_totla);
if (difference < final_result)
{
final_result = difference;
}
}
final_result = Math.Abs(final_result);
return final_result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment