Question

In: Computer Science

Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...

Find the K'th smallest element in an unsorted array of integers.

Find the K'th largest element in an unsorted array of integers.

please make two separate methods in java

Solutions

Expert Solution

ANSWER:-

import java.util.*;
import java.util.Arrays;
import java.util.Collections;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static int kthSmallest(int [] arr,int k)
{
// Sort the given array
Arrays.sort(arr);
  
// Return k'th element in
// the sorted array
return arr[k-1];
}
public static int kthlargest(int [] arr,int k)
{
// Sort the given array
Arrays.sort(arr);
  
// Return k'th element in
// the sorted array
return arr[arr.length-k];
}
   public static void main (String[] args)
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the size of the array : ");
       int n=sc.nextInt();
       int []arr=new int[n];
       System.out.println("enter the array elements : ");
       for(int i=0;i<n;i++)
       arr[i]=sc.nextInt();
       System.out.println("enter the k value : ");
       int k=sc.nextInt();
       System.out.println("kth smallest is : "+kthSmallest(arr, k));
       System.out.println("kth largest is : "+kthlargest(arr, k));
   }
}

OUTPUT

// If any doubt please comment


Related Solutions

Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT