Sum of Sortable Integers Problem
Description
LeetCode Problem 3886.
You are given an integer array nums of length n.
An integer k is called sortable if k divides n and you can sort nums in non-decreasing order by sequentially performing the following operations:
- Partition
numsinto consecutive subarrays of lengthk. - Cyclically rotate each subarray independently any number of times to the left or to the right.
Return an integer denoting the sum of all possible sortable integers k.
Example 1:
Input: nums = [3,1,2]
Output: 3
Explanation:
- For n = 3, possible divisors are 1 and 3.
- For k = 1: each subarray has one element. No rotation can sort the array.
- For k = 3: the single subarray [3, 1, 2] can be rotated once to produce [1, 2, 3], which is sorted.
- Only k = 3 is sortable. Hence, the answer is 3.
Example 2:
Input: nums = [7,6,5]
Output: 0
Explanation:
- For n = 3, possible divisors are 1 and 3.
- For k = 1: each subarray has one element. No rotation can sort the array.
- For k = 3: the single subarray [7, 6, 5] cannot be rotated into non-decreasing order.
- No k is sortable. Hence, the answer is 0.
Example 3:
Input: nums = [5,8]
Output: 3
Explanation:
- For n = 2, possible divisors are 1 and 2.
- Since [5, 8] is already sorted, every divisor is sortable. Hence, the answer is 1 + 2 = 3.
Constraints:
1 <= n == nums.length <= 10^51 <= nums[i] <= 10^5
Solution
Approach: Enumerate Divisors + Check
Build three helper arrays: prefMax[i] is the maximum of nums[0..i-1], suffMin[i] is the minimum of nums[i..n-1], and prefDrop[i] counts the positions j <= i with nums[j - 1] > nums[j].
Then try every divisor k of n and look at each block of length k starting at start. A block can be rotated into sorted order only if, read as a circle, it has at most one descent: the descents inside the block, prefDrop[end] - prefDrop[start], plus one when the last element is larger than the first, must be at most 1. In addition, every element before the block must be no larger than every element from the block on, so prefMax[start] <= suffMin[start]. If all blocks pass, add k to the answer. Divisors are found in pairs up to sqrt(n).
Complexity: time O(n + sigma(n)), where sigma(n) is the sum of the divisors of n (each divisor k costs n / k block checks); space O(n).
class Solution:
def sortableIntegers(self, nums: List[int]) -> int:
n = len(nums)
pref_max = [0] * (n + 1)
for i, x in enumerate(nums):
pref_max[i + 1] = max(pref_max[i], x)
suff_min = [10**9] * (n + 1)
for i in range(n - 1, -1, -1):
suff_min[i] = min(nums[i], suff_min[i + 1])
pref_drop = [0] * n
for i in range(1, n):
pref_drop[i] = pref_drop[i - 1] + (nums[i - 1] > nums[i])
def check(k: int) -> bool:
for start in range(0, n, k):
end = start + k - 1
drops = pref_drop[end] - pref_drop[start]
if nums[end] > nums[start]:
drops += 1
if drops > 1 or pref_max[start] > suff_min[start]:
return False
return True
ans = 0
i = 1
while i <= n // i:
if n % i == 0:
if check(i):
ans += i
other = n // i
if i != other and check(other):
ans += other
i += 1
return ans