Answers for "Balanced Array | Interview Question"

C#
0

Balanced Array | Interview Question

static int solve(int[] a){
         if(a.Length % 2 == 0)
            {


                int firsthalf = a.Take(a.Length / 2).ToArray().Sum();
                int secondhalf= a.Skip(a.Length / 2).ToArray().Sum();

                if(firsthalf == secondhalf)
                {
                   return 0;
                }
                else if (firsthalf > secondhalf)
                {

                   return  firsthalf - secondhalf;

                   

                }
                else if (secondhalf > firsthalf)
                {

                    return secondhalf - firsthalf;

                   

                }



                 

              return 0;
            }
 return 0;
    }
Posted by: Guest on August-23-2021

Code answers related to "Balanced Array | Interview Question"

C# Answers by Framework

Browse Popular Code Answers by Language