Raffle Stub Lookup
You run the raffle at a school fair. The stubs of sold tickets are filed in one list from lowest number to highest. Not every number was sold, so there are gaps.
The head teacher reads out a winning number. Before anyone walks up, you want to know whether it was sold and where its stub sits.
Your task
Write findTicket(tickets, drawn). tickets is sorted from smallest to largest and has no repeats. Return the index of drawn in tickets, or -1 if that number was never sold.
Use the sorted order: your solution should look at about log₂ n stubs, not all of them. An empty list returns -1.
findTicket(tickets: int[], drawn: int) → int
Examples
tickets = [102, 215, 340, 488, 519, 760, 901] drawn = 488
3
The middle stub, index 3, holds 488, so the ticket is found with one look.
tickets = [102, 215, 340, 488, 519, 760, 901] drawn = 500
-1
The middle stub, 488, is less than 500, so look right: 519, 760, 901. Their middle, 760, is too big, so look left of it: just 519. That stub is not 500 either and nothing is left, so the answer is -1 after three looks.
Constraints
- 0 ≤ tickets.length ≤ 100,000
- 1 ≤ tickets[i], drawn ≤ 1,000,000,000
- tickets is strictly increasing (sorted, no repeats).
Start with brute force
Read the stubs from the start until you find the number or run out. This is correct and simple.
But with 100,000 stubs one call can take 100,000 looks, ignoring that the list is sorted. Binary search needs about 17, because 2¹⁷ is more than 100,000.
Hints
0 of 4 shown- Hint 1
In the first example, look only at the middle stub, 488. If the drawn number were 700, which half could you throw away unread?
- Hint 2
Because the list is sorted, one look at the middle rules out half of what is left. Keep the still-possible part as a range from
lotohi. - Hint 3
Each round, look at the middle of the range. If the middle value equals the drawn number, you are done. If it is too small, the answer is to the right, so move
lopast the middle. If it is too big, movehito just before the middle. When does the range become empty? - Hint 4
Start with
lo = 0andhi = n - 1. Loop whilelo <= hi(the=matters: a one-stub range still needs checking). Setmid = floor((lo + hi) / 2)and compare. If the loop ends, return-1.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
In a sorted list, one comparison rules out half the candidates. Keep a range [lo, hi] where the number could still be, and halve it each round.
Trace [102, 215, 340, 488, 519, 760, 901] looking for 500:
lo = 0,hi = 6,mid = 3. 488 < 500, so index 3 and below are too small.lo = 4.lo = 4,hi = 6,mid = 5. 760 > 500, so index 5 and above are too big.hi = 4.lo = 4,hi = 4,mid = 4. 519 > 500.hi = 3.- Now
lo = 4is pasthi = 3. The range is empty, so return-1.
In step 3 the range held a single stub and still had to be checked. That is why the loop runs while lo <= hi, not lo < hi. With <, searching for 901 would never look at the last stub.
The moves are mid + 1 and mid - 1, never just mid: you already checked mid. Keeping it can stop the range shrinking and loop forever.
function findTicket(tickets, drawn) {
let lo = 0, hi = tickets.length - 1;
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2);
if (tickets[mid] === drawn) return mid;
if (tickets[mid] < drawn) lo = mid + 1; // answer is to the right
else hi = mid - 1; // answer is to the left
}
return -1; // the range is empty: never sold
}def find_ticket(tickets, drawn):
lo, hi = 0, len(tickets) - 1
while lo <= hi:
mid = (lo + hi) // 2
if tickets[mid] == drawn:
return mid
if tickets[mid] < drawn:
lo = mid + 1 # answer is to the right
else:
hi = mid - 1 # answer is to the left
return -1 # the range is empty: never soldRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.