Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Thursday, August 3, 2017

Find Profession Geeks Solution


Problem:



Approach is to find the ancestor , if n is divisible by 2 it means it is right child hence it changes the value else same as ancestor(for odd number)


Code 1 :

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int k=ab.nextInt();
   int n=ab.nextInt();
   boolean count=false;
   if(n==0)
   count=true;
   if(n!=0)
  while(n!=1)
  {
      if(n%2!=0)
     {
         n=(n+1)/2;
     }
     else
     {
         count=!count;
         n/=2;
     }
  }
   if(!count)
   System.out.println("Engineer");
   else
   System.out.println("Doctor");
}
}
}

Code 2 :

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int n=ab.nextInt();
   int k=ab.nextInt();
   int count=0;
   long val=1;
   val=val<<n-1;
   val+=k-1;
  // System.out.println(val);
   while(val>1)
   {
       if(val%2!=0)
       count++;
       val/=2;
   }
   if(count%2==0)
   System.out.println("Engineer");
   else
   System.out.println("Doctor");
}
}
}

0 Comments:

Post a Comment

Stats