mainul098
10/7/2016 - 6:00 PM

https://codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/

public class Program
{
        /// <summary>
        /// The solution.
        /// </summary>
        /// <param name="A">
        /// The a.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int solution(int[] A)
        {
            var length = A.Length;
            var rightSum = new int[length];
            var leftSum = new int[length];
            var minDiff = int.MaxValue;
            rightSum[length - 1] = A[length - 1];

            for (var i = length - 2; i > -1; i--)
            {
                rightSum[i] = A[i] + rightSum[i + 1];
            }

        for (var i = 0; i < length - 1; i++)
        {
            leftSum[i] = A[i];
            if (i != 0)
            {
                leftSum[i] += leftSum[i - 1];
            }

            var diff = Math.Abs(leftSum[i] - rightSum[i + 1]);

            if (diff < minDiff)
            {
                minDiff = diff;
            }
        }

        return minDiff;
    }

    /// <summary>
    /// The main.
    /// </summary>
    /// <param name="args">
    /// The args.
    /// </param>
    static void Main(string[] args)
    {
        var result = new Program().solution(new[] { 3, 1, 2, 4, 3,7 });
        Console.WriteLine(result);
        Console.ReadLine();
    }
}