Saturday, August 18, 2018

New Year Chaos (Minimum bribe)

t's New Year's Day and everyone's in line for the Wonderland rollercoaster ride! There are a number of people queued up, and each person wears a sticker indicating their initial position in the queue. Initial positions increment by  from  at the front of the line to  at the back.
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if and  bribes , the queue will look like this: .
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state!
Answer:-
static void minimumBribes(int[] q)
        {         
            var count = 0;
            var total = 0;
            bool flag = true;
            for (int i = 0; i < q.Length -1; i++)
            {
                if (q[i] != i-1)
                {
                    for (int j = i+1 ; j < q.Length; j++)
                    {
                        if (q[i] > q[j])
                        {
                            count++;
                            total = total + 1;
                        }
                    }
                    if (count > 2)                    {
                        flag = false;
                        Console.WriteLine("Too chaotic");
                        break;
                    }
                    count = 0;
                }
            }
            if (flag)
                Console.WriteLine(total);
        }

No comments:

Post a Comment

Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...