Skip to main content

HACKER RANK - ARRAYS - NEW YEAR CHAOS

 HACKER RANK - ARRAYS - NEW YEAR CHAOS

QUESTION:

It is New Year's Day and people are in line for the Wonderland rollercoaster ride. Each person wears a sticker indicating their initial position in the queue from  to . Any person can bribe the person directly in front of them to swap positions, but they still wear their original sticker. One person can bribe at most two others.

Determine the minimum number of bribes that took place to get to a given queue order. Print the number of bribes, or, if anyone has bribed more than two people, print Too chaotic.

Example

If person  bribes person , the queue will look like this: . Only  bribe is required. Print 1.

Person  had to bribe  people to get to the current position. Print Too chaotic.

SAMPLE INPUT:

  • 2
  • 5
  • 2 1 5 3 4
  • 5
  • 2 5 1 3 4

OUTPUT:

3
  • Too chaotic

SOLUTION:


def minimumBribes(q):
    # Write your code here
    count=0
    d=dict(enumerate(q,1))
    for i in range(len(d),1,-1):
        if d[i]==i:
            continue
        if d[i-1]==i:
          d[i-1]=d[i]
          count+=1
        elif d[i-2]==i :
            d[i-2],d[i-1]=d[i-1],d[i]
            count+=2 
        else:
            return "Too chaotic"
    return count

Comments

Popular posts from this blog

POST ORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below. It received   parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the   function. Constraints   Nodes in the tree    Output Format Print the tree's postorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 4 3 6 5 2 1 Explanation The postorder traversal is shown. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct  node *ri...

HACKERRANK -- DELETE A NODE AT A GIVEN POSITION

  HACKERRANK --  DELETE A NODE AT A GIVEN POSITION Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example After removing the node at position  ,  . Function Description Complete the  deleteNode  function in the editor below. deleteNode  has the following parameters: -  SinglyLinkedListNode pointer llist:  a reference to the head node in the list -  int position:  the position of the node to remove Returns -  SinglyLinkedListNode pointer:  a reference to the head of the modified list Input Format The first line of input contains an integer  , the number of elements in the linked list. Each of the next   lines contains an integer, the node data values in order. The last line contains an integer,  , the position of the node to delete. Constraints , wh...

TREE PREORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below, which has   parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the  preOrder  function. Constraints  Nodes in the tree  Output Format Print the tree's preorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 1 2 5 3 4 6 Explanation The preorder traversal of the binary tree is printed. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct ...