Skip to content

Commit

Permalink
Merge pull request #76 from samara6855/patch-3
Browse files Browse the repository at this point in the history
Kth Largest Element using Java
  • Loading branch information
mrsamirr authored Oct 22, 2023
2 parents 5530e84 + b7c2cbb commit aecad6f
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 aecad6f

Please sign in to comment.