K Largest Elements
You are given with an integer k and an array of integers that contain numbers in random order. Write a program to find k largest numbers from given array. You need to save them in an array and return it.
Time complexity should be O(nlogk) and space complexity should be not more than O(k).
Sample Input :
13
2 12 9 16 10 5 3 20 25 11 1 8 6
4
Sample Output :
12
16
20
25
You are given an unsorted array containing ‘N’ integers. You need to find ‘K’ largest elements from the given array. Also, you need to return the elements in non-decreasing order.
Input Format:
The first line of the input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated positive integers ‘N’ and ‘K’ denoting the number of the elements present in the array and count of the largest elements you need to return as the answer respectively.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
The only line of output of each test case should contain ‘K’ largest elements in the array in non-decreasing order.
Print the output of each test case in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Follow Up:
Can you solve this in less than O(N*log(N)) time complexity?
Constraints:
1 <= T <= 100
1 <= N <= 10^4
1<= K <= N
-10^9 <= ARR[i] <= 10^9
Where ‘T’ is the number of test cases, ‘N’ is the size of the array, ‘K’ is the number of elements you need to return as an answer and ARR[i] is the size of the array of elements.
Time Limit: 1 sec
AnswerBot
1y
Given an unsorted array, find the K largest elements in non-decreasing order.
Sort the array in non-decreasing order.
Return the last K elements of the sorted array.
CodingNinjas
author
2y
import java.util.ArrayList;
import java.util.PriorityQueue;
public class Solution {
public static ArrayList kLargest(int input[], int k) {
int i=0;
PriorityQueue pq=new PriorityQueue<>();
ArrayList arr=...read more
CodingNinjas
author
2y
Bubble sort
- We can use bubble sort or any other sorting algorithm with some changes to solve this problem.
- In bubble sort we have two loops:- inner loop for swapping the elements and take the highest el...read more
CodingNinjas
author
2y
Sorting
- We can improve our sorting using some efficient sorting algorithms like Merge sort or Heap sort etc.
- Sort the whole array in decreasing order and take out the first ‘K’ elements one by one to re...read more
CodingNinjas
author
2y
Heap data structure
- We can use the Heap data structure to store the elements in the sorted order. The problem can be solved by both Min heap and Max heap but for simplicity, we will Min heap.
- We will in...read more
CodingNinjas
author
2y
Quick Select
- We can break this problem into two parts:
- In the first part, we find the Kth largest element in the array.
- In the second part, we take ‘K’-1 elements which are greater than or equal to Kth l...read more
Add answer anonymously...
Top Persistent Systems Senior Software Engineer interview questions & answers
Popular interview questions of Senior Software Engineer
Top HR questions asked in Persistent Systems Senior Software Engineer
>
Persistent Systems Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app