You are given an integer array nums of length n and an integer k.
You must partition the array into k contiguous subarrays of equal length and reverse each subarray.
It is guaranteed that n is divisible by k.
Return the resulting array after performing the above operation.
Example 1:
Input: nums = [1,2,4,3,5,6], k = 3
Output: [2,1,3,4,6,5]
Explanation:
k = 3 subarrays: [1, 2], [4, 3], and [5, 6].[2, 1], [3, 4], and [6, 5].[2, 1, 3, 4, 6, 5].Example 2:
Input: nums = [5,4,4,2], k = 1
Output: [2,4,4,5]
Explanation:
k = 1 subarray: [5, 4, 4, 2].[2, 4, 4, 5], which is the final array.
Constraints:
1 <= n == nums.length <= 10001 <= nums[i] <= 10001 <= k <= nn is divisible by k.Problem Overview: You are given an array and an integer k. The task is to reverse every contiguous subarray of size k. Process the array in chunks of length k and reverse the elements inside each chunk while preserving the order of the chunks themselves.
This problem focuses on careful index manipulation. You traverse the array in steps of k, and for each segment, reverse the elements in-place. The core technique is the classic two-pointer reversal commonly used in two pointers problems over an array.
Approach 1: Auxiliary Array Simulation (O(n) time, O(n) space)
The straightforward method creates a new result array. Iterate over the original array in blocks of size k. For each block, traverse it backward and append elements to the result. This simulates the reversal without modifying the input array. The logic is simple and reduces the risk of index mistakes, but it uses additional memory proportional to the input size.
This approach works well when immutability matters or when you want the clearest implementation. However, interviews usually expect an in-place solution when working with arrays.
Approach 2: In-Place Two-Pointer Reversal (O(n) time, O(1) space)
The optimal solution reverses each k-sized segment directly inside the array. Start from index 0 and move in increments of k. For each segment, set two pointers: left = start and right = min(start + k - 1, n - 1). Swap elements while left < right, moving the pointers toward the center.
This is the same logic used when reversing an array or string segment. Every element participates in at most one swap per segment, so the total work across the entire array remains linear. No additional memory is required beyond a few pointer variables.
The key insight: treat each chunk as an independent mini-array and apply a standard reversal routine. Because segments never overlap, processing them sequentially guarantees correctness.
Recommended for interviews: The in-place two-pointer method is what interviewers expect. It demonstrates that you can manipulate array indices safely and apply standard reversal logic efficiently. Mentioning the auxiliary array version first shows understanding of the brute-force simulation, but implementing the O(n) time and O(1) space approach signals stronger problem-solving skills.
Since we need to partition the array into k subarrays of equal length, the length of each subarray is m = \frac{n}{k}. We can use a loop to traverse the array with a step size of m, and in each iteration, reverse the current subarray.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1), as we only use a constant amount of extra space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Auxiliary Array Simulation | O(n) | O(n) | When clarity matters more than memory usage or when the original array must remain unchanged |
| In-Place Two-Pointer Reversal | O(n) | O(1) | Best general solution; optimal for interviews and large arrays |
Practice Reverse K Subarrays with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor