From b7c2cbb8e0e5a2795bd50054492ce3854fe6bbde Mon Sep 17 00:00:00 2001 From: Yasani Samara Simha Reddy <101248119+samara6855@users.noreply.github.com> Date: Sun, 22 Oct 2023 08:54:47 +0530 Subject: [PATCH] Kth Largest Element using Java This solves the issue #71 Signed-off-by: Yasani Samara Simha Reddy <101248119+samara6855@users.noreply.github.com> --- JAVA/KthLargest.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 JAVA/KthLargest.java diff --git a/JAVA/KthLargest.java b/JAVA/KthLargest.java new file mode 100644 index 0000000..9a186c0 --- /dev/null +++ b/JAVA/KthLargest.java @@ -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(); + } +}