Finding Profession | Practice | GeeksforGeeks

Consider a special family of Engineers and Doctors with following rules :

Everybody has two children.
First child of an Engineer is an Engineer and second child is a Doctor.
First child of an Doctor is Doctor and second child is an Engineer.
All generations of Doctors and Engineers start with Engineer.

Everybody has two children.
First child of an Engineer is an Engineer and second child is a Doctor.
First child of an Doctor is Doctor and second child is an Engineer.
All generations of Doctors and Engineers start with Engineer.
We can represent the situation using below diagram:
                E
           /        \
          E          D
        /   \       /  \
       E     D     D    E
      / \   / \   / \   / \
     E   D D   E  D  E  E  D

Given level and position(pos) of a person in above ancestor tree, find profession of the person.
Example 1:
Input: level = 4, pos = 2
Output: Doctor
Explaination: It is shown in the tree given 
in question.
Example 2:
Input: level = 3, pos = 4
Output: Engineer
Explaination: Already given in the tree in 
question.
Your Task:
You do not need to read input or print anything. Your task is to complete the function profession() which takes level and pos as input parameters and returns 'd' if it is a doctor. Otherwise return 'e'. The driver code will output Doctor for 'd' and Engineer for 'e' itself.
Expected Time Complexity: O(log(pos))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ level ≤ 109
1 ≤ pos ≤ min(109, 2level-1)
0
blank551k2 months ago
class Solution{
public:
   char recursion(int pos,long total_element_in_level,int diff){
       if(total_element_in_level==1){
           if(diff%2==0){
               return 'e';
           }
           return 'd';
       }
       if(pos>total_element_in_level/2){
           diff=(diff+1)%2;
           return recursion(pos-total_element_in_level/2,total_element_in_level/2,diff);
       }
       return recursion(pos,total_element_in_level/2,diff);
   }
   char profession(int level, int pos){
       // long long total = pow(2,(double)(level-1));
       // return recursion(pos,total,0);
       
       int temp=0;
       int t=pos;
       while(t > 0){
           t/=2;
           temp++;
       }
       
       long total=pow(2,(temp-1));
       if(total<pos){
           total*=2;
       }
       return recursion(pos,total,0);
   }
};
0
devendray21062 months ago
Why this is giving seg fault?
 
    char profession(int level, int pos){
        if(level==1)
            return 'e';
        if(profession(level-1,(pos+1)/2)=='d')
            return pos%2?'d':'e';
        return pos%2?'e':'d';
        
    }
0
aayushchauhan1767 months ago
Why my code is showing segmentation error? PLEASE HELP!!
-1
ten000perhr7 months ago
SEE THIS
       if (level == 1)       return 'e';
 if (profession(level-1, (pos+1)/2) == 'd')       return (pos%2)? 'd' : 'e';
 
 return (pos%2)?  'e' : 'd';
0
shubhamd2312057 months ago
 
class Solution{   int  solve(int level,int pos) {              if(level==0 or pos==0)return pos;              int mid=pow(2,level-1)/2;              if(pos<=mid) {           return solve(level-1,pos);       }       else       return !solve(level-1,pos-mid);   }public:   char profession(int level, int pos){       // code here       int ans=solve(level,pos);       cout<<ans;       if(ans==1)       return 'D';       else       return 'E';   }};
 
-2
Ali john Naqvi8 months ago
Ali john Naqvi
int countBits(int n){     //count ON bits using n & n-1    int on_bits=0;    while(n)    {        n=n&(n-1);        on_bits++;    }    return on_bits; }    char profession(int level, int pos){        // code here        int x=countBits(pos-1);        if (x%2==0)            return 'e';        else            return 'd';     }
0
Anand Sharma9 months ago
Anand Sharma
Please help.I'm getting segmentation fault.I have checked on ide code is working fine.
void solve(char p,int level,vector<char> &v,int i)    {        if(i>level)        {            return;        }
        if(i==level)                v.push_back(p);
        if(p=='e')        {            //traverse left subtree            solve('e',level,v,i+1);
            //traverse right subtree            solve('d',level,v,i+1);            }        else        {            //traverse left subtree            solve('d',level,v,i+1);
            //traverse right subtree            solve('e',level,v,i+1);        }    }    char profession(int level, int pos)    {        // code here        vector<char>v;        solve('e',level,v,1);        //cout<<"\nSize : "<<v.size()<<"\n"; return="" v[pos-1];="" }="">
0
Dwane 11 months ago
Dwane 
char profession(int level, int pos){        if(pos==1) return 'e';        char par=profession(level-1,(pos+1)/2);        if(pos%2!=0)        {            return par;        }        if(par=='e') return 'd';        else return 'e';       }
0
tri11 months ago
tri
beware postion can be 0
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.