-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathkthMaxMin.java
More file actions
32 lines (32 loc) · 1008 Bytes
/
kthMaxMin.java
File metadata and controls
32 lines (32 loc) · 1008 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
import java.util.Scanner;
public class kthMaxMin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Size of Array: ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// for(int i = 0; i < n; i++){
// System.out.print(arr[i] + " ");
// }
for(int j = 0; j < n-1; j++) {
for (int i = 0; i < n - 1; i++) {
int temp;
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.print("Position of Kth Smallest Element: ");
int k = sc.nextInt();
System.out.println(arr[k-1]);
}
}