Array Sum Reduction

Replace the largest with second largest element in an integer array recursively so as to reduce the total sum by a given integer value. If there is an over reduction by doing so, then deduct the remainder value equally from the largest elements.
{70, 30, 15, 45, 40} 50
{45, 30, 15, 45, 40} 25
{40, 30, 15, 40, 40} 15
{35, 30, 15, 35, 35} 0 
 

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            int[] numArray =  {70, 30, 15, 45, 40};

            int num = 50;

            Console.WriteLine("Initial Array");

            for (int i = 0; i < numArray.Length; i++)

            {

                Console.Write(numArray[i] + " ");

            }

            Console.WriteLine();

            numArray = Algo(numArray, num);

            Console.WriteLine("Modified Array");

            for (int i = 0; i < numArray.Length; i++)

            {

                Console.Write(numArray[i] + " ");

            }

            Console.WriteLine();

        }

        public static int[] Algo(int[] numArray, int num)

        {

            if (num != 0)

            {

                do

                {

                    int[] maxAndSecondMax = GetMaxAndSecondMax(numArray);

                    int count = ReplaceCount(numArray, maxAndSecondMax[0], maxAndSecondMax[1]);

                    if (num – (count * (maxAndSecondMax[0] – maxAndSecondMax[1])) > 0)

                    {

                        num = num – (count * (maxAndSecondMax[0] – maxAndSecondMax[1]));

                        Replace(numArray, maxAndSecondMax[0], maxAndSecondMax[1]);

                    }

                    else

                    {

                        int decrementCount = num / ReplaceCount(numArray, maxAndSecondMax[0], maxAndSecondMax[1]);

                        Replace(numArray, maxAndSecondMax[0], maxAndSecondMax[0] – decrementCount);

                        num = 0;

                    }

                } while (num != 0);

                return (numArray);

            }

            else

            {

                return numArray;

            }

        }

        public static int[] GetMaxAndSecondMax(int[] numArray)

        {

            int[] maxAndSecondMax = new int[2];

            int[] tempArray = new int[numArray.Length];

            for (int i = 0; i < numArray.Length; i++)

            {

                tempArray[i] = numArray[i];

            }

            Array.Sort(tempArray);

            maxAndSecondMax[0] = tempArray[tempArray.Length – 1];

            int flag = 0;

            for (int i = tempArray.Length – 1; i > 0; i–)

            {

                if (tempArray[i] != tempArray[i – 1])

                {

                    maxAndSecondMax[1] = tempArray[i – 1];

                    flag = 1;

                    break;

                }

            }

            if (flag == 1)

            {

                return maxAndSecondMax;

            }

            else

            {

                maxAndSecondMax[1] = maxAndSecondMax[0];

                return maxAndSecondMax;

            }

        }

        public static void Replace(int[] numArray, int Max, int secondMax)

        {

            for (int i = 0; i < numArray.Length; i++)

            {

                if (numArray[i] == Max)

                {

                    numArray[i] = secondMax;

                }

            }

        }

        public static int ReplaceCount(int[] numArray, int Max, int secondMax)

        {

            int count = 0;

            for (int i = 0; i < numArray.Length; i++)

            {

                if (numArray[i] == Max)

                {

                    count++;

                }

            }

            return count;

        }

    }

}
 
Initial Array
70 30 15 45 40
Modified Array
35 30 15 35 35
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s