Question: Write a method findClosest that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element

Write a method findClosest that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element in list that is closest to num and is smaller than or equal to num. If all elements are larger than num then the method should return -1.
For example, given list storing values [20,30,25,89,14],
findClosest(list,29) should return 2(index of element 25)
findClosest(list,50) should return 1(index of element 30)
findClosest(list,10) should return -1(no elements meet the conditions)

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!