-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKthMinMax.java
37 lines (32 loc) · 890 Bytes
/
KthMinMax.java
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
package arrays;
import java.util.Scanner;
public class KthMinMax {
public static void main(String[] args) {
System.out.println("enter the kth number and size of array and the array");
int size,minElement,maxElement;
Scanner sc = new Scanner(System.in);
int k =sc.nextInt();
size = sc.nextInt();
int array[] = new int[size];
for(int i = 0; i<size; i++ )
array[i] = sc.nextInt();
if(k>size || k<0)
System.out.println("please re-enter the kth element");
else {
minElement = k-1;
maxElement = size-k;
for(int i = 0; i<size-1; i++) {
int minIndex = i;
for(int j = i; j<size; j++) {
if(array[j]<array[minIndex]) {
minIndex = j;
}
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
System.out.println("maximum is "+array[maxElement]+" and minimun is "+array[minElement]);}
sc.close();
}
}