forked from seeditsolution/javaprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKth largest number
More file actions
42 lines (37 loc) · 725 Bytes
/
Kth largest number
File metadata and controls
42 lines (37 loc) · 725 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
class Main{
static int kthLargest(int arr[], int size, int k)
{
int temp = 0;
int result =0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i=0;i<size;i++){
if(k==i){
result = arr[size-1-i];
break;
}
}
return result;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int size = sc.nextInt();
int arr[] = new int[size];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
int result= kthLargest(arr, size, k);
System.out.println(result);
}
}