Sum of Valid Block Sizes
You are given an integer array nums of length n.
A positive integer k is called valid if all of the following are true:
- k divides n
- You split nums into consecutive blocks, each of size k
- For every block, you may rotate that block cyclically any number of times, either to the left or to the right
- After applying such rotations independently on the blocks, the entire array becomes sorted in non-decreasing order
Return the sum of all valid integers k.
A cyclic rotation means shifting elements inside a block while keeping their relative circular order.
For example, rotating [4, 1, 3] once to the left gives [1, 3, 4], and rotating it once to the right gives [3, 4, 1].
Example 1:
Input: nums = [2,1,4,3]
Output: 2
Explanation:
The divisors of n = 4 are 1, 2, 4.
- For k = 1: no change is possible, and [2,1,4,3] is not sorted
- For k = 2: blocks are [2,1] and [4,3]
- Rotate them to [1,2] and [3,4], so the full array becomes [1,2,3,4]
- For k = 4: rotations of [2,1,4,3] are [2,1,4,3], [1,4,3,2], [4,3,2,1], [3,2,1,4] none are sorted
Only k = 2 is valid, so the answer is 2.
Example 2:
Input: nums = [1,2,3,4]
Output: 7
Explanation:
The divisors of n = 4 are 1, 2, 4.
Since the array is already sorted, we can choose not to rotate any block.
So all divisors are valid:
- k = 1
- k = 2
- k = 4
Sum = 1 + 2 + 4 = 7.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [3,1,2]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- n == nums.length
- 1 <= nums[i] <= 105