Arranging heights based on number of persons of greater height standing in front of him

You are given two arrays, first array contain integer which represent heights of persons and second array contain how many persons in front of him are standing who are greater than him in terms of height and forming a queue. Ex
A: 3 2 1
B: 0 1 1

It means in front of person of height 3 there is no person standing; person of height 2 has one person in front of him who has greater height than him, similar to person of height 1. Your task is to arrange them. Output should be:
3 1 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] number = { 3, 5, 8, 2, 9, 7, 9, 4 };
            int[] countGreaterThan = { 1, 0, 0, 7, 0, 3, 0, 5 };
            ArrayList currentOrder = new ArrayList();
            currentOrder.AddRange(countGreaterThan);
            for (int i = 0; i < number.Length; i++)
            {
                Console.Write(number[i] + " ");
                currentOrder[i] = 0;
            }
            Console.WriteLine();
            for (int i = 0; i < countGreaterThan.Length; i++)
            {
                Console.Write(countGreaterThan[i] + " ");
            }
            Console.WriteLine("\n=====================");
            Array.Sort(number, countGreaterThan);
            //Initializing the array list
            ArrayList numList = new ArrayList();
            numList.AddRange(number);
            ArrayList expectedOrder = new ArrayList();
            expectedOrder.AddRange(countGreaterThan);
            int flag = 0;
            do
            {
                flag = numList.Count;
                for (int i = 0; i < numList.Count; i++)
                {
                    if ((int)currentOrder[i] < (int)expectedOrder[i])
                    {
                        flag = i;
                        break;
                    }
                }
                for (int i = flag + 1; i < numList.Count; i++)
                {
                    if ((int)numList[flag] > (int)numList[i])
                    {
                        currentOrder[i] = (int)currentOrder[i] - 1;
                    }

                    else if ((int)numList[flag] < (int)numList[i])
                    {
                        currentOrder[flag] = (int)currentOrder[flag] + 1;
                        if ((int)currentOrder[flag] == (int)expectedOrder[flag])
                        {
                            //Swap the elements here
                            numList.Insert(i + 1, numList[flag]);
                            currentOrder.Insert(i + 1, currentOrder[flag]);
                            expectedOrder.Insert(i + 1, expectedOrder[flag]);
                            numList.RemoveAt(flag);
                            currentOrder.RemoveAt(flag);
                            expectedOrder.RemoveAt(flag);
                            break;
                        }
                    }
                }
            } while (flag != numList.Count);
            for (int i = 0; i < numList.Count; i++)
            {
                Console.Write(numList[i] + " ");
            }
            Console.WriteLine();
            for (int i = 0; i < currentOrder.Count; i++)
            {
                Console.Write(currentOrder[i] + " ");
            }
            Console.WriteLine();
        }
    }
}

Output
=======
3 5 8 2 9 7 9 4
1 0 0 7 0 3 0 5
=====================
5 3 8 9 9 7 4 2
0 1 0 0 0 3 5 7
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