Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Friday, June 9, 2017

Find position of non selected person geeks


Problem : 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


input  : Test case t followed by no. of people in row
Output : position
Explanation : it is a mathematical approach that last 2^k which is lower than  n is answer



import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int n=ab.nextInt();
   System.out.println((int)Math.pow(2, (int)(Math.log(n)/Math.log(2))));
}
}
}

0 Comments:

Post a Comment

Stats