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:
- Define an empty array to store the shuffled elements of the given array.
shuffled_arr = []
- Initialize two pointers
i
andj
to point to the first and then+1
th element of the given array respectively.
i = 0
j = n - Iterate through the array until
i
is less thann
.
while i < n:
- Append the
i
th andj
th element of the given array to theshuffled_arr
.
shuffled_arr.append(nums[i])
shuffled_arr.append(nums[j]) - Increment both
i
andj
by 1.
i += 1
j += 1 - 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; };
vectorshuffle(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; }