Cut a cake so both sides have equal area
A cake is made of N rectangular blocks arranged left-to-right in a straight line (no gaps, no overlap). Block i has:
All blocks share the same baseline; the cake’s top edge forms a step-like profile.
You want to make one vertical cut at position x (measured from the cake’s left edge) so that the area to the left of the cut equals the area to the right.
Input
-
An integer
N
.
-
Arrays
w[0..N-1]
,
h[0..N-1]
.
Output
-
Return the cut position
x
as a real number such that:
-
0 <= x <= sum(w_i)
-
area(left of
x
) = total_area / 2
Notes
-
If the cut falls inside a block, that block is split proportionally by width.
-
Assume
total_area
is even in the sense that an exact cut position exists.
Complexity discussion
Explain your approach and its time/space complexity. (A common approach is to search which block contains the half-area point, then locate the exact offset inside that block.)