Java | Priority Queue | Easy Explained
class Solution {\n    public int[] maxSubsequence(int[] nums, int k) {\n        \n        // Create proirity queue with min priority queue so that min element w
class Solution {\n    public int[] maxSubsequence(int[] nums, int k) {\n        \n        // Create proirity queue with min priority queue so that min element will be removed first, with index\n        // Add those unique index in a set\n        // Loop from 0 to n-1 and add element in result if set contains those index\n        // For ex. set has index 3,5,6 Just add those element. Order will be maintained\n            \n     \n       // We are defining the min priority queue  \n       PriorityQueue<int[]> q = new PriorityQueue<>((a,b)-> (a[0]-b[0])); \n        \n        // Add element with index to priority queue\n        for(int i=0; i<nums.length; i++)\n        {\n            q.offer(new int[]{nums[i],i});\n            if(q.size()> k)\n            {\n                q.poll();\n            }\n        }\n        \n        // Set to keep index\n        Set<Integer> index = new HashSet<>();\n        \n        \n        // Add the index in the set since index are unique \n        while(!q.isEmpty())\n        {\n            int[] top = q.poll();\n            index.add(top[1]);\n        }\n        \n        // Final result add here\n        int[] result = new int[k];\n        \n        \n        // Just add the element in the result for those index present in SET\n        int p =0;\n        for(int i=0; i< nums.length; i++)\n        {\n            if(index.contains(i))\n            {\n                result[p] = nums[i];\n                ++p;\n            }\n        }\n        \n        return result;\n    }\n}
['Heap (Priority Queue)', 'Java']