Answered step by step
Verified Expert Solution
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
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 sc = new Scanner(System.in);
- System.out.print("Enter size : ");
- int n = sc.nextInt();
- int a[] = new int[n];
- for(int i=0;i
- {
- System.out.print("Enter value for array : ");
- a[i] = sc.nextInt();
- }
- System.out.print("Enter k : ");
- int k = sc.nextInt()-1;
- int l=0,u=n-1,i,j,x;
- while(l
- {
- i=l;
- j=u;
- x=a[k];
- while(i=k)
- {
- while (a[i]
- i++;
- while (a[j]>x)
- j--;
- int t = a[i];
- a[i] = a[j];
- a[j] = t;
- i++;
- j--;
- }
- if (j
- l=i;
- if (i>k)
- u=j;
- }
- System.out.println("kth Smallest element : "+a[k]);
- sc.close();
- }
- }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started