Set low to the minimum bloom day (1) and high to the maximum bloom day (10). This defines the initial search space for the minimum feasible day.
💡 Starting with the full range of possible days ensures we don't miss any candidate day.
Line:low, high = min(bloomDay), max(bloomDay)
💡 The search space covers all days from earliest to latest bloom.
compare
Calculate Midpoint Day
Calculate mid as (1 + 10) // 2 = 5. This is the candidate day to test if we can make 3 bouquets by day 5.
💡 Midpoint splits the search space to test feasibility efficiently.
Line:mid = (low + high) // 2
💡 Midpoint represents the current guess for the minimum feasible day.
compare
Check Feasibility for Day 5
Check if we can make 3 bouquets by day 5. Flowers bloomed by day 5 are at indices 0 (day 1), 2 (day 3), and 4 (day 2). Each flower can form a bouquet since k=1.
💡 Feasibility check determines if mid day is sufficient to meet bouquet requirements.
Line:if canMake(mid):
💡 Day 5 is feasible to make required bouquets.
shrink
Adjust High Boundary to Mid - 1
Since day 5 is feasible, update high to 4 to search for a smaller feasible day.
💡 Narrowing search space to find minimum feasible day.
Line:high = mid - 1
💡 Eliminating days greater than 5 because we want the minimum day.
compare
Calculate New Midpoint Day
Calculate mid as (1 + 4) // 2 = 2. Test if day 2 is feasible to make 3 bouquets.
💡 Testing earlier days to find minimum feasible day.
Line:mid = (low + high) // 2
💡 Midpoint moves left to test smaller days.
compare
Check Feasibility for Day 2
Check if we can make 3 bouquets by day 2. Flowers bloomed by day 2 are at indices 0 (day 1) and 4 (day 2). Only 2 bouquets can be made, which is less than required 3.
💡 Feasibility fails because not enough flowers have bloomed.
Line:if canMake(mid):
💡 Day 2 is not feasible to make required bouquets.
expand
Adjust Low Boundary to Mid + 1
Since day 2 is not feasible, update low to 3 to search for feasible days greater than 2.
💡 Discarding days that are too early to meet bouquet requirements.
Line:low = mid + 1
💡 Eliminating days less than or equal to 2.
compare
Calculate New Midpoint Day
Calculate mid as (3 + 4) // 2 = 3. Test if day 3 is feasible to make 3 bouquets.
💡 Testing a day closer to feasibility boundary.
Line:mid = (low + high) // 2
💡 Midpoint moves to 3 to test feasibility.
compare
Check Feasibility for Day 3
Check if we can make 3 bouquets by day 3. Flowers bloomed by day 3 are at indices 0 (day 1), 2 (day 3), and 4 (day 2). We can make 3 bouquets, meeting the requirement.
💡 Feasibility passes, so day 3 is a candidate answer.
Line:if canMake(mid):
💡 Day 3 is feasible and possibly the minimum day.
shrink
Adjust High Boundary to Mid - 1
Since day 3 is feasible, update high to 2 to try to find an even smaller feasible day.
💡 Narrowing search space to find minimum feasible day.
Line:high = mid - 1
💡 Search space shrinks to days less than 3.
reconstruct
Binary Search Ends, Return Answer
Search space is invalid (low > high), so return the last feasible day found, which is 3.
💡 The minimum day to make required bouquets is found.
Line:return answer
💡 Binary search successfully found the minimum feasible day.
def minDays(bloomDay, m, k):
n = len(bloomDay)
if m * k > n:
return -1
low, high = min(bloomDay), max(bloomDay) # STEP 1
def canMake(day):
bouquets = flowers = 0
for bloom in bloomDay:
if bloom <= day:
flowers += 1
if flowers == k:
bouquets += 1
flowers = 0
if bouquets >= m:
return True
else:
flowers = 0
return bouquets >= m
answer = -1
while low <= high:
mid = (low + high) // 2 # STEP 2,5,8
if canMake(mid): # STEP 3,6,9
answer = mid
high = mid - 1 # STEP 4,10
else:
low = mid + 1 # STEP 7
return answer # STEP 11
if __name__ == '__main__':
print(minDays([1,10,3,10,2], 3, 1)) # Output: 3
📊
Minimum Days to Make m Bouquets - Watch the Algorithm Execute, Step by Step
Watching each step reveals how the algorithm efficiently eliminates impossible days, focusing only on promising candidates.
Step 1/11
·Active fill★Answer cell
1
0
lo
10
1
3
2
10
3
2
4
Next: lo=1 hi=10
Target: 5
1
0
lo
10
1
3
2
10
3
2
4
Next: lo=1 hi=10
Target: 5
1
0
lo
10
1
3
2
10
3
2
4
Next: lo=1 hi=4
Target: 5
1
0
lo
10
1
3
2
10
3
hi
2
4
Next: lo=1 hi=4
Target: 2
1
0
lo
10
1
mid
3
2
10
3
hi
2
4
mid[2]=3
Next: lo=1 hi=4
Target: 2
1
0
lo
10
1
mid
3
2
10
3
hi
2
4
mid[2]=flowers bloomed by day 2: indices 0,4 - not feasible to make 3 bouquets
Next: lo=3 hi=4
Target: 2
1
0
10
1
mid
3
2
lo
10
3
hi
2
4
mid[2]=flowers bloomed by day 2: indices 0,4 - not feasible to make 3 bouquets
Next: lo=3 hi=4
Target: 3
1
0
10
1
3
2
lo/mid
10
3
hi
2
4
mid[3]=10
Next: lo=3 hi=4
Target: 3
1
0
10
1
3
2
lo/mid
10
3
hi
2
4
mid[3]=flowers bloomed by day 3: indices 0,2,4 - feasible to make 3 bouquets
Next: lo=3 hi=2
Target: 3
1
0
10
1
hi
3
2
lo/mid
10
3
2
4
mid[3]=flowers bloomed by day 3: indices 0,2,4 - feasible to make 3 bouquets
Next: lo=3 hi=2
1
0
10
1
hi
3
2
lo
10
3
2
4
Next: lo=3 hi=2
Key Takeaways
✓ Binary search on the answer space efficiently finds the minimum feasible day without checking every day.
This insight is hard to see from code alone because the search space is conceptual, not directly on array indices.
✓ Feasibility checks can short-circuit early once enough bouquets are formed, improving performance.
Understanding early stopping requires watching the bouquet counting process step-by-step.
✓ Adjusting low and high boundaries depends entirely on feasibility results, showing how the search space shrinks.
1. You are given a sorted array and need to find how many times a specific target value appears. Which algorithmic approach guarantees the most efficient solution in terms of time complexity?
easy
A. Binary search to find the left and right boundaries of the target
B. Linear scan through the array counting occurrences
C. Dynamic programming to count occurrences using subproblems
D. Greedy approach selecting elements until target count is found
Solution
Step 1: Understand problem constraints
The array is sorted, so binary search can be used to find boundaries efficiently.
Step 2: Identify optimal approach
Binary search for left and right boundaries finds the first and last occurrence indices in O(log n) time, enabling count calculation by subtraction.
Applying greedy which doesn't guarantee correctness
2. Consider the following Python code implementing the optimal approach to find k closest elements to x in a sorted array. What is the returned output when calling findClosestElements([1, 3, 5], 2, 4)?
easy
A. [1, 3]
B. [3, 4]
C. [1, 5]
D. [3, 5]
Solution
Step 1: Initialize variables
Array length is 3, k=2, so low=0, high=1.
Step 2: First iteration of binary search
Calculate mid = (0+1)//2 = 0. Compare x - arr[mid] = 4 - 1 = 3 and arr[mid + k] - x = arr[2] - 4 = 5 - 4 = 1. Since 3 > 1, set low = mid + 1 = 1.
Step 3: Loop ends as low == high == 1
Return arr[1:3] = [3, 5].
Final Answer:
Option D -> Option D
Quick Check:
Binary search correctly chooses window starting at index 1 [OK]
Hint: Check mid and window edges distances carefully [OK]
Common Mistakes:
Off-by-one error in slicing window
Incorrect comparison direction in if condition
Returning wrong slice boundaries
3. You need to find the largest integer r such that r * r ≤ x for a given non-negative integer x. Which algorithmic approach guarantees an efficient and correct solution for this problem?
easy
A. Sorting all numbers and selecting the closest square root
B. Greedy approach incrementing from 0 until r * r > x
C. Dynamic programming to build up solutions for all numbers up to x
D. Modified binary search narrowing down the range of possible roots
Solution
Step 1: Understand the problem constraints
The problem requires finding the integer square root efficiently without checking all numbers linearly.
Step 2: Identify the suitable algorithm
Modified binary search efficiently narrows down the search space by comparing mid*mid with x, guaranteeing logarithmic time complexity.
Final Answer:
Option D -> Option D
Quick Check:
Binary search halves the search space each iteration [OK]
Hint: Binary search efficiently narrows down integer roots [OK]
Common Mistakes:
Assuming greedy linear search is efficient
Confusing DP with this problem
Using sorting which is irrelevant
4. The following code attempts to find the k closest elements to x in a sorted array. Identify the line that contains a subtle bug that can cause incorrect results when distances tie.
medium
A. Line 7: mid = (low + high) // 2
B. Line 8: if abs(x - arr[mid]) > abs(arr[mid + k] - x):
C. Line 5: while low < high:
D. Line 10: return arr[low:low + k]
Solution
Step 1: Understand tie-breaker logic
The problem requires choosing the smaller element when distances tie, but abs() removes sign information.
Step 2: Identify bug in distance comparison
Using abs() ignores which side is closer and breaks tie-breaker rules, causing incorrect window selection.
Final Answer:
Option B -> Option B
Quick Check:
Correct code compares x - arr[mid] and arr[mid+k] - x without abs [OK]
Hint: Tie-breakers require signed distance comparison, not absolute values [OK]
Common Mistakes:
Using absolute values loses tie-breaker info
Off-by-one errors in mid calculation
Returning wrong slice boundaries
5. What is the time complexity of the optimized binary search solution for placing m balls in n positions to maximize the minimum distance, where d is the maximum distance between the first and last positions?
medium
A. O(n log d)
B. O(n * d)
C. O(d log n)
D. O(n log n)
Solution
Step 1: Identify outer loop complexity
Binary search on answer space runs in O(log d) iterations, where d is max distance.
Step 2: Identify inner loop complexity
Each feasibility check scans positions once, O(n). Total complexity is O(n log d).
Final Answer:
Option A -> Option A
Quick Check:
Binary search x linear feasibility check = O(n log d) [OK]
Hint: Binary search on distance x linear feasibility check [OK]
Common Mistakes:
Confusing d with n in complexity
Assuming linear search over distances (O(n*d))
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.