Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
3,494 views

in Company-wise-questions by (130 points)
edited by | 3,494 views

1 Answer

1 like 0 dislike
import java.util.*;
class DESHAW {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int q=sc.nextInt();
        int[]arr=new int[n];
        int[]query=new int[q];
        for(int i=0;i<n;i++) arr[i]=sc.nextInt();
        for(int i=0;i<q;i++) query[i]=sc.nextInt();
        // int[]arr={3,1,4,1,5};
        // int query[]={6,2};
        int max=0;
        for(int x:query) max=Math.max(max,x);
        HashMap<Integer,Deque<Integer>> map=new HashMap<>();
        Deque<Integer> q=new ArrayDeque<>();
        for(int x:arr) q.add(x);
        int x=0;
        ArrayList<Deque<Integer>> ans=new ArrayList<>();
        while(max>=1){
            
            int a=q.pollFirst();
            int b=q.pollFirst();
            
            if(a>b){
               q.addFirst(a);
               q.addLast(b);
            }
            else{
                q.addFirst(b);
                q.addLast(a);
            }
            max--;
            ans.add(new ArrayDeque<>(q));
        }
         ArrayList<ArrayList<Integer>> res=new ArrayList<>();
        for(int i=0;i<query.length;i++){
            Deque<Integer> a=ans.get(query[i]-1);
           ArrayList<Integer> temp=new ArrayList<>();
           temp.add(a.pollFirst());
           temp.add(a.pollFirst());
           res.add(new ArrayList<>(temp));
        }
         System.out.println(res);
    }
        
}
by (160 points)