Finding Position
Easy Accuracy: 63.32% Submissions: 1767 Points: 2

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)

1<= n <=108

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Finding Position

Output Window