Skip to content

Commit

Permalink
Kth Largest Element using Java
Browse files Browse the repository at this point in the history
This solves the issue #71 

Signed-off-by: Yasani Samara Simha Reddy <[email protected]>
  • Loading branch information
samara6855 authored Oct 22, 2023
1 parent 6da9b42 commit b7c2cbb
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions JAVA/KthLargest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Arrays;
import java.util.Scanner;

public class KthLargest {

public static int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();

int[] nums = new int[n];

System.out.println("Enter " + n + " array elements:");
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}

System.out.print("Enter the value of k to find the k'th largest element: ");
int k = scanner.nextInt();

System.out.println("The " + k + "th largest element is: " + findKthLargest(nums, k));

scanner.close();
}
}

0 comments on commit b7c2cbb

Please sign in to comment.