Finding Position | Practice | GeeksforGeeks

Some people(n) are standing in a queue. A selection process follows a rule where people standing on even positions are selected. Of the selected people a queue is formed and again out of these only people on even position are selected. This continues until we are left with one person. Find out the position of that person in the original queue.

Example 1:
Input: n = 5
Output: 4 
Explanation: 1,2,3,4,5 -> 2,4 -> 4.


Example 2:
Input: n = 9
Output: 8
Explanation: 1,2,3,4,5,6,7,8,9
->2,4,6,8 -> 4,8 -> 8. 


Your Task:  
You dont need to read input or print anything. Complete the function nthPosition() which takes n as input parameter and returns the position(original queue) of that person who is left.

Expected Time Complexity: O(logn)
Expected Auxiliary Space: O(1)

Constraints:
1<= n <=108
0
debasishtewary53 weeks ago
0.11/15.52
static long find(long i,long n){       if(i>n)       return i/2;       else{           i=i*2;           return find(i,n);       }   }   static long nthPosition(long n){       // code here      return Solution.find(1,n);   }
0
rajsinh21812 months ago
Easy Recursive solution
long long int nthPosition(long long int n){
       if(n==1)
           return 1;
           
       return nthPosition(n/2)*2;
   }
0
abhishekbajpaiamity2 months ago
class Solution {   static long nthPosition(long n){       int value=(int)(Math.log(n) /Math.log(2));      int pow=(int)Math.pow(2,value);      return pow;   }}
0
aminul012 months ago
Simple java solution 0.1 time taken
class Solution {   static long nthPosition(long n){       // code here       long ans = 1;       while (n != 1) {           ans = 2 * ans;           n /= 2;                  }       return ans;   }}
0
harshitsharma60913 months ago
 long long int nthPosition(long long int n)   {      if(n<2) return 0;            while(( n & (n-1)) !=0)      {          n=n&(n-1);      }      return n;   }
0
uday_wahi3 months ago
java simple one liner solution
time complexity : O(1)
time 0.1/15.5
 
static long nthPosition(long n){
       return (long) Math.pow(2,(long) (Math.log(n)/Math.log(2)));
   }
0
programmingdopractice3 months ago
long long int nthPosition(long long int n){
        // code here
        int len = log2(n);
        return pow(2,len);
    }
    Didn't do anything
0
user_990i4 months ago
Iterative solution in 0 sec execution
class Solution { public:   long long int nthPosition(long long int n){     long long int count =1;   while(n!=1){       count=2*count;       n=n/2;   }   return count;   }};
0
user_990i4 months ago
class Solution { public:   long long int nthPosition(long long int n){          if(n==1){       return 1;   }   return nthPosition(n/2)*2;   }};
+1
ankulchaudhary244 months ago
A simlple and very efficeint recursive solution
  long long int nthPosition(long long int n){       if(n==1)       return 1;       else       return 2*nthPosition(n/2);   }
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.