27. Given an array and a number k where k is smaller than the size of the array,
* we need to find the k’th largest element in the given array.
* It is given that all array elements are distinct.
* This program should be done without sorting the array.
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 3
Output: 10
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 5
Output: 4
Ans:
import java.util.Scanner;
public class Largest
{
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[6],i,j,k,c=0,l=0;
System.out.println("ENTER 6 NUMBERS"); // input
for(i=0;i<6;++i)
a[i] = sc.nextInt();
System.out.println("ENTER AN INTEGER");
k= sc.nextInt();
for(j=0;j<(2*k-1);++j) { // finding the kth largest
for(i=0;i<6;++i) {
if(a[i]!=l) {
++c;
if(c==1||a[i]>l)
l=a[i];
}
else {
l=a[i]=0;
break;
}
}
}//loop ends
System.out.println(k+"th LARGEST NUMBER = "+l);
}
}
PROGRAM BY ANUBRATA DAS