• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Monday, July 8, 2013



    Problem Statement

    Rahul is playing a very interesting game. He has N disks ( each of equal radius). 
    Every disk has a distinct number out of 1 to N associated with it. Disks are placed one over other in a single pile.

    Rahul wants to sort this pile of disk in increasing order from top to bottom. But he has a very very special method of doing this. In a single step he can only choose one disk out of the pile and he can only put it at the top. 

    Rahul wants to sort his pile of disks in minimum number of possible steps. So help Rahul in doing so. So don't have to show the actual steps just answer the minimum number of possible steps to sort the pile so that that Rahul can check whether he is doing his work right or wrong

    ===============================================================================

    This was a very easy question, but Some of us found it difficult.

    Here goes the explanation,

    If you write it down on a paper then you will notice that We first track the highest numbered disk and then just move the disks with the decreasing number on top of it.

    For example lets take 3 4 5 1 2  as the numbering. 

    Here, since 5 is the highest number I will move only 2 and then 1 on top of it, so the ans comes as 2. 

    There are two steps to solve this problem
    1) First find the max element. then,
    2) Find (P) the number of elements before the max element which are in increasing order. Total-1-P is the ans.

    This logic fetches 100 ont Techgig.

    If anyone wants the code then just give a comment.

    6 comments:

    1. Its not working for input 2 6 1 3 4 8 5. Can u plz elaborate your method for this input sequence.

      ReplyDelete
    2. I got 20 marks using your logic. Tell me How can i get 100.



      public class sorttechgig1 {

      public static void main(String[] args) {
      int ar[]={5,1,3,2,4};
      System.out.println(sort(ar));

      }
      static public int sort(int input1[]){
      int len=input1.length;
      int max=0,count=0,index=0;
      for(int i=0;i=i;j--){
      if(max<input1[j]){
      max=input1[j];
      index=j;
      }
      }
      if(index!=i & input1[index]!=input1[i]){
      int temp=input1[i];
      input1[i]=input1[index];
      input1[index]=temp;
      count++;
      }
      max=0;
      index=0;
      }
      for(int x=0;x<len;x++){
      System.out.print(input1[x]+", ");
      }
      return count;
      }

      }

      ReplyDelete