Find profession in a special family | Java 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 . ; 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 . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static char findProffesion ( int level , int pos ) { int c = countSetBits ( pos - 1 ) ; return ( c % 2 != 0 ) ? ' d ' : ' e ' ; } public static void main ( String [ ] args ) { int level = 3 , pos = 4 ; String prof = ( findProffesion ( level , pos ) == ' e ' ) ? " Engineer " : " Doctor " ; System . out . print ( prof ) ; } }