Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedI 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

5. Who should facilitate the focus group?

Answered: 1 week ago