-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterestingdrink.java
More file actions
38 lines (30 loc) · 986 Bytes
/
interestingdrink.java
File metadata and controls
38 lines (30 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class interestingdrink {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nshops = sc.nextInt();
int[] prices = new int[nshops];
for (int i = 0; i < nshops; i++) {
prices[i] = sc.nextInt();
}
int days = sc.nextInt();
Arrays.sort(prices);
for (int i = 0; i < days; i++) {
int budget = sc.nextInt();
int count = binarysearch(prices, budget);
System.out.println(count);
}
}
private static int binarysearch(int[] prices, int budget) {
int left = 0, right = prices.length;
while (left < right) {
int mid = left + (right - left) / 2;
if (prices[mid] <= budget) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}