Files

116 lines
12 KiB
Markdown

_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
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 640 220'><text x='20' y='30' font-family='sans-serif' font-size='15' fill='currentColor'>k = 1: six blocks of length 1</text><rect x='80' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3' stroke='currentColor'/><rect x='160' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3' stroke='currentColor'/><rect x='240' y='60' width='70' height='50' fill='none' stroke='currentColor'/><rect x='320' y='60' width='70' height='50' fill='none' stroke='currentColor'/><rect x='400' y='60' width='70' height='50' fill='none' stroke='currentColor'/><rect x='480' y='60' width='70' height='50' fill='none' stroke='currentColor'/><g font-family='ui-monospace, monospace' font-size='18' fill='currentColor' text-anchor='middle'><text x='115' y='92'>2</text><text x='195' y='92'>1</text><text x='275' y='92'>3</text><text x='355' y='92'>4</text><text x='435' y='92'>6</text><text x='515' y='92'>5</text></g><g font-family='ui-monospace, monospace' font-size='13' fill='currentColor' text-anchor='middle'><text x='115' y='130'>0</text><text x='195' y='130'>1</text><text x='275' y='130'>2</text><text x='355' y='130'>3</text><text x='435' y='130'>4</text><text x='515' y='130'>5</text></g><text x='80' y='180' font-family='ui-monospace, monospace' font-size='15' fill='currentColor'>max(S0)=2 &gt; min(S1)=1 -&gt; reject k=1</text></svg>
```
**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
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 640 220'><text x='20' y='30' font-family='sans-serif' font-size='15' fill='currentColor'>k = 2: three blocks</text><g fill='#22c55e' fill-opacity='0.3' stroke='currentColor'><rect x='80' y='60' width='70' height='50'/><rect x='160' y='60' width='70' height='50'/><rect x='240' y='60' width='70' height='50'/><rect x='320' y='60' width='70' height='50'/><rect x='400' y='60' width='70' height='50'/><rect x='480' y='60' width='70' height='50'/></g><g fill='none' stroke='currentColor' stroke-width='2.5'><rect x='75' y='55' width='160' height='60' rx='6'/><rect x='235' y='55' width='160' height='60' rx='6'/><rect x='395' y='55' width='160' height='60' rx='6'/></g><g font-family='ui-monospace, monospace' font-size='18' fill='currentColor' text-anchor='middle'><text x='115' y='92'>2</text><text x='195' y='92'>1</text><text x='275' y='92'>3</text><text x='355' y='92'>4</text><text x='435' y='92'>6</text><text x='515' y='92'>5</text></g><g font-family='ui-monospace, monospace' font-size='13' fill='currentColor' text-anchor='middle'><text x='115' y='135'>0</text><text x='195' y='135'>1</text><text x='275' y='135'>2</text><text x='355' y='135'>3</text><text x='435' y='135'>4</text><text x='515' y='135'>5</text></g><g font-family='ui-monospace, monospace' font-size='14' fill='currentColor' text-anchor='middle'><text x='235' y='170'>max 2 &lt;= min 3</text><text x='395' y='195'>max 4 &lt;= min 5</text></g><text x='20' y='210' font-family='sans-serif' font-size='14' fill='currentColor'>accept, sum = 2</text></svg>
```
**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
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 640 220'><text x='20' y='30' font-family='sans-serif' font-size='15' fill='currentColor'>k = 3: two blocks</text><g stroke='currentColor'><rect x='80' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/><rect x='160' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/><rect x='240' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/><rect x='320' y='60' width='70' height='50' fill='#f59e0b' fill-opacity='0.3'/><rect x='400' y='60' width='70' height='50' fill='#f59e0b' fill-opacity='0.3'/><rect x='480' y='60' width='70' height='50' fill='#f59e0b' fill-opacity='0.3'/></g><g fill='none' stroke='currentColor' stroke-width='2.5'><rect x='75' y='55' width='240' height='60' rx='6'/><rect x='315' y='55' width='240' height='60' rx='6'/></g><g font-family='ui-monospace, monospace' font-size='18' fill='currentColor' text-anchor='middle'><text x='115' y='92'>2</text><text x='195' y='92'>1</text><text x='275' y='92'>3</text><text x='355' y='92'>4</text><text x='435' y='92'>6</text><text x='515' y='92'>5</text></g><g font-family='ui-monospace, monospace' font-size='13' fill='currentColor' text-anchor='middle'><text x='115' y='135'>0</text><text x='195' y='135'>1</text><text x='275' y='135'>2</text><text x='355' y='135'>3</text><text x='435' y='135'>4</text><text x='515' y='135'>5</text></g><g font-family='ui-monospace, monospace' font-size='14' fill='currentColor' text-anchor='middle'><text x='195' y='170'>2&gt;1, 3&gt;2 (wrap)</text><text x='195' y='192'>2 descents: fail</text><text x='435' y='170'>6&gt;5</text><text x='435' y='192'>1 descent: ok</text></g></svg>
```
**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
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 640 220'><text x='20' y='30' font-family='sans-serif' font-size='15' fill='currentColor'>k = 6: one block</text><g stroke='currentColor'><rect x='80' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/><rect x='160' y='60' width='70' height='50' fill='none'/><rect x='240' y='60' width='70' height='50' fill='none'/><rect x='320' y='60' width='70' height='50' fill='none'/><rect x='400' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/><rect x='480' y='60' width='70' height='50' fill='#ef4444' fill-opacity='0.3'/></g><rect x='75' y='55' width='480' height='60' rx='6' fill='none' stroke='currentColor' stroke-width='2.5'/><g font-family='ui-monospace, monospace' font-size='18' fill='currentColor' text-anchor='middle'><text x='115' y='92'>2</text><text x='195' y='92'>1</text><text x='275' y='92'>3</text><text x='355' y='92'>4</text><text x='435' y='92'>6</text><text x='515' y='92'>5</text></g><g font-family='ui-monospace, monospace' font-size='13' fill='currentColor' text-anchor='middle'><text x='115' y='135'>0</text><text x='195' y='135'>1</text><text x='275' y='135'>2</text><text x='355' y='135'>3</text><text x='435' y='135'>4</text><text x='515' y='135'>5</text></g><text x='80' y='170' font-family='ui-monospace, monospace' font-size='14' fill='currentColor'>descents: 2&gt;1, 6&gt;5, 5&gt;2 (wrap) = 3 -&gt; fail</text><text x='80' y='200' font-family='sans-serif' font-size='15' fill='currentColor'>Answer = 2</text></svg>
```
## 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
```