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
sujeetkr25082 weeks ago
C++
long long int nthPosition(long long int n){       // code here       if(n==1) return 1;       return nthPosition(n>>1)<<1;   }
0
bhabalomkar4211 month ago
long long int nthPosition(long long int n){       // code here       return pow(2, int(log2(n)));   }
+2
debasishtewary53 months 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);   }
+6
rajsinh21814 months ago
Easy Recursive solution
long long int nthPosition(long long int n){
       if(n==1)
           return 1;
           
       return nthPosition(n/2)*2;
   }
0
abhishekbajpaiamity5 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
aminul015 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
harshitsharma60915 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_wahi5 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
programmingdopractice6 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_990i6 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;   }};
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.