Question: I want to turn this code from finding the KthSmallest to KthLargest import java.util.Scanner; public class KthSmallestElement { public static void main(String[] args) { Scanner

I want to turn this code from finding the KthSmallest to KthLargestimport java.util.Scanner; public class KthSmallestElement { public static void main(String[] args) {I want to turn this code from finding the KthSmallest to KthLargest

 
  1. import java.util.Scanner;
  2. public class KthSmallestElement {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. System.out.print("Enter size : ");
  6. int n = sc.nextInt();
  7. int a[] = new int[n];
  8. for(int i=0;i
  9. {
  10. System.out.print("Enter value for array : ");
  11. a[i] = sc.nextInt();
  12. }
  13. System.out.print("Enter k : ");
  14. int k = sc.nextInt()-1;
  15. int l=0,u=n-1,i,j,x;
  16. while(l
  17. {
  18. i=l;
  19. j=u;
  20. x=a[k];
  21. while(i=k)
  22. {
  23. while (a[i]
  24. i++;
  25. while (a[j]>x)
  26. j--;
  27. int t = a[i];
  28. a[i] = a[j];
  29. a[j] = t;
  30. i++;
  31. j--;
  32. }
  33. if (j
  34. l=i;
  35. if (i>k)
  36. u=j;
  37. }
  38. System.out.println("kth Smallest element : "+a[k]);
  39. sc.close();
  40. }
  41. }

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter size : "); int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i=k) while (a[i]x) --; int t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; if (jk) u=j; ystem.out.println("kth Smallest element : "+a[k])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!