Skip to main content
Back to problems
#3865
Medium Algorithms

Reverse k subarrays

Array Two Pointers
93.1% acceptance
Apr 3, 2026
3
2
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn reverse_subarrays(nums: Vec<i32>, k: i32) -> Vec<i32> {
    let mut nums = nums;
    let block_len = nums.len() / k as usize;

    for block in nums.chunks_mut(block_len) {
      block.reverse();
    }

    nums
  }
}