Finding Position
PROBLEM :
Some people 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.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,number of people standing in a queue.
Output:
Print the position(original queue) of that person who is left.
Constraints:
1 = T = 1000
1 = N = 100000000
Example:
Input
1
5
Output
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
int Finding_Position(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Finding_Position(no)<<endl ;
}
return 0;
}
int Finding_Position(int no)
{
int i ;
i=1 ;
while(pow(2,i)<=no)
i++ ;
return pow(2,i-1) ;
}
---------------------------------------------------------------------------------
Some people 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.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,number of people standing in a queue.
Output:
Print the position(original queue) of that person who is left.
Constraints:
1 = T = 1000
1 = N = 100000000
Example:
Input
1
5
Output
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
int Finding_Position(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Finding_Position(no)<<endl ;
}
return 0;
}
int Finding_Position(int no)
{
int i ;
i=1 ;
while(pow(2,i)<=no)
i++ ;
return pow(2,i-1) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment