Coding Round

 using System;

using System.Linq;

using static System.Runtime.InteropServices.JavaScript.JSType;


class Program

{

    public static void Main()

    {

        ProblemStatementOne.ArrayHeights();

        ProblemStatementTwo.RearrangeArray();                

    }

}


public static class ProblemStatementOne

{

    public static void ArrayHeights()

    {

        int[] heights = { 10, 6, 8, 5, 11, 9 }; //exp op [3,1,2,1,0]

        List<int> result = new List<int>();


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

        {

            int count = 0;

            for (int j = 0; j < i; j++) // look only to the left

            {

                if (heights[j] > heights[i])

                {

                    count++;

                }

            }

            result.Add(count);

        }


        Console.WriteLine("[" + string.Join(", ", result) + "]");

    }


}


public static class ProblemStatementTwo

{

    public static void RearrangeArray()

    {

        int[] arr = { 1, 2, 3, -4, -1, 4, -7,-9,7,9,5 };

        List<int> pos = new List<int>();

        List<int> neg = new List<int>();


        // Separate positive and negative numbers

        foreach (int num in arr)

        {

            if (num >= 0)

                pos.Add(num);

            else

                neg.Add(num);

        }


        List<int> result = new List<int>();

        int i = 0, j = 0;


        // Alternate positive and negative

        while (i < pos.Count && j < neg.Count)

        {

            result.Add(pos[i++]);

            result.Add(neg[j++]);

        }


        // Add remaining positives

        while (i < pos.Count)

        {

            result.Add(pos[i++]);

        }


        // Add remaining negatives

        while (j < neg.Count)

        {

            result.Add(neg[j++]);

        }

        Console.WriteLine("[" + string.Join(", ", result) + "]");

    }

}



Comments

Popular posts from this blog

API development using .NET Core - ProductManagmentCRUD

Singleton Design Pattern Realtime Example of banking

Consume API in MVC Application