Hướng dẫn find k largest elements in array python - tìm k phần tử lớn nhất trong mảng python

Viết một chương trình hiệu quả để in k các yếu tố lớn nhất trong một mảng. Các yếu tố trong một mảng có thể theo bất kỳ thứ tự nào. Ví dụ: Nếu mảng đã cho là [1, 23, 12, 9, 30, 2, 50] và bạn được yêu cầu 3 yếu tố lớn nhất, tức là, k = 3 sau đó chương trình của bạn Nên in 50, 30 và 23.
For example: if the given array is [1, 23, 12, 9, 30, 2, 50] and you are asked for the largest 3 elements i.e., k = 3 then your program should print 50, 30, and 23.

Phương pháp 1 (sử dụng bong bóng k lần) & nbsp; cảm ơn Shailendra đã đề xuất phương pháp này. & Nbsp; 1. Độ phức tạp thời gian: O (n*k) & nbsp; 
Thanks to Shailendra for suggesting this approach. 
1) Modify Bubble Sort to run the outer loop at most k times. 
2) Print the last k elements of the array obtained in step 1.
Time Complexity: O(n*k) 

Giống như sắp xếp bong bóng, các thuật toán sắp xếp khác như sắp xếp lựa chọn cũng có thể được sửa đổi để có được các yếu tố K lớn nhất.

Phương pháp 2 (sử dụng mảng tạm thời) & nbsp; k các yếu tố lớn nhất từ ​​ARR [0..N-1] 
K largest elements from arr[0..n-1]

1) Lưu trữ các phần tử k đầu tiên trong nhiệt độ mảng tạm thời [0..K-1]. & Nbsp; 2) Tìm phần tử nhỏ nhất trong temp [], hãy để phần tử nhỏ nhất là tối thiểu. & Nbsp; 3-a) cho mỗi phần tử x trong mảng [k] đến mảng [n-1]. O (n-k) & nbsp; Nếu x lớn hơn tối thiểu thì hãy xóa tối thiểu khỏi temp [] và chèn x. & Nbsp; 3-b) thì hãy xác định tối thiểu mới từ temp []. O (k) & nbsp; 4) in các yếu tố k cuối cùng của temp []
2) Find the smallest element in temp[], let the smallest element be min. 
3-a) For each element x in arr[k] to arr[n-1]. O(n-k) 
If x is greater than the min then remove min from temp[] and insert x. 
3-b)Then, determine the new min from temp[]. O(k) 
4) Print final k elements of temp[]

Độ phức tạp về thời gian: O ((n-k)*k). Nếu chúng ta muốn đầu ra được sắp xếp thì O ((n-k)*k + k*log (k)) nhờ nesamani1822 vì đã đề xuất phương pháp này. & Nbsp;
Thanks to nesamani1822 for suggesting this method. 

Phương pháp 3 (sử dụng sắp xếp) & nbsp; 1) Sắp xếp các phần tử theo thứ tự giảm dần trong o (n*log (n)) & nbsp; 2) in các số k đầu tiên của mảng được sắp xếp o (k). & Nbsp; 
1) Sort the elements in descending order in O(n*log(n)) 
2) Print the first k numbers of the sorted array O(k). 

Sau đây là việc thực hiện các điều trên. & Nbsp; & nbsp;

C++

#include <bits/stdc++.h>

using namespace std;

void kLargest(int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
7int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
Top 2 Elements: 787 23 
4

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
6
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
9

int #include <bits/stdc++.h>1

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>8#include <bits/stdc++.h>9using0#include <bits/stdc++.h>9using2

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using7

Top 2 Elements: 787 23 
9

C

using9

namespace0

int namespace2namespace3 voidnamespace5namespace3

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Các

Top 2 Elements: 787 23 
9

void kLargest(int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void8void9#include <bits/stdc++.h>9
Top 2 Elements: 787 23 
2intkLargest(3

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
Top 2 Elements: 787 23 
4

Top 2 Elements: 787 23 
5int0
Top 2 Elements: 787 23 
2int2int3

Top 2 Elements: 787 23 
9

int #include <bits/stdc++.h>1

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>8#include <bits/stdc++.h>9using0#include <bits/stdc++.h>9using2

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using7

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 9

C

int namespace2namespace3 voidnamespace5namespace3

Các

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
15

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
32

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
17

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
41
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
19

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
57
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
61

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
17

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
74

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
19

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
26int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
4

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
39

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
49int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
51int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
4

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
12

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
63
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
65

Top 2 Elements: 787 23 
30
Top 2 Elements: 787 23 
31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 5std;1 3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 77

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
84

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
88
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
90__1919

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
99
Top 2 Elements: 787 23 
00
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
92______202

Top 2 Elements: 787 23 
5int
Top 2 Elements: 787 23 
08
Top 2 Elements: 787 23 
09
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5int
Top 2 Elements: 787 23 
15__191919192

Python

Top 2 Elements: 787 23 
74

C#

Top 2 Elements: 787 23 
35
Top 2 Elements: 787 23 
36

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
15

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
91

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
93

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
17

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40#include <bits/stdc++.h>00
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void #include <bits/stdc++.h>09

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5int#include <bits/stdc++.h>14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 int#include <bits/stdc++.h>17

#include <bits/stdc++.h>18#include <bits/stdc++.h>19

Top 2 Elements: 787 23 
5int using5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
12

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

PHP

#include <bits/stdc++.h>28

Is

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6#include <bits/stdc++.h>39#include <bits/stdc++.h>31
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2#include <bits/stdc++.h>45 #include <bits/stdc++.h>46#include <bits/stdc++.h>45

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>54 #include <bits/stdc++.h>31
Top 2 Elements: 787 23 
56#include <bits/stdc++.h>45#include <bits/stdc++.h>58
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>31 #include <bits/stdc++.h>63#include <bits/stdc++.h>64#include <bits/stdc++.h>65

#include <bits/stdc++.h>66#include <bits/stdc++.h>67

#include <bits/stdc++.h>33 #include <bits/stdc++.h>69#include <bits/stdc++.h>31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

#include <bits/stdc++.h>35 #include <bits/stdc++.h>73

kLargest(#include <bits/stdc++.h>31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
92#include <bits/stdc++.h>33
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
92#include <bits/stdc++.h>35
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

#include <bits/stdc++.h>81

JavaScript

#include <bits/stdc++.h>82

#include <bits/stdc++.h>29 #include <bits/stdc++.h>84

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6#include <bits/stdc++.h>87

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1 #include <bits/stdc++.h>90

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>92
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6#include <bits/stdc++.h>97

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6#include <bits/stdc++.h>99

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using01

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using7

using04

Hướng dẫn find k largest elements in array python - tìm k phần tử lớn nhất trong mảng python

Độ phức tạp về thời gian: O (n*log (n)) Không gian phụ trợ: O (1) O(n*log(n))
Auxiliary Space: O(1)

Phương pháp 4 (sử dụng HEAP tối đa) & NBSP; 1) Xây dựng cây HEAP tối đa trong O (N*log (n)) & nbsp; 2) N)) 
1) Build a Max Heap tree in O(n*log(n)) 
2) Use Extract Max k times to get k maximum elements from the Max Heap O(k*log(n))

Độ phức tạp về thời gian: O (n*log (n) + k*log (n)) & nbsp; O(n*log(n) + k*log(n)) 

Phương pháp 5 (sử dụng thống kê thứ tự) & nbsp; 1) sử dụng thuật toán thống kê thứ tự để tìm phần tử lớn nhất thứ k. Vui lòng xem lựa chọn chủ đề trong trường hợp xấu nhất thời gian tuyến tính o (n) & nbsp; 2) phần tử lớn nhất kth) o (k*log (k)). Bước này chỉ cần thiết nếu đầu ra được sắp xếp là bắt buộc. 
1) Use an order statistic algorithm to find the kth largest element. Please see the topic selection in worst-case linear time O(n) 
2) Use QuickSort Partition algorithm to partition around the kth largest number O(n). 
3) Sort the k-1 elements (elements greater than the kth largest element) O(k*log(k)). This step is needed only if the sorted output is required.

Độ phức tạp về thời gian: O (n) nếu chúng ta không cần đầu ra được sắp xếp, nếu không thì O (N+K*log (k)) cảm ơn Shilpi đã đề xuất hai cách tiếp cận đầu tiên. O(n) if we don’t need the sorted output, otherwise O(n+k*log(k))
Thanks to Shilpi for suggesting the first two approaches.

Phương pháp 6 (sử dụng Min Heap) & NBSP; Phương pháp này chủ yếu là tối ưu hóa phương pháp 2. Thay vì sử dụng mảng temp [] [K-1]) của mảng đã cho. O (k*log (k)) 2) cho mỗi phần tử, sau phần tử KTH (ARR [K] với ARR [N-1]), so sánh nó với gốc của MH. & NBSP; lớn hơn gốc sau đó làm cho nó root và gọi Heapify cho MH & nbsp; và gốc của MH là phần tử lớn nhất KTH. Độ phức tạp của thời gian: O (K*log (k) + (n-k)*log (k)) mà không có đầu ra được sắp xếp. Nếu đầu ra được sắp xếp là cần, thì o (k*log (k) + (n-k)*log (k) + k*log (k)) vì vậy tổng thể nó là o (k*log (k) + (n-k)*log ( k)) 
This method is mainly an optimization of method 2. Instead of using temp[] array, use Min Heap.
1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k*log(k))
2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH. 
……a) If the element is greater than the root then make it root and call heapify for MH 
……b) Else ignore it. 
// The step 2 is O((n-k)*log(k))
3) Finally, MH has k largest elements, and the root of the MH is the kth largest element.
Time Complexity: O(k*log(k) + (n-k)*log(k)) without sorted output. If sorted output is needed then O(k*log(k) + (n-k)*log(k) + k*log(k)) so overall it is O(k*log(k) + (n-k)*log(k))

Tất cả các phương pháp trên cũng có thể được sử dụng để tìm phần tử lớn nhất (hoặc nhỏ nhất).

C++

using05

using namespace std;

int using10intusing12intusing14

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using18

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using20

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using22

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20 using25

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using28

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6intusing31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23using33

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using35int using37int using39

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void using42int using44

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void using47

Top 2 Elements: 787 23 
05

using49int using37int using53

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using56using57

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using56using60

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using47

Top 2 Elements: 787 23 
9

void using65int using67

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70 using71

Top 2 Elements: 787 23 
5std;1
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using77

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using80

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using83

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using85

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70 using88

Top 2 Elements: 787 23 
5using90

using91using92

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70 using95

Top 2 Elements: 787 23 
5using97

Top 2 Elements: 787 23 
5using99

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

void namespace04

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 namespace10

Top 2 Elements: 787 23 
5namespace12

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

void namespace17int namespace19int namespace21int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace25
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 namespace27

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int namespace32

Top 2 Elements: 787 23 
5using70 namespace35

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace37
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace45

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int namespace54

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
6
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

int #include <bits/stdc++.h>1

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int namespace67

namespace68namespace69

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int namespace72#include <bits/stdc++.h>9using0#include <bits/stdc++.h>9using2

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace81

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1 namespace84

Top 2 Elements: 787 23 
9

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace87

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace89

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20 namespace91

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void namespace17int namespace19

namespace98int namespace21

namespace98int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
4

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;06
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 std;08

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
222
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
39

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5std;19

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
222 std;26

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5using70 std;31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace37
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5namespace40

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40std;40

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40std;19

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;48

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50 std;51

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5std;55
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void std;64

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int std;68std;69__192

Các

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int std;96

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
08
Top 2 Elements: 787 23 
09
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void03

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Python3

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 void07

Top 2 Elements: 787 23 
35 void09

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void11
Top 2 Elements: 787 23 
39 void13

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
Top 2 Elements: 787 23 
47

Top 2 Elements: 787 23 
5void21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void23

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46void29

Top 2 Elements: 787 23 
5using70 void32
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38void34

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace37

Top 2 Elements: 787 23 
5namespace40using33

Các

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40void50

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40void21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40void23

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45 void59

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
49void62
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
41

Top 2 Elements: 787 23 
54
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
56std;69__

void93

Top 2 Elements: 787 23 
39 void95void96

Top 2 Elements: 787 23 
71
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
09

kLargest(00

C#

using

Top 2 Elements: 787 23 
76

using kLargest(04

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20 kLargest(07

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void namespace17int kLargest(14

namespace98int namespace21

namespace98int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
4

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;06
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 std;08

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
222
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
39

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5kLargest(38

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
222 std;26

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5kLargest(49

Top 2 Elements: 787 23 
5using70 std;31

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace37
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50 std;51

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40kLargest(61

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40kLargest(38

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void std;64

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5kLargest(78
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int std;68std;69__192

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Các

std;83namespace69

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int std;96

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void03

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 6int Top 2 Elements: 787 23 08Top 2 Elements: 787 23 09Top 2 Elements: 787 23 8

#include <bits/stdc++.h>82

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 void07

Top 2 Elements: 787 23 
35 void09

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void11
Top 2 Elements: 787 23 
39 void13

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int17

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
Top 2 Elements: 787 23 
47

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int26

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46void29

#include <bits/stdc++.h>66namespace37

Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5using70 void32
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38void34

#include <bits/stdc++.h>66int37

#include <bits/stdc++.h>66int39

#include <bits/stdc++.h>66int37

Các

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45 void59

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int54
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
49void62
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
41

std;83int65

Top 2 Elements: 787 23 
54
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
56std;69__

void93

Top 2 Elements: 787 23 
39 void95void96

Top 2 Elements: 787 23 
5void03

using04

Top 2 Elements: 787 23 
71
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
09
O(nlogn)
Auxiliary Space: O(n)

using

Top 2 Elements: 787 23 
76

  1. using kLargest(04
  2. 3 largest elements are : 50 88 96 
    3 smallest elements are : 3 2 1 
    23
    3 largest elements are : 50 88 96 
    3 smallest elements are : 3 2 1 
    20 kLargest(07
  3. Nếu k == pivot_index: In mảng (thấp đến trục để có các yếu tố kmallest k và (n-pivot_index) thành n cho các yếu tố lớn nhất k)
  4. if & nbsp; k> pivot_index: Lặp lại các bước cho phần phải.

Chúng ta có thể cải thiện thuật toán QuickSort tiêu chuẩn bằng cách sử dụng hàm ngẫu nhiên (). Thay vì sử dụng phần tử trục làm phần tử cuối cùng, chúng ta có thể chọn ngẫu nhiên phần tử trục. Độ phức tạp thời gian trong trường hợp xấu nhất của phiên bản này là O (N2) và độ phức tạp thời gian trung bình là O (N).

Sau đây là việc thực hiện thuật toán trên:

C++

#include <bits/stdc++.h>

using namespace std;

int int80int int82int int84int int86

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int90

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int93

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
003

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
005

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
011

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

Top 2 Elements: 787 23 
9

int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
030

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
032

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
049

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
055

int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
030

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

Top 2 Elements: 787 23 
5std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
045

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
047

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
058

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
090
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
Top 2 Elements: 787 23 
8

void

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
061int int82int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
065______

#include <bits/stdc++.h>66

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
099
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
103

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
090
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
070int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
072

#include <bits/stdc++.h>66

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
099
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
124

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
130

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
085

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
088

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
097

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
112

Top 2 Elements: 787 23 
5namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
122

Top 2 Elements: 787 23 
5namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
128

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
157

int #include <bits/stdc++.h>1

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 6int 3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 139

using9

namespace0

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>8#include <bits/stdc++.h>9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
1444#include <bits/stdc++.h>9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
146

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
149

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
175

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
177

Top 2 Elements: 787 23 
9

int int80int int82int int84int int86

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int90

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int93

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
203

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
005

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
211

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

Top 2 Elements: 787 23 
9

int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
030

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
232

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
247

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
049

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
255

int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
030

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

Top 2 Elements: 787 23 
5std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
045

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
047

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
058

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int0
Top 2 Elements: 787 23 
2
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
292
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
293

void

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
061int int82int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
065______

#include <bits/stdc++.h>66int0

Top 2 Elements: 787 23 
2int2
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
303

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int0
Top 2 Elements: 787 23 
2
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
307
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40int0
Top 2 Elements: 787 23 
2
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
312
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
293

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
070int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
072

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
124

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
130

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
085

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
088

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int #include <bits/stdc++.h>8#include <bits/stdc++.h>9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
1444#include <bits/stdc++.h>9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
146

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
149

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
152

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int using5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
157

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1 namespace84

Top 2 Elements: 787 23 
9

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace89

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20 namespace91

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int int80int int82int int84__

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int90

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int93

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
401

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
403

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
405

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
005

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
416

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
418

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
438
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
443int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
445
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
446
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
447

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
450

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
452

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
454

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9 int98

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
401

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
472

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
474

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
049

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
485

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
487

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017______

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
438
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
443int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
445
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
446
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
447

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
450

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396std;1
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
045

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
470

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
526
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
483

Top 2 Elements: 787 23 
30
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
537
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
541

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
526
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
058

Top 2 Elements: 787 23 
30
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
537
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
061int int82int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
065__

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
562
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
564

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
505int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
072

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
570
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
572

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
085

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
088

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
535

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
157

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Python3

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
112

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
122

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
128

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
84

Các

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
613

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
616
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
621
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
08
Top 2 Elements: 787 23 
09
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
635

Top 2 Elements: 787 23 
35
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
637

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
639
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
641

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
643
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
645

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
651

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
654
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
656

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
658
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
660

Các

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
613

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
616
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
621
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
08
Top 2 Elements: 787 23 
09
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
635

Top 2 Elements: 787 23 
35
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
637

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
639
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
641

Top 2 Elements: 787 23 
5std;1

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40using33

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
643
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
645

Top 2 Elements: 787 23 
5using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
756
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
759

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
2222

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
776

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
779
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
Top 2 Elements: 787 23 
41

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
785

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
2222

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
802

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
807
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
Top 2 Elements: 787 23 
41

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
812
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
813

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
815void46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
818

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
812
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
821

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
823
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
663
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
826

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
827
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
829std;69__192

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
676
Top 2 Elements: 787 23 
39 void95
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
857

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
858
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
861
Top 2 Elements: 787 23 
39
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
676void46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91

Top 2 Elements: 787 23 
71
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
09

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
869

C#

using

Top 2 Elements: 787 23 
76

using

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
873

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int int80int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
882int int84__

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int90

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int int93

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int int98

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
903

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
905

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
907

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
005

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
416

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
418

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
931int int84int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
886

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
940
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
942

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int09
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
945

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
948

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
450

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
953

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
955

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int int98

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
903

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
973

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
474

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
049

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
986

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
487

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
931int int84int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
886

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396std;1
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
027

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
940
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
942

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int09
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
945

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
024
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
948

Top 2 Elements: 787 23 
30
Top 2 Elements: 787 23 
033
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
037

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
024
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
450

Top 2 Elements: 787 23 
30
Top 2 Elements: 787 23 
033
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
035

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
045

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
130

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
470

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
483

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
058

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
061int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
882______

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
157

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 396int 3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 085

#include <bits/stdc++.h>82

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
088

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
097

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
101

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
103

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
112

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
111

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
403

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
405

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
005

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
123

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
416

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
418

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
014

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
017int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
931int int84int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
886

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
139

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
141

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
143

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
452

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
454

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
149

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
037

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
112

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
001

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
159

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
472

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
474

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
049

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
171

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
485

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
487

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
058

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46#include <bits/stdc++.h>29
Top 2 Elements: 787 23 
183

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
505
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
072

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
076

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396std;1
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
198

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
088

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
203
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
208

Top 2 Elements: 787 23 
209
Top 2 Elements: 787 23 
210
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
214
Top 2 Elements: 787 23 
215
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
203
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
223

Top 2 Elements: 787 23 
30
Top 2 Elements: 787 23 
210
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
100
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
122

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
124

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396namespace40 using70
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
128

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
130

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
46
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
247

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
249

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
251

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
253

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using01

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
157

using04

Đầu ra

3 smallest elements are : 3  2  1  
3 largest elements are : 96  50  88  

Độ phức tạp về thời gian: O (NLOGN) Không gian phụ trợ: O (1)O(nlogn)
Auxiliary Space: O(1)

Phương pháp 8 (Sử dụng Thư viện STL hàng đợi ưu tiên): Trong phương pháp này, chúng ta có thể in hiệu quả các phần tử K lớn nhất/nhỏ nhất của một mảng sử dụng hàng đợi ưu tiên trong độ phức tạp thời gian O (N*log (k)). Đầu tiên, chúng tôi đẩy các phần tử K vào hàng đợi ưu tiên từ mảng. Từ đó trở đi, sau mỗi lần chèn một phần tử mảng, chúng tôi sẽ bật phần tử ở đầu ưu tiên_queue. Trong trường hợp của yếu tố K lớn nhất, Priority_queue sẽ theo thứ tự ngày càng tăng và do đó, hầu hết các yếu tố hàng đầu sẽ là nhỏ nhất nên chúng tôi sẽ loại bỏ nó. Tương tự, trong trường hợp phần tử nhỏ nhất k, ưu tiên_queue theo thứ tự giảm và do đó phần tử hàng đầu nhất là phần lớn nhất vì vậy chúng tôi sẽ loại bỏ nó. Trong thời trang này, toàn bộ mảng được đi qua và hàng đợi ưu tiên có kích thước k được in có chứa k lớn nhất/nhỏ nhất.
In this approach, we can efficiently print the k largest/smallest elements of an array using a priority queue in O(n*log(k)) time complexity. First, we push k elements into the priority queue from the array. From there on, after every insertion of an array element, we will pop the element at the top of priority_queue. In the case of the k largest element, the priority_queue will be in increasing order, and thus top most element will be the smallest so we are removing it. Similarly, in the case of the k smallest element, the priority_queue is in decreasing order and hence the top most element is the largest one so we will remove it. In this fashion whole array is traversed and the priority queue of size k is printed which contains k largest/smallest elements.

Dưới đây là việc thực hiện phương pháp trên:

C++

#include <bits/stdc++.h>

using namespace std;

void

Top 2 Elements: 787 23 
264int
Top 2 Elements: 787 23 
266int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
273int
Top 2 Elements: 787 23 
275int
Top 2 Elements: 787 23 
277int
Top 2 Elements: 787 23 
279

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
Top 2 Elements: 787 23 
284

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
286

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
289

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
291

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50
Top 2 Elements: 787 23 
298

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
302
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
291

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
310

Top 2 Elements: 787 23 
9

void

Top 2 Elements: 787 23 
313int
Top 2 Elements: 787 23 
266int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
273int
Top 2 Elements: 787 23 
324

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
Top 2 Elements: 787 23 
284

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
286

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
289

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
291

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50
Top 2 Elements: 787 23 
298

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
302
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

void

Top 2 Elements: 787 23 
313int
Top 2 Elements: 787 23 
266int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
291

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
359int
Top 2 Elements: 787 23 
361

int #include <bits/stdc++.h>1

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
364

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
369
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6using7

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
369
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
379

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 3 smallest elements are : 3 2 1 6int using5

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace89

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
398

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void kLargest(int int82int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
2int

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40#include <bits/stdc++.h>63
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
Top 2 Elements: 787 23 
402

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
411

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
409

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
416

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40using70
Top 2 Elements: 787 23 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
425
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
416

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
541

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5std;50
Top 2 Elements: 787 23 
423

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
398

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
Top 2 Elements: 787 23 
439__ int82int

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
455

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40#include <bits/stdc++.h>63
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
Top 2 Elements: 787 23 
402

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
411

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
409

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
416

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40using70
Top 2 Elements: 787 23 
414

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
425
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
416

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
84

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5int
Top 2 Elements: 787 23 
496

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
498std;69__192

Top 2 Elements: 787 23 
5int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
613

Top 2 Elements: 787 23 
5int
Top 2 Elements: 787 23 
08
Top 2 Elements: 787 23 
09
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
532
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
536

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
532
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
542

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Python3

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14
Top 2 Elements: 787 23 
547

Top 2 Elements: 787 23 
35
Top 2 Elements: 787 23 
549

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
551
Top 2 Elements: 787 23 
39 void13

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
555

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
Top 2 Elements: 787 23 
561

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
563

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
2void95
Top 2 Elements: 787 23 
568

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
570

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50__22222222

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
581
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
583
Top 2 Elements: 787 23 
41

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
785

Top 2 Elements: 787 23 
35
Top 2 Elements: 787 23 
589

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
551
Top 2 Elements: 787 23 
39 void13

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
44
Top 2 Elements: 787 23 
45
Top 2 Elements: 787 23 
46
Top 2 Elements: 787 23 
561

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
601void46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
689
Top 2 Elements: 787 23 
605

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
2void95
Top 2 Elements: 787 23 
568

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
570

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;50__22222222

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
623
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
689void46
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
91
Top 2 Elements: 787 23 
627
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
583
Top 2 Elements: 787 23 
41

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
785

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
49
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
785

Top 2 Elements: 787 23 
35
Top 2 Elements: 787 23 
589

Top 2 Elements: 787 23 
54
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
56std;69__

Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
669
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
106
Top 2 Elements: 787 23 
627
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
673

#include <bits/stdc++.h>84

Top 2 Elements: 787 23 
49
Top 2 Elements: 787 23 
669
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
091
Top 2 Elements: 787 23 
627
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
673

Top 2 Elements: 787 23 
681

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
676
Top 2 Elements: 787 23 
39 void95void96

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 

Top 2 Elements: 787 23 
71
Top 2 Elements: 787 23 
39
Top 2 Elements: 787 23 
09
O(n*log(k))
Auxiliary Space: O(k)

Đầu ra

Độ phức tạp về thời gian: O (n*log (k)) Không gian phụ trợ: O (k)
 

C++

#include <bits/stdc++.h>

Phương pháp 9 (tạo BST và nhận K Các yếu tố lớn nhất):

Theo cách tiếp cận này, chúng tôi sẽ tạo một cây tìm kiếm nhị phân và sau đó chúng tôi sẽ in k các yếu tố lớn nhất của nó. & NBSP;

using namespace std;

Top 2 Elements: 787 23 
686
Top 2 Elements: 787 23 
687

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
690

Top 2 Elements: 787 23 
05

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
686
Top 2 Elements: 787 23 
693

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23using33

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
704

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
686
Top 2 Elements: 787 23 
696

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
Top 2 Elements: 787 23 
699

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
715

Top 2 Elements: 787 23 
5void
Top 2 Elements: 787 23 
707int
Top 2 Elements: 787 23 
709

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
720

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
727

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
711
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
Top 2 Elements: 787 23 
713

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40using70
Top 2 Elements: 787 23 
718

#include <bits/stdc++.h>66std;50

Top 2 Elements: 787 23 
730

Top 2 Elements: 787 23 
737
Top 2 Elements: 787 23 
738

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731using70
Top 2 Elements: 787 23 
733

Top 2 Elements: 787 23 
737
Top 2 Elements: 787 23 
745

Top 2 Elements: 787 23 
737std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
734using70
Top 2 Elements: 787 23 
736

Top 2 Elements: 787 23 
737
Top 2 Elements: 787 23 
760

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
734namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
737
Top 2 Elements: 787 23 
767

Top 2 Elements: 787 23 
737std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
734namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
734using70
Top 2 Elements: 787 23 
758

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
794

Top 2 Elements: 787 23 
5void
Top 2 Elements: 787 23 
783int
Top 2 Elements: 787 23 
785int
Top 2 Elements: 787 23 
787

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
801

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
803

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
805

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
05

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40using70
Top 2 Elements: 787 23 
790std;1
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23using33

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40using70
Top 2 Elements: 787 23 
797std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
359int
Top 2 Elements: 787 23 
826

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
828

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
Top 2 Elements: 787 23 
810

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
359int
Top 2 Elements: 787 23 
816__

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
839

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
2int
Top 2 Elements: 787 23 
833

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
05

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
835

Top 2 Elements: 787 23 
5std;1
Top 2 Elements: 787 23 
842

int

Top 2 Elements: 787 23 
847

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
855

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396int
Top 2 Elements: 787 23 
850

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396int
Top 2 Elements: 787 23 
853

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
857
Top 2 Elements: 787 23 
858

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
396
Top 2 Elements: 787 23 
871
Top 2 Elements: 787 23 
7
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
860
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
307
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6std;1 namespace84

Top 2 Elements: 787 23 
9

Java

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace87

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
14 namespace89

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
Top 2 Elements: 787 23 
889

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6int
Top 2 Elements: 787 23 
690

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
894

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
896

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
Top 2 Elements: 787 23 
899

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
901
Top 2 Elements: 787 23 
902
Top 2 Elements: 787 23 
8

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void
Top 2 Elements: 787 23 
707int
Top 2 Elements: 787 23 
908

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
912
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58
Top 2 Elements: 787 23 
713

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
916

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
919
Top 2 Elements: 787 23 
902292921

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
720

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
930

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40std;50
Top 2 Elements: 787 23 
933
Top 2 Elements: 787 23 
902292921

#include <bits/stdc++.h>66using70

Top 2 Elements: 787 23 
938

Top 2 Elements: 787 23 
731using70
Top 2 Elements: 787 23 
941
Top 2 Elements: 787 23 
902
Top 2 Elements: 787 23 
921

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
945

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
952

Top 2 Elements: 787 23 
734std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>66namespace40

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
731using70
Top 2 Elements: 787 23 
965
Top 2 Elements: 787 23 
902
Top 2 Elements: 787 23 
921

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
969

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
731namespace40
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
734
Top 2 Elements: 787 23 
976

Top 2 Elements: 787 23 
734std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
731
Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>66

Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

#include <bits/stdc++.h>66namespace40

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

Top 2 Elements: 787 23 
731using70
Top 2 Elements: 787 23 
965
Top 2 Elements: 787 23 
902
Top 2 Elements: 787 23 
921

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>010

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6void
Top 2 Elements: 787 23 
783int
Top 2 Elements: 787 23 
994

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40std;1
Top 2 Elements: 787 23 
8

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5using70
Top 2 Elements: 787 23 
999
Top 2 Elements: 787 23 
902 #include <bits/stdc++.h>001
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
921

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
803

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>026

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5using70 #include <bits/stdc++.h>013
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38
Top 2 Elements: 787 23 
921

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>022

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20 #include <bits/stdc++.h>031

Các

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>043
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 #include <bits/stdc++.h>045

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40
Top 2 Elements: 787 23 
835

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
839

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>047
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 #include <bits/stdc++.h>049

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22____9
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38#include <bits/stdc++.h>056

Top 2 Elements: 787 23 
5std;1
Top 2 Elements: 787 23 
842

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
5

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
20
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
21

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
23
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
24 void
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
84

Top 2 Elements: 787 23 
5int #include <bits/stdc++.h>8std;79#include <bits/stdc++.h>082
Top 2 Elements: 787 23 
022

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>103

Top 2 Elements: 787 23 
5int#include <bits/stdc++.h>087
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
95
Top 2 Elements: 787 23 
92

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>099
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
58 #include <bits/stdc++.h>101

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
40#include <bits/stdc++.h>120
Top 2 Elements: 787 23 
7
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
541

3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
6
Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
9

Top 2 Elements: 787 23 
5#include <bits/stdc++.h>105
Top 2 Elements: 787 23 
861 #include <bits/stdc++.h>107#include <bits/stdc++.h>108
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
43

Top 2 Elements: 787 23 

Top 2 Elements: 787 23 
5
Top 2 Elements: 787 23 
1
Top 2 Elements: 787 23 
22int
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
37
3 largest elements are : 50 88 96 
3 smallest elements are : 3 2 1 
38#include <bits/stdc++.h>116
Top 2 Elements: 787 23 
022
O(n*log(n)) + O(k) ~= O(n*log(n)) (here making of Binary Search Tree from array take n*log(n) time + O(n) time for finding top k element)
Auxiliary Space: O(n) (to store the tree with n node we need O(n) space + O(k) space for storing the top k element to print)

Đầu ra
References: 
http://en.wikipedia.org/wiki/Selection_algorithm


Yếu tố lớn nhất Kth trong một mảng là gì?

Nếu chúng ta sắp xếp mảng theo thứ tự tăng dần, phần tử KTH của một mảng sẽ là phần tử nhỏ nhất thứ k.Để tìm phần tử lớn nhất thứ k, chúng ta có thể vượt qua k = độ dài (mảng) - k.k= length(Array) – k.

Chúng ta có thể tìm thấy yếu tố lớn nhất kth bằng Min không

Để tìm ra yếu tố lớn nhất kth, chúng ta luôn cần xây dựng một min-heap và phần tử hàng đầu trong đống sẽ là yếu tố lớn nhất thứ k của chúng ta.Để tìm ra yếu tố nhỏ nhất kth, chúng ta luôn cần xây dựng một phần lớn tối đa và phần tử hàng đầu trong tối đa sẽ là yếu tố nhỏ nhất của chúng ta.. To find Kth smallest element, we always need to construct a max-heap and the top element in the max-heap would be our Kth smallest element.

Yếu tố lớn nhất Kth có nghĩa là gì?

Nếu bạn sắp xếp mảng từ nhỏ nhất đến lớn nhất, phần tử nhỏ nhất KTH là phần tử KTH trong mảng được sắp xếp.Yếu tố lớn nhất kth là KTH từ cuối trong mảng được sắp xếp.the kth from the end in the sorted array.