Java Program to Find kth Largest Element in a Sequence

«
»
This is a java program to find kth largest element form the given sequence of numbers. We find the kth largest by sorting the sequence first and then returning the element at position N-k, which qualifies as the kth largest element of the sequence.

Here is the source code of the Java Program to Find kth Largest Element in a Sequence. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find kth largest element in randomly generated sequence
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Kth_Largest 
  6. {
  7.     static int N = 20;
  8.     static int []sequence = new int[N];
  9.     public static void sort()
  10.     {
  11.         System.out.println("The Sequence is: ");
  12.         for(int i=0; i<N; i++)
  13.             System.out.print(sequence[i] + " ");
  14.         System.out.println();
  15.  
  16.         int i, j, temp;
  17.         for (i = 1; i< N; i++) 
  18.         {
  19.             j = i;
  20.             temp = sequence[i];    
  21.             while (j > 0 && temp < sequence[j-1])
  22.             {
  23.                 sequence[j] = sequence[j-1];
  24.                 j = j-1;
  25.             }
  26.             sequence[j] = temp;            
  27.         }        
  28.     }
  29.  
  30.     public static void main(String args[])
  31.     {
  32.         Random random = new Random();
  33.  
  34.         for(int i=0; i<N; i++)
  35.             sequence[i] = Math.abs(random.nextInt(100));
  36.  
  37.         Scanner sc = new Scanner(System.in);
  38.         System.out.println("Enter the kth largest to find");
  39.         int k = sc.nextInt();
  40.  
  41.         sort();
  42.         System.out.println(k+"th largest element is " + sequence[N-k-1]);
  43.         sc.close();
  44.     }
  45. }

Output:

advertisement
$ javac Kth_Largest.java
$ java Kth_Largest
 
Enter the kth largest to find
5
The Sequence is: 
77 20 91 48 29 55 2 53 29 7 20 91 78 21 87 81 49 53 77 1 
5th largest element is 77

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter