Shuffle The Array

Solution For Shuffle The Array

Problem Statement:

Given an array nums consisting of 2n elements, return an array consisting of n elements of the original array shuffled in a way where nums[i] and nums[i + n] are shuffled together.

Example:
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7] Explanation: Since n = 3, we have [2,5,1] and [3,4,7]. After shuffling, the array becomes [2,3,5,4,1,7].

Solution:
We can solve this problem by iterating through the array and creating a new array where the elements are shuffled together. Below is the step-by-step solution to this problem:

  1. Define an empty array to store the shuffled elements of the given array.
    shuffled_arr = []
  2. Initialize two pointers i and j to point to the first and the n+1th element of the given array respectively.
    i = 0
    j = n
  3. Iterate through the array until i is less than n.
    while i < n:
  4. Append the ith and jth element of the given array to the shuffled_arr.
    shuffled_arr.append(nums[i])
    shuffled_arr.append(nums[j])
  5. Increment both i and j by 1.
    i += 1
    j += 1
  6. Return the shuffled_arr.
    return shuffled_arr

The complete function will look like this:
def shuffle(nums, n):
shuffled_arr = [] i = 0
j = n
while i < n:
shuffled_arr.append(nums[i])
shuffled_arr.append(nums[j])
i += 1
j += 1
return shuffled_arr

Time Complexity: O(n), where n is the length of the given array.

Space Complexity: O(n), where n is the length of the given array.

Step by Step Implementation For Shuffle The Array

class Solution { 
    public int[] shuffle(int[] nums, int n) {
        // create an empty array to store the shuffled elements
        int[] shuffled = new int[2 * n];
        
        // fill in the first half of the array with the elements from the first half of nums
        for (int i = 0; i < n; i++) {
            shuffled[2 * i] = nums[i];
        }
        
        // fill in the second half of the array with the elements from the second half of nums
        for (int i = 0; i < n; i++) {
            shuffled[2 * i + 1] = nums[i + n];
        }
        
        // return the shuffled array
        return shuffled;
    }
}
def shuffle(nums, n):
    # create an empty list to store the shuffled array
    shuffled_array = []
    
    # loop through the first half of the original array
    for i in range(0, n):
        # add the ith element from the first half to the list
        shuffled_array.append(nums[i])
        # add the ith element from the second half to the list
        shuffled_array.append(nums[i + n])
        
    # return the shuffled array
    return shuffled_array
/**
 * @param {number[]} nums
 * @param {number} n
 * @return {number[]}
 */
var shuffle = function(nums, n) {
    // create a new array to store the shuffled elements
    let shuffled = [];
    
    // loop through the first half of the original array
    for (let i = 0; i < n; i++) {
        // push the current element at index i
        shuffled.push(nums[i]);
        // push the element at index i + n
        shuffled.push(nums[i + n]);
    }
    
    return shuffled;
};
vector shuffle(vector& nums, int n) {
        vector res(2*n);
        int i=0,j=n;
        for(int k=0;k<2*n;k++)
        {
            if(k%2==0)
            {
                res[k]=nums[i];
                i++;
            }
            else
            {
                res[k]=nums[j];
                j++;
            }
        }
        return res;
    }
public int[] Shuffle(int[] nums, int n) {
    
    // create a new array to hold the shuffled elements
    int[] shuffled = new int[nums.Length];
    
    // create two pointers, one for the start of the first half
    // of the array and one for the start of the second half
    int first = 0;
    int second = n;
    
    // loop through the new array and assign the elements
    // from the two halves of the input array alternately
    for (int i = 0; i < shuffled.Length; i++)
    {
        if (i % 2 == 0)
        {
            shuffled[i] = nums[first];
            first++;
        }
        else
        {
            shuffled[i] = nums[second];
            second++;
        }
    }
    
    return shuffled;
}


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]