Before attempting this problem, you should be comfortable with:
Array Indexing - Calculating source and destination indices based on position formulas
Bit Manipulation - Packing and unpacking multiple values in a single integer for in-place solutions
Modular Arithmetic - Using multiplication and modulo to encode/decode values without overflow
1. Iteration (Extra Space)
Intuition
The array is given as [x1, x2, ..., xn, y1, y2, ..., yn] and we need to rearrange it to [x1, y1, x2, y2, ..., xn, yn]. The simplest approach is to iterate through the first half of the array and alternate between picking elements from the first half (x values at index i) and the second half (y values at index i + n). We build the result in a new array.
Algorithm
Create an empty result array.
Loop i from 0 to n - 1:
Append nums[i] (the x value) to the result.
Append nums[i + n] (the corresponding y value) to the result.
classSolution:defshuffle(self, nums: List[int], n:int)-> List[int]:
res =[]for i inrange(n):
res.append(nums[i])
res.append(nums[i + n])return res
publicclassSolution{publicint[]shuffle(int[] nums,int n){int[] res =newint[2* n];int idx =0;for(int i =0; i < n; i++){
res[idx++]= nums[i];
res[idx++]= nums[i + n];}return res;}}
classSolution{public:
vector<int>shuffle(vector<int>& nums,int n){
vector<int> res;for(int i =0; i < n; i++){
res.push_back(nums[i]);
res.push_back(nums[i + n]);}return res;}};
classSolution{/**
* @param {number[]} nums
* @param {number} n
* @return {number[]}
*/shuffle(nums){const res =[];for(let i =0; i < n; i++){
res.push(nums[i]);
res.push(nums[i + n]);}return res;}}
publicclassSolution{publicint[]Shuffle(int[] nums,int n){int[] res =newint[2* n];int idx =0;for(int i =0; i < n; i++){
res[idx++]= nums[i];
res[idx++]= nums[i + n];}return res;}}
funcshuffle(nums []int, n int)[]int{
res :=make([]int,0,2*n)for i :=0; i < n; i++{
res =append(res, nums[i])
res =append(res, nums[i+n])}return res
}
class Solution {funshuffle(nums: IntArray, n: Int): IntArray {val res =IntArray(2* n)var idx =0for(i in0 until n){
res[idx++]= nums[i]
res[idx++]= nums[i + n]}return res
}}
classSolution{funcshuffle(_ nums:[Int],_ n:Int)->[Int]{var res =[Int]()for i in0..<n {
res.append(nums[i])
res.append(nums[i + n])}return res
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n) extra space.
2. Multiplication And Modulo
Intuition
To shuffle in-place without extra space, we can encode two values in a single array element. Since values are at most 1000, we pick a base M > 1000. For each position i in the result, we determine which original element belongs there and encode it by adding original_value * M to nums[i]. The original value is retrieved using nums[index] % M (since we have not yet divided), and the new value is stored in the upper part. After encoding all positions, we divide each element by M to extract the shuffled values.
Algorithm
Choose M = max(nums) + 1 or a constant like 1001.
For each index i from 0 to 2n - 1:
If i is even, the value should come from position i / 2 (an x value).
If i is odd, the value should come from position n + i / 2 (a y value).
Add (nums[source] % M) * M to nums[i].
Divide each element by M to get the final shuffled array.
classSolution:defshuffle(self, nums: List[int], n:int)-> List[int]:
M =max(nums)+1for i inrange(2* n):if i %2==0:
nums[i]+=(nums[i //2]% M)* M
else:
nums[i]+=(nums[n + i //2]% M)* M
for i inrange(2* n):
nums[i]//= M
return nums
publicclassSolution{publicint[]shuffle(int[] nums,int n){intM=1001;for(int i =0; i <2* n; i++){if(i %2==0){
nums[i]+=(nums[i /2]%M)*M;}else{
nums[i]+=(nums[n + i /2]%M)*M;}}for(int i =0; i <2* n; i++){
nums[i]/=M;}return nums;}}
classSolution{public:
vector<int>shuffle(vector<int>& nums,int n){int M =*max_element(nums.begin(), nums.end())+1;for(int i =0; i <2* n; i++){if(i %2==0){
nums[i]+=(nums[i /2]% M)* M;}else{
nums[i]+=(nums[n + i /2]% M)* M;}}for(int i =0; i <2* n; i++){
nums[i]/= M;}return nums;}};
classSolution{/**
* @param {number[]} nums
* @param {number} n
* @return {number[]}
*/shuffle(nums){constM= Math.max(...nums)+1;for(let i =0; i <2* n; i++){if(i %2===0){
nums[i]+=(nums[i >>1]%M)*M;}else{
nums[i]+=(nums[n +(i >>1)]%M)*M;}}for(let i =0; i <2* n; i++){
nums[i]= Math.floor(nums[i]/M);}return nums;}}
publicclassSolution{publicint[]Shuffle(int[] nums,int n){int M =1001;for(int i =0; i <2* n; i++){if(i %2==0){
nums[i]+=(nums[i /2]% M)* M;}else{
nums[i]+=(nums[n + i /2]% M)* M;}}for(int i =0; i <2* n; i++){
nums[i]/= M;}return nums;}}
funcshuffle(nums []int, n int)[]int{
M :=1001for i :=0; i <2*n; i++{if i%2==0{
nums[i]+=(nums[i/2]% M)* M
}else{
nums[i]+=(nums[n+i/2]% M)* M
}}for i :=0; i <2*n; i++{
nums[i]/= M
}return nums
}
class Solution {funshuffle(nums: IntArray, n: Int): IntArray {val M =1001for(i in0 until 2* n){if(i %2==0){
nums[i]+=(nums[i /2]% M)* M
}else{
nums[i]+=(nums[n + i /2]% M)* M
}}for(i in0 until 2* n){
nums[i]/= M
}return nums
}}
classSolution{funcshuffle(_ nums:[Int],_ n:Int)->[Int]{var nums = nums
letM=(nums.max()??0)+1for i in0..<(2* n){if i %2==0{
nums[i]+=(nums[i /2]%M)*M}else{
nums[i]+=(nums[n + i /2]%M)*M}}for i in0..<(2* n){
nums[i]/=M}return nums
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) extra space.
3. Bit Manipulation
Intuition
Similar to the multiplication approach, we can use bit manipulation to pack two values into one integer. Since values fit in 10 bits (max 1000 < 1024 = 2^10), we can store one value in the lower 10 bits and another in the upper bits. First, we pack each x and its corresponding y into the first half of the array. Then we unpack them from back to front into their final interleaved positions, ensuring we do not overwrite values we still need.
Algorithm
For each i from 0 to n - 1:
Combine nums[i] (x) and nums[i + n] (y) into nums[i] using: nums[i] = (nums[i] << 10) | nums[i + n].
Starting from i = n - 1 down to 0, and using a pointer j starting at 2n - 1:
classSolution:defshuffle(self, nums: List[int], n:int)-> List[int]:for i inrange(n):
nums[i]=(nums[i]<<10)| nums[i + n]# Store x, y in nums[i]
j =2* n -1for i inrange(n -1,-1,-1):
y = nums[i]&((1<<10)-1)
x = nums[i]>>10
nums[j]= y
nums[j -1]= x
j -=2return nums
publicclassSolution{publicint[]shuffle(int[] nums,int n){for(int i =0; i < n; i++){
nums[i]=(nums[i]<<10)| nums[i + n];// Store x, y in nums[i]}int j =2* n -1;for(int i = n -1; i >=0; i--){int y = nums[i]&((1<<10)-1);int x = nums[i]>>10;
nums[j]= y;
nums[j -1]= x;
j -=2;}return nums;}}
classSolution{public:
vector<int>shuffle(vector<int>& nums,int n){for(int i =0; i < n; i++){
nums[i]=(nums[i]<<10)| nums[i + n];// Store x, y in nums[i]}int j =2* n -1;for(int i = n -1; i >=0; i--){int y = nums[i]&((1<<10)-1);int x = nums[i]>>10;
nums[j]= y;
nums[j -1]= x;
j -=2;}return nums;}};
classSolution{/**
* @param {number[]} nums
* @param {number} n
* @return {number[]}
*/shuffle(nums){for(let i =0; i < n; i++){
nums[i]=(nums[i]<<10)| nums[i + n];// Store x, y in nums[i]}let j =2* n -1;for(let i = n -1; i >=0; i--){let y = nums[i]&((1<<10)-1);let x = nums[i]>>10;
nums[j]= y;
nums[j -1]= x;
j -=2;}return nums;}}
publicclassSolution{publicint[]Shuffle(int[] nums,int n){for(int i =0; i < n; i++){
nums[i]=(nums[i]<<10)| nums[i + n];// Store x, y in nums[i]}int j =2* n -1;for(int i = n -1; i >=0; i--){int y = nums[i]&((1<<10)-1);int x = nums[i]>>10;
nums[j]= y;
nums[j -1]= x;
j -=2;}return nums;}}
funcshuffle(nums []int, n int)[]int{for i :=0; i < n; i++{
nums[i]=(nums[i]<<10)| nums[i+n]// Store x, y in nums[i]}
j :=2*n -1for i := n -1; i >=0; i--{
y := nums[i]&((1<<10)-1)
x := nums[i]>>10
nums[j]= y
nums[j-1]= x
j -=2}return nums
}
class Solution {funshuffle(nums: IntArray, n: Int): IntArray {for(i in0 until n){
nums[i]=(nums[i]shl10)or nums[i + n]// Store x, y in nums[i]}var j =2* n -1for(i in n -1 downTo 0){val y = nums[i]and((1shl10)-1)val x = nums[i]shr10
nums[j]= y
nums[j -1]= x
j -=2}return nums
}}
classSolution{funcshuffle(_ nums:[Int],_ n:Int)->[Int]{var nums = nums
for i in0..<n {
nums[i]=(nums[i]<<10)| nums[i + n]// Store x, y in nums[i]}var j =2* n -1for i instride(from: n -1, through:0, by:-1){let y = nums[i]&((1<<10)-1)let x = nums[i]>>10
nums[j]= y
nums[j -1]= x
j -=2}return nums
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) extra space.
Common Pitfalls
Using Wrong Index for the Y Values
The array is structured as [x1, x2, ..., xn, y1, y2, ..., yn], where the y values start at index n, not index n-1. A common mistake is using nums[n + i - 1] instead of nums[n + i] when pairing x[i] with its corresponding y value, resulting in incorrect pairings.
Overwriting Values in In-Place Solutions
When attempting an in-place shuffle without extra space, a frequent error is overwriting values before they have been used. The bit manipulation and multiplication/modulo approaches carefully encode two values in one element to avoid this, but implementing them incorrectly (such as reading from a position after it has already been modified) corrupts the data and produces wrong results.