Question
JAVA help needed. Please help with implementation of the bolded method: public static Integer[] findKthLargest(List list, int k) throws IllegalArgumentException I cannot seem to figure
JAVA help needed. Please help with implementation of the bolded method:
public static Integer[] findKthLargest(Listlist, int k) throws IllegalArgumentException
I cannot seem to figure out how to make List
Thanks!
--------------------------------------------------------------------- import java.math.BigInteger; import java.util.*; /** * This class represents a program that that determines the * largest number possible from the contents of a given integer array. * @author */ public class LargestNumberSolver { /** * This generic method sorts the input array using an insertion sort and the input Comparator object. * * @param- Generic type * @param arr - this array. * @param cmp - what is being compared. */ public static void insertionSort(T[] arr, Comparator super T> cmp) { if (arr == null || cmp == null) { throw new IllegalArgumentException("Array and comparator cannot be null"); } for (var i = 0; i < arr.length; i++) { T val = arr[i]; int j; for (j = i - 1; j >= 0 && cmp.compare(arr[j], val) > 0; j--) { arr[j + 1] = arr[j]; } arr[j + 1] = val; } } /** * This method returns the largest number that can be formed by arranging the * integers of the given array, in any order. If the array is empty, the largest * number that can be formed is 0. * * This method must not alter the given array and must call your insertionSort method * with a Comparator or lambda expression that you design. * * @param arr - this array. * @return largeNumber - new BigInteger toString. */ public static BigInteger findLargestNumber(Integer[] arr) { Integer[] tmp = arr.clone(); StringBuilder bigNumber = new StringBuilder(); if (tmp != null) { insertionSort(tmp, new largestNumber()); for (Integer integer : tmp) bigNumber.append(integer); } else { bigNumber.append(0); } return new BigInteger(bigNumber.toString()); } static class largestNumber implements Comparator
{ public int compare(Integer o1, Integer o2) { String xy = o1.toString() + o2.toString(); String yx = o2.toString() + o1; return yx.compareTo(xy); } } /** * This method determines the kth largest number that can be formed by each array in the given list. * * E.g., if k=0 returns the largest overall, if k=list.size()-1 returns the smallest overall. *
* This method returns the original array that represents the kth largest number, not the kth largest number itself. *
* This method must not alter the given list and must call your insertionSort method with a Comparator * or lambda expression that you design. * * @param list - list of each array. * @param k - representing all the largest numbers from each array in the list * sorted in descending order. * @return Integer [] - returns an Integer array of the largest numbers in the list. * @throws IllegalArgumentException - thrown if k is not a valid position in the list. */
public static Integer[] findKthLargest(Listlist, int k) throws IllegalArgumentException { // TODO } }
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