public class Ex12PriceIsRight { public static int priceIsRight(int[] bids, int price) { int closest = Integer.MIN_VALUE; for (int bid : bids) { if (bid > closest && bid <= price) { closest = bid; } } if (closest == Integer.MIN_VALUE) { // if all bids are higher than the price... return -1; } return closest; } public static void main(String[] args) { int[] bids1 = { 200, 300, 250, 1, 950, 40 }; int[] bids2 = { 290, 300, 295, 1000, 950, 405 }; System.out.println( priceIsRight(bids1, 280) ); System.out.println( priceIsRight(bids2, 280) ); } }