diff --git a/solving/sum-of-sortable-integers/explain.md b/solving/sum-of-sortable-integers/explain.md new file mode 100644 index 0000000..30fffa2 --- /dev/null +++ b/solving/sum-of-sortable-integers/explain.md @@ -0,0 +1,115 @@ +_Explained by claude-opus-5-5 on 20 October 2026._ + +## Key observation + +Rotations preserve each block's contents, so $k$ works iff every block is a rotation of a sorted sequence and each block's max is $\le$ the next block's min. A block is a rotation of a sorted sequence exactly when, read cyclically, it decreases at most once. + +## Hint 1 + +Your overall shape is right: try every divisor $k$ and check it directly. A number up to $10^5$ has at most 128 divisors. Focus on making the check *exactly* right, especially when values repeat. + +## Hint 2 + +The target is non-decreasing. Re-read every strict `<` in your conditions and ask what happens with equal values, e.g. `[2,1,2]` as a single block, or two blocks where one's max equals the next one's min. + +## Hint 3 + +Instead of branching on `S[0] < S[k-1]`, think of a block as written around a circle. How many times can a rotated sorted array "step down" as you walk once around it, counting the wrap from last back to first? + +## Hint 4 + +A block is a rotation of a sorted array iff it has **at most one** index $j$ with $s_j > s_{(j+1) \bmod k}$. Combine that per-block test with $\max(S_i) \le \min(S_{i+1})$ between neighbours. + +## Approach + +**Your plan is basically correct.** Trying every divisor and checking it directly is the intended solution. What needs fixing is how you handle equal values. + +**Why brute force over divisors is fine.** For $n \le 10^5$, $n$ has at most $128$ divisors. One $O(n)$ check per divisor costs about $1.3\times 10^7$ operations, so no cleverer idea is needed. + +**Splitting the condition (your step 3).** Fix $k$ and cut `nums` into $m = n/k$ blocks $S_0,\dots,S_{m-1}$. Note the last block is $S_{n/k-1}$, not $S_{n/k}$. Rotating a block keeps its multiset, so the final array is sorted exactly when both of these hold: + +1. **Each block can be rotated into sorted order.** +2. **Neighbouring blocks fit together:** $\max(S_i) \le \min(S_{i+1})$ for every $i$. Once blocks are sorted, the last element of $S_i$ is its max and the first of $S_{i+1}$ is its min. + +You wrote `max S_i < min S_(i+1)`. It must be $\le$, because the target is *non-decreasing*. For example, `[1,2,2,3]` with $k=2$ has $\max=2$ and $\min=2$ and is clearly sortable. + +**The per-block test, and where your branching breaks.** Your rule is: if `S[0] < S[k-1]` the block must already be sorted, otherwise it is two non-decreasing runs with the second run *less than* `S[0]`. The idea is right, but equal values slip through. + +- Take `[2,1,2]`. Here `S[0] == S[k-1]`, so you go to the else-branch. The second run `[1,2]` contains `2`, which is not strictly less than `S[0]`, so you reject it. Yet rotating gives `[1,2,2]`, which is sorted. +- You could patch this with $\le$ in the right places. There is a cleaner test with no case split. + +**Count cyclic descents.** Count positions $j$ in the block where $s_j > s_{(j+1) \bmod k}$, including the wrap-around pair (last element, first element). + +- A sorted array, read in a circle, can only step down once: at the wrap from its max back to its min. It steps down zero times if all elements are equal. +- A rotation does not change the circular order, so a rotated sorted block also has at most one cyclic descent. +- Conversely, if there is at most one descent, start reading just after it. You walk a non-decreasing sequence all the way around. + +So a block is sortable iff it has **at most one** cyclic descent. Equal values never count as descents, so duplicates need no special handling. + +**Putting it together.** For each divisor $k$, scan the blocks: + +- Count the cyclic descents of each block and reject $k$ if any block has 2 or more. +- Track each block's min and max, and reject if $\max(S_i) > \min(S_{i+1})$. +- If nothing fails, add $k$ to the answer. + +As sanity checks: $k=1$ reduces to "the array is already sorted", and $k=n$ reduces to "the array is a rotation of a sorted array". + +## Walkthrough + +**Step 1.** Example `nums = [2,1,3,4,6,5]` with $n=6$, so the divisors are $1,2,3,6$. **$k=1$:** every block is a single element, so only the between-block check matters. Block 0 has max $2$ and block 1 has min $1$, and $2 > 1$, so $k=1$ fails. + +```svg +k = 1: six blocks of length 1213465012345max(S0)=2 > min(S1)=1 -> reject k=1 +``` + +**Step 2.** **$k=2$:** the blocks are `[2,1]`, `[3,4]`, `[6,5]`. A block of length 2 always has at most one cyclic descent. Between blocks, $2 \le 3$ and $4 \le 5$ both hold, so $k=2$ is sortable (rotating gives `[1,2,3,4,5,6]`). Running answer: $2$. + +```svg +k = 2: three blocks213465012345max 2 <= min 3max 4 <= min 5accept, sum = 2 +``` + +**Step 3.** **$k=3$:** the blocks are `[2,1,3]` and `[4,6,5]`. Walking around block 0 cyclically, $2>1$ is a descent and the wrap $3>2$ is another. Two descents mean `[2,1,3]` is not a rotation of a sorted array. Block 1 has a single descent ($6>5$), but block 0 already sinks $k=3$. + +```svg +k = 3: two blocks2134650123452>1, 3>2 (wrap)2 descents: fail6>51 descent: ok +``` + +**Step 4.** **$k=6$:** the whole array is one block. Its cyclic descents are $2>1$, $6>5$ and the wrap $5>2$, three in total, so it is not a rotation of a sorted array and $k=6$ fails. The final answer is $2$. + +```svg +k = 6: one block213465012345descents: 2>1, 6>5, 5>2 (wrap) = 3 -> failAnswer = 2 +``` + +## Complexity + +- **Time:** $O(n \cdot d(n))$, where $d(n) \le 128$ for $n \le 10^5$. Each divisor is checked with one linear scan, so the total is about $1.3\times 10^7$ steps. +- **Memory:** $O(1)$ extra, since each block only needs a running min, max and descent count. + +## Pitfalls + +- Using strict `<` between blocks. With duplicates, $\max(S_i) = \min(S_{i+1})$ is allowed. +- Branching on `S[0] < S[k-1]` breaks when `S[0] == S[k-1]` (e.g. `[2,1,2]` or `[1,2,1]`, both sortable). The cyclic-descent count avoids this case split. +- Forgetting the wrap-around comparison (last element vs first). Without it, `[1,3,2]` would show only one descent and be wrongly accepted. +- Off-by-one in block indexing. There are $n/k$ blocks, indexed $0 \dots n/k-1$. +- Do not skip $k=1$ or $k=n$. Both are divisors and must be tested; $k=1$ passes only if `nums` is already sorted. + +## Solution sketch + +```text +answer = 0 +for k in divisors of n: + ok = true + prevMax = -infinity + for start in 0, k, 2k, ..., n-k: + descents = 0; lo = +inf; hi = -inf + for j in 0 .. k-1: + a = nums[start + j] + b = nums[start + (j+1) mod k] + if a > b: descents += 1 + lo = min(lo, a); hi = max(hi, a) + if descents > 1 or prevMax > lo: + ok = false; break + prevMax = hi + if ok: answer += k +return answer +```