Sum of Sortable Integers

You are given an integer array nums of length n.

An integer k is considered sortable if it strictly divides n and you can sort the array nums in non-decreasing order by sequentially performing the following operations:

  • Partition nums into consecutive subarrays (blocks) of length k.
  • Independently apply cyclic rotations (left or right) to each subarray any number of times.

Return an integer denoting the sum of all possible sortable integers k.

Example 1:

Input: nums = [4, 3, 2, 1]

Output: 0

Explanation:

n = 4, possible divisors are 1, 2, and 4. The globally sorted array is [1, 2, 3, 4]. No matter which divisor is chosen as the block size k, the subarrays cannot be cyclically rotated to match the strictly non-decreasing global order. Sum = 0.

Example 2:

Input: nums = [2, 2, 2, 2]

Output: 7

Explanation:

n = 4, possible divisors are 1, 2, and 4. The globally sorted array is [2, 2, 2, 2]. Because all elements are identical, the array is already sorted. Any valid block partition (k = 1, 2, or 4) naturally matches the sorted state without requiring any rotations. Sum = 1 + 2 + 4 = 7.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [2, 1, 4, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

1 <= n == nums.length <= 105

1 <= nums[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int sumOfSortableIntegers(vector<int>& nums) {
// Write your code here
}
};
Test Case

Input:

Nums