Try it first — give it 20 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
This is a Codeforces problem, so you read input and print output yourself (no function to fill in).
Vasiliy wants to buy a drink. There are n shops, and shop i sells it for x[i] coins. He plans to buy for q days, and on day j he has m[j] coins. For each day, print how many shops sell the drink at a price he can afford, meaning x[i] <= m[j].
Input: n, then the n prices, then q, then q budgets, one per line. Limits: n and q up to 10⁵, prices up to 10⁵, budgets up to 10⁹.
Example: prices 3 10 8 6 11 and budgets 1, 10, 3, 11. With 1 coin he can afford nothing: 0. With 10 coins he can afford 3, 10, 8 and 6: 4. With 3 coins only the shop at 3: 1. With 11 coins, all five: 5.
Start with brute force
For each day, loop over all shops and count prices <= m. It's clearly correct.
It costs n steps per day, so n · q in total. With n = q = 10⁵ that's 10¹⁰ comparisons, far beyond the roughly 10⁸ simple steps per second you can count on. It times out.
Building the intuition
The brute force redoes the same work every day: it looks at all the prices again, in a random order. What could you prepare once, so each day is quick?
Sort the prices: 3 6 8 10 11. Now look at a budget of 10:
| i | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| sorted price | 3 | 6 | 8 | 10 | 11 |
| affordable with 10? | ✓ | ✓ | ✓ | ✓ | ✗ |
The affordable shops are always a prefix of the sorted list. Once one price is too expensive, every price after it is too. So you don't need to count them one by one. You only need to find where the prefix ends.
Turn that into the yes/no question from the primer: "is price[i] > m?" It's F F F F T, falses then trues. The first true index is where the prefix ends, and since indices start at 0, that index is the number of affordable shops. In the primer's words, it's the upper bound of m.
The optimal approach
- Read the
nprices and sort them. - For each budget
m:- Set
lo = 0andhi = n. Indexnis past the end; landing there means "every shop is affordable". - While
lo < hi:mid = lo + (hi - lo) // 2. Ifprice[mid] > m, sethi = mid. Otherwise setlo = mid + 1. - Print
lo.
- Set
- Collect the answers and print them all at the end. Printing 10⁵ lines one by one is slow in Python and Java.
The subtle detail: > and not >=. A shop whose price equals the budget is affordable. With >= you'd find the first price >= m, which stops before the shops priced exactly m. On the example, budget 10 would give 3 instead of 4. That is the lower bound versus upper bound difference, and it's one character.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0])
price = sorted(int(x) for x in data[1:1 + n])
q = int(data[1 + n])
out = []
for j in range(q):
m = int(data[2 + n + j])
# First index with price > m (the upper bound). Every shop before it is
# affordable, so that index IS the count. It always stays in [lo, hi].
lo, hi = 0, n
while lo < hi:
mid = lo + (hi - lo) // 2
if price[mid] > m: # ">" not ">=": a price equal to m is affordable
hi = mid
else:
lo = mid + 1
out.append(str(lo))
print("\n".join(out))
main()#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> price(n);
for (int& x : price) cin >> x;
sort(price.begin(), price.end());
int q;
cin >> q;
string out;
while (q--) {
int m; // up to 1e9, still fits in int
cin >> m;
// First index with price > m (the upper bound). Every shop before it is
// affordable, so that index IS the count. It always stays in [lo, hi].
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (price[mid] > m) { // ">" not ">=": a price equal to m is affordable
hi = mid;
} else {
lo = mid + 1;
}
}
out += to_string(lo);
out += '\n';
}
cout << out;
return 0;
}import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in, 1 << 16));
StringBuilder out = new StringBuilder();
int n = nextInt(in);
int[] price = new int[n];
for (int i = 0; i < n; i++) price[i] = nextInt(in);
// Arrays.sort(int[]) is a quicksort that crafted tests can push to O(n^2);
// a random shuffle first makes that practically impossible.
Random rnd = new Random();
for (int i = n - 1; i > 0; i--) {
int j = rnd.nextInt(i + 1);
int tmp = price[i]; price[i] = price[j]; price[j] = tmp;
}
Arrays.sort(price);
int q = nextInt(in);
while (q-- > 0) {
int m = nextInt(in); // up to 1e9, still fits in int
// First index with price > m (the upper bound). Every shop before it is
// affordable, so that index IS the count. It always stays in [lo, hi].
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (price[mid] > m) { // ">" not ">=": a price equal to m is affordable
hi = mid;
} else {
lo = mid + 1;
}
}
out.append(lo).append('\n');
}
System.out.print(out);
}
// Fast integer reader: Scanner is too slow for 2*10^5 numbers
private static int nextInt(DataInputStream in) throws IOException {
int ret = 0, b = in.read();
while (b != '-' && (b < '0' || b > '9')) b = in.read();
boolean neg = b == '-';
if (neg) b = in.read();
while (b >= '0' && b <= '9') {
ret = ret * 10 + (b - '0');
b = in.read();
}
return neg ? -ret : ret;
}
}// Codeforces: choose "Node.js" as the language
const data = require("fs").readFileSync(0, "utf8").split(/\s+/).filter(Boolean).map(Number);
let pos = 0;
const n = data[pos++];
// A typed array sorts numerically (a plain array's default sort compares strings)
const price = Int32Array.from(data.slice(pos, pos + n)).sort();
pos += n;
const q = data[pos++];
const out = [];
for (let j = 0; j < q; j++) {
const m = data[pos++];
// First index with price > m (the upper bound). Every shop before it is
// affordable, so that index IS the count. It always stays in [lo, hi].
let lo = 0, hi = n;
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
if (price[mid] > m) { // ">" not ">=": a price equal to m is affordable
hi = mid;
} else {
lo = mid + 1;
}
}
out.push(lo);
}
console.log(out.join("\n"));Dry run
Sorted prices [3, 6, 8, 10, 11], so n = 5. Budget m = 10, question "is price[mid] > 10?":
| lo | mid | hi | price[mid] | > 10? | update |
|---|---|---|---|---|---|
| 0 | 2 | 5 | 8 | No | lo = 3 |
| 3 | 4 | 5 | 11 | Yes | hi = 4 |
| 3 | 3 | 4 | 10 | No | lo = 4 |
| 4 | 4 | stop |
Print 4. Notice row 3: the price equals the budget, the answer is "No", and lo moves past it, so that shop is counted.
Budget m = 1:
| lo | mid | hi | price[mid] | > 1? | update |
|---|---|---|---|---|---|
| 0 | 2 | 5 | 8 | Yes | hi = 2 |
| 0 | 1 | 2 | 6 | Yes | hi = 1 |
| 0 | 0 | 1 | 3 | Yes | hi = 0 |
| 0 | 0 | stop |
Print 0.
Complexity
- Time: O((n + q) log n). Sorting costs O(n log n). Each of the
qsearches costs O(log n), about 17 steps for 10⁵ shops. In total that's a few million steps instead of 10¹⁰. - Space: O(n) for the prices (plus the output buffer).
Edge cases and traps
>=instead of>. Undercounts every shop priced exactly at the budget. Test a budget equal to a price.- Budget bigger than every price. The search ends at
lo = n. That's the correct answer, all shops, and there's no array read atnbecausemidis always less thanhi. - Duplicate prices. Fine: the upper bound skips past every copy of a price
<= m. - Slow I/O. Up to 2 × 10⁵ numbers in and 10⁵ lines out. Read everything at once, and build the output in one string.
- Java's
Arrays.sort(int[]). It's a quicksort, and some Codeforces tests are built to make it run in O(n²). Shuffle the array first, as the Java solution does.
Say it in the interview
"Scanning every shop for every day is n times q, up to ten to the ten. Instead I sort the prices once. Then the shops I can afford on a given day are a prefix of the sorted list, so I just need where that prefix ends. That's the first index whose price is strictly greater than the budget, the upper bound, and it equals the count. Each query is a binary search, so the total is O of n plus q, times log n."
Follow-ups interviewers ask
- "Prices are at most 10⁵. Can you avoid binary search?" Yes. Count how many shops have each price, then build prefix sums so
atMost[v]is the number of shops priced<= v. Each day isatMost[min(m, 10⁵)], O(1). It only works because prices are small. - "What if shops can be added between days?" A sorted array costs O(n) per insert. Use a balanced tree with order statistics, or a Fenwick tree over prices.
- "What if you needed shops with price strictly less than m?" Use the lower bound (
price[mid] >= m) instead.
Practise the same idea
This is Day 38 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free