K Largest Element

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
325 upvotes
Asked in companies
Tata Consultancy Services (TCS)AmazonWalmart

Problem statement

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.

Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
2
4 2
3 4 2 1
5 1
2 2 3 3 1
Sample Output 1:
3 4
3
Explanation for sample input 1:
Test case 1:
If we sort the array then it will look like: [1, 2, 3, 4]. The 2 largest elements will be [3, 4].

Test case 2:
If we sort the array then it will look like: [1, 2, 2, 3, 3]. Then the largest element will be [3].
Sample Input 2:
2
5 5
0 10 1 2 2
6 2
-2 12 -1 1 20 1 
Sample Output 2:
0 1 2 2 10
12 20
Hint

Use some modified sorting algorithm.

Approaches (4)
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 element to the end of the remaining unsorted array and an outer loop for performing that operation ‘N’ so that array becomes sorted.
  • If we run the outer loop for ‘K’ times then we have ‘K’ largest element at the end of the array. Thus we simply take those ‘K’ elements and return them because they are already sorted in non-decreasing order as well.
Time Complexity

O(N*K), where ‘N’ is the number of elements present in the array and ‘K’ is the count of the largest elements.

As we ran two nested outer loops till K and inner loop till ‘N’. Hence, the overall time complexity is O(N*K).

Space Complexity

O(1).

The output array does not count as extra space for the purpose of space complexity analysis. Hence, the overall space complexity is O(1). 

Code Solution
(100% EXP penalty)
K Largest Element
Full screen
Console