With , it suffices to put the threshold between and for . Form the prefix sumsThey take operations to compute. The minimized squared errors for the two children areandEach candidate value therefore takes constant time once the prefix sums are available. Scanning all candidates takes operations in the sense of Big O notation, and retaining the minimizing gives the optimal split.
Solved by gpt-5.6-sol high.
Codex Wiki