Friday, May 20, 2016

Codility TapeEquilibrium Solution C#

Codility TapeEquilibrium Solution C# 

Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])

Solution:

Here we need to add two namespaces, 

using System;
// you can also use other imports, for example:
using System.Collections.Generic;
using System.Linq;

        public int solution(int[] A)
        {
            // write your code in C# 6.0 with .NET 4.5 (Mono)
            List<int> list = A.ToList();
            int temp = 0;
            int sum = list.Sum();
            List<int> result = new List<int>();
            foreach (int i in list)
            {
                temp = temp + i;
                result.Add(Math.Abs((sum - temp) - temp));

            }
            return result.Min();

        }

And Here goes the result for TapeEquilibrium,


Tape  Equilibrium

No comments:

Post a Comment