Find profession in a special family

12 Jun, 2022
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 of a person in above ancestor tree, find the profession of the person.Examples : 
Input : level = 4, pos = 2
Output : Doctor

Input : level = 3, pos = 4
Output : Engineer
Method 1 (Recursive) The idea is based on the fact that profession of a person depends on following two.
Profession of parent.Position of node : If position of a node is odd, then its profession is same as its parent. Else profession is different from its parent.
Profession of parent.
Position of node : If position of a node is odd, then its profession is same as its parent. Else profession is different from its parent.
We recursively find the profession of parent, then use point 2 above to find the profession of current node. Below is implementation of above idea. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find profession of a person at// given level and position.#include<bits/stdc++.h>using namespace std; // Returns 'e' if profession of node at given level// and position is engineer. Else doctor. The function// assumes that given position and level have valid values.char findProffesion(int level, int pos){    // Base case    if (level == 1)        return 'e';     // Recursively find parent's profession. If parent    // is a Doctor, this node will be a Doctor if it is    // at odd position and an engineer if at even position    if (findProffesion(level-1, (pos+1)/2) == 'd')        return (pos%2)? 'd' : 'e';     // If parent is an engineer, then current node will be    // an engineer if at add position and doctor if even    // position.    return (pos%2)?  'e' : 'd';} // Driver codeint main(void){    int level = 4, pos = 2;    (findProffesion(level, pos) == 'e')? cout << "Engineer"                                       : cout << "Doctor" ;    return 0;}
// Java program to find// profession of a person// at given level and positionimport java.io.*; class GFG{ // Returns 'e' if profession// of node at given level// and position is engineer.// Else doctor. The function// assumes that given position// and level have valid values.static char findProffesion(int level,                           int pos){    // Base case    if (level == 1)        return 'e';     // Recursively find parent's    // profession. If parent    // is a Doctor, this node    // will be a Doctor if it    // is at odd position and an    // engineer if at even position    if (findProffesion(level - 1,                      (pos + 1) / 2) == 'd')        return (pos % 2 > 0) ?                         'd' : 'e';     // If parent is an engineer,    // then current node will be    // an engineer if at add    // position and doctor if even    // position.    return (pos % 2 > 0) ?                     'e' : 'd';} // Driver codepublic static void main (String[] args){    int level = 4, pos = 2;    if(findProffesion(level,                      pos) == 'e')    System.out.println("Engineer");    else    System.out.println("Doctor");}} // This code is contributed// by anuj_67.
# python 3 program to find profession of a person at# given level and position. # Returns 'e' if profession of node at given level# and position is engineer. Else doctor. The function# assumes that given position and level have valid values.def findProffesion(level, pos):    # Base case    if (level == 1):        return 'e'     # Recursively find parent's profession. If parent    # is a Doctor, this node will be a Doctor if it is    # at odd position and an engineer if at even position    if (findProffesion(level-1, (pos+1)//2) == 'd'):        if (pos%2):            return 'd'        else:            return 'e'     # If parent is an engineer, then current node will be    # an engineer if at add position and doctor if even    # position.    if(pos%2):        return 'e'    else:        return 'd' # Driver codeif __name__ == '__main__':    level = 3    pos = 4    if(findProffesion(level, pos) == 'e'):        print("Engineer")    else:        print("Doctor")         # This code is contributed by# Surendra_Gangwar
// C# program to find// profession of a person// at given level and positionusing System; class GFG{ // Returns 'e' if profession// of node at given level// and position is engineer.// Else doctor. The function// assumes that given position// and level have valid values.static char findProffesion(int level,                           int pos){    // Base case    if (level == 1)        return 'e';     // Recursively find parent's    // profession. If parent    // is a Doctor, this node    // will be a Doctor if it    // is at odd position and an    // engineer if at even position    if (findProffesion(level - 1,                      (pos + 1) / 2) == 'd')        return (pos % 2 > 0) ?                         'd' : 'e';     // If parent is an engineer,    // then current node will be    // an engineer if at add    // position and doctor if even    // position.    return (pos % 2 > 0) ?                     'e' : 'd';} // Driver codepublic static void Main (){    int level = 4, pos = 2;    if(findProffesion(level,                    pos) == 'e')    Console.WriteLine("Engineer");    else    Console.WriteLine("Doctor");}} // This code is contributed// by anuj_67.
<?php// PHP program to find profession// of a person at given level// and position. // Returns 'e' if profession of// node at given level and position// is engineer. Else doctor. The// function assumes that given// position and level have valid values.function findProffesion($level, $pos){    // Base case    if ($level == 1)        return 'e';     // Recursively find parent's    // profession. If parent is    // a Doctor, this node will    // be a doctor if it is at    // odd position and an engineer    // if at even position    if (findProffesion($level - 1,                      ($pos + 1) / 2) == 'd')        return ($pos % 2) ? 'd' : 'e';     // If parent is an engineer, then    // current node will be an engineer    // if at odd position and doctor    // if even position.    return ($pos % 2) ? 'e' : 'd';} // Driver code$level = 4; $pos = 2;if((findProffesion($level,                   $pos) == 'e') == true)    echo "Engineer";else    echo "Doctor" ;     // This code is contributed by ajit?>
<script> // JavaScript program to find// profession of a person// at given level and position // Returns 'e' if profession// of node at given level// and position is engineer.// Else doctor. The function// assumes that given position// and level have valid values.function findProffesion(level,                           pos){    // Base case    if (level == 1)        return 'e';      // Recursively find parent's    // profession. If parent    // is a Doctor, this node    // will be a Doctor if it    // is at odd position and an    // engineer if at even position    if (findProffesion(level - 1,                      (pos + 1) / 2) == 'd')        return (pos % 2 > 0) ?                         'd' : 'e';      // If parent is an engineer,    // then current node will be    // an engineer if at add    // position and doctor if even    // position.    return (pos % 2 > 0) ?                     'e' : 'd';} // Driver Code     let level = 4, pos = 2;    if(findProffesion(level,                      pos) == 'e')    document.write("Engineer");    else    document.write("Doctor");                         </script>
Output : 
Doctor
Time Complexity: O(logn)
Auxiliary Space: O(1) 
Method 2 (Using Bitwise Operators)
Level 1: E
Level 2: ED
Level 3: EDDE
Level 4: EDDEDEED
Level 5: EDDEDEEDDEEDEDDE 
Level input isn’t necessary (if we ignore max position limit) because first elements are same.The result is based on count of 1’s in binary representation of position minus one. If count of 1’s is even then result is Engineer, else then Doctor.And of course position limit is 2^(Level-1)
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find profession of a person at// given level and position.#include<bits/stdc++.h>using namespace std; /* Function to get no of set bits in binary   representation of passed binary no. */int countSetBits(int n){    int count = 0;    while (n)    {      n &= (n-1) ;      count++;    }    return count;} // Returns 'e' if profession of node at given level// and position is engineer. Else doctor. The function// assumes that given position and level have valid values.char findProffesion(int level, int pos){    // Count set bits in 'pos-1'    int c = countSetBits(pos-1);     // If set bit count is odd, then doctor, else engineer    return (c%2)?  'd' : 'e';} // Driver codeint main(void){    int level = 3, pos = 4;    (findProffesion(level, pos) == 'e')? cout << "Engineer"                                       : cout << "Doctor" ;    return 0;}
// Java program to find profession of a person at// given level and position.class GFG{/* Function to get no of set bits in binaryrepresentation of passed binary no. */static int countSetBits(int n){    int count = 0;    while (n!=0)    {    n &= (n-1) ;    count++;    }    return count;} // Returns 'e' if profession of node at given level// and position is engineer. Else doctor. The function// assumes that given position and level have valid values.static char findProffesion(int level, int pos){    // Count set bits in 'pos-1'    int c = countSetBits(pos-1);     // If set bit count is odd, then doctor, else engineer    return (c%2 !=0)? 'd' : 'e';} // Driver codepublic static void main(String [] args){    int level = 3, pos = 4;    String prof = (findProffesion(level, pos) == 'e')? "Engineer"                                    : "Doctor"  ;    System.out.print(prof);}}
# Python3 program to find profession of a person at# given level and position. """ Function to get no of set bits in binary   representation of passed binary no. """def countSetBits(n):    count = 0    while n > 0:      n &= (n-1)      count+=1    return count  # Returns 'e' if profession of node at given level# and position is engineer. Else doctor. The function# assumes that given position and level have valid values.def findProffesion(level, pos):    # Count set bits in 'pos-1'    c = countSetBits(pos-1)      # If set bit count is odd, then doctor, else engineer    if c%2 == 0:        return 'e'    else:        return 'd' level, pos = 3, 4if findProffesion(level, pos) == 'e':    print("Engineer")else:    print("Doctor")         # This code is contributed by divyeshrabadiya07.
using System; // c# program to find profession of a person at // given level and position. public class GFG{/* Function to get no of set bits in binary representation of passed binary no. */public static int countSetBits(int n){    int count = 0;    while (n != 0)    {    n &= (n - 1);    count++;    }    return count;} // Returns 'e' if profession of node at given level // and position is engineer. Else doctor. The function // assumes that given position and level have valid values. public static char findProffesion(int level, int pos){    // Count set bits in 'pos-1'     int c = countSetBits(pos - 1);     // If set bit count is odd, then doctor, else engineer     return (c % 2 != 0)? 'd' : 'e';} // Driver code public static void Main(string[] args){    int level = 3, pos = 4;    string prof = (findProffesion(level, pos) == 'e')? "Engineer" : "Doctor";    Console.Write(prof);}}   // This code is contributed by Shrikant13
<?php// PHP program to find profession// of a person at given level and position. // Function to get no of set// bits in binary representation// of passed binary no.function countSetBits($n){    $count = 0;    while ($n)    {        $n &= ($n - 1) ;        $count++;    }    return $count;} // Returns 'e' if profession of// node at given level and position// is engineer. Else doctor. The// function assumes that given// position and level have valid values.function findProffesion($level, $pos){    // Count set bits in 'pos-1'    $c = countSetBits($pos - 1);     // If set bit count is odd,    // then doctor, else engineer    return ($c % 2) ? 'd' : 'e';} // Driver Code$level = 3;$pos = 4;if((findProffesion($level,                   $pos) == 'e') == true)        echo "Engineer \n";    else        echo "Doctor \n"; // This code is contributed by aj_36?>
<script>     // Javascript program to find    // profession of a person at    // given level and position.         /* Function to get no of set bits in binary    representation of passed binary no. */    function countSetBits(n)    {        let count = 0;        while (n != 0)        {          n &= (n - 1);          count++;        }        return count;    }     // Returns 'e' if profession of node at given level    // and position is engineer. Else doctor.    // The function assumes that given position and    // level have valid values.    function findProffesion(level, pos)    {        // Count set bits in 'pos-1'        let c = countSetBits(pos - 1);         // If set bit count is odd, then doctor,        // else engineer        return (c % 2 != 0)? 'd' : 'e';    }         let level = 3, pos = 4;    let prof = (findProffesion(level, pos) == 'e')?                "Engineer" : "Doctor";    document.write(prof);     </script>
Output : 
Engineer
Time Complexity: O(logn)
Auxiliary Space: O(1)
Thanks to Furkan Uslu for suggesting this method.This article is contributed by Harsh Parikh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
jit_t
vt_m
Smitha Dinesh Semwal
shrikanth13
SURENDRA_GANGWAR
romanbalayan
ravikushwah1
code_hunt
divyesh072019
divyeshrabadiya07
ranjanrohit840
Oracle
Bit Magic
Recursion
Oracle
Recursion
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Builtin functions of GCC compiler
Calculate XOR from 1 to n.
Find two numbers from their sum and XOR
Calculate square of a number without using *, / and pow()
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction