Question
Asha has got a set of integers and he is thinking about what interesting problem statement could be formed from these integers. And Eureka! She found one!
The problem statement goes as follow:
You have to find the maximum integer among the set of integers and divide the integer by 2.
Since this is an integer division the result of division will be an integer.
After division if the integer is greater than 0, then we put the updated integer again in the set of integers.
Asha gives you m queries, each query q[i] means, that q[q[i]−1] number of operations has already been performed, and its time to perform q[i]th operation. Your task is to tell that for q[i]th operation which integer will get divided.
Input Format
The first line contains 2 space-separated integers n,m
denoting the number of integers and the number of queries respectively.
The next line contains n space-separated integers followed by m lines.
In the next m lines, the ith line will contain q[i].
Output format
Output m lines, the ith of which contains the answer for the ith query.
Constraints
1<=n,m<=10^6
1<=n,m<=10^6
For 1<=i<=m1<=i<=m,
q[i]>q[i−1].
For 0<=i<=n0<=i<=n,
1<=a[i]<=1091<=a[i]<=109
If no integer is present at the time of the query stop the program.
public class Problem
{
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“enter number of elements”);
int n=Integer.parseInt(br.readLine());
System.out.println(“enter number of constraints”);
int m=Integer.parseInt(br.readLine());
int arr[]=new int[n];
int div[]=new int[m];
System.out.println(“enter elements”);
for(int i=0;i<n;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println(“enter constraints”);
for(int j=0;j<m;j++)
{
div[j]=Integer.parseInt(br.readLine());
}
for(int k=0;k<m;k++)
{
//System.out.println(div[k]);
Arrays.sort(arr);
System.out.print(prob(arr,div[k]));
}
}
public static int prob(int a[], int b)
{
int[] arr2=new int[a.length];
for(int j=0;j<a.length;j++)
{
arr2[j]=a[j];
}
for(int i=0;i<b-1;i++)
{
arr2[arr2.length-1]= arr2[arr2.length-1]/2;
Arrays.sort(arr2);
}
return arr2[arr2.length-1];
}