Lets Code Everyday - Day 7

Hello there ๐, enthusiasts.
Welcome to another day of learning and growing.
Question - 7 :
Shuffle the Array
Given the array nums consisting of 2n elements in the form [x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>].
Return the array in the form [x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>].
Example 1:
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
Example 2:
Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
Example 3:
Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]
First approach:
class Solution {
public int[] shuffle(int[] nums, int n) {
List<Integer> shuffledList=new ArrayList<>();
for(int i=0;i<n;i++){
Collections.addAll(shuffledList,nums[i],nums[i+n]);
}
return shuffledList.stream()
.mapToInt(Integer::intValue)
.toArray();
}
}
Step-by-step explanation:
Create an empty
ArrayListnamedshuffledListto hold the shuffled elements.Iterate over the first
nelements of thenumsarray. In each iteration:Add the current element at index
ito theshuffledList.Add the element at index
i+nto theshuffledList.
This step essentially takes the first n elements of nums and interleaves them with the second n elements of nums to create the shuffled list.
Convert the
shuffledListto an array of integers using thestream()method,mapToInt()method, andtoArray()method of the Java 8 Stream API.Return the resulting array of shuffled integers.
Overall, the shuffle method takes an array of integers and a number n, shuffles the first 2n elements of the array in a specific way, and returns the resulting shuffled array.
Second approach:
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] shuffled = new int[2 * n];
int index = 0;
for (int i = 0; i < n; i++) {
shuffled[index++] = nums[i];
shuffled[index++] = nums[i + n];
}
return shuffled;
}
}
Step-by-step explanation:
Create an empty integer array
shuffledwith a length of2nto hold the shuffled elements.Initialize an integer variable
indexto 0, which will be used to keep track of the current index in theshuffledarray.Iterate over the first
nelements of thenumsarray. In each iteration:Add the current element at index
ito theshuffledarray at indexindex.Add the element at index
i+nto theshuffledarray at indexindex+1.Increment the
indexby 2 to move to the next pair of indices.
This step essentially takes the first n elements of nums and interleaves them with the second n elements of nums to create the shuffled array.
- Return the resulting array of shuffled integers.
Overall, the shuffle method takes an array of integers and a number n, shuffles the first 2n elements of the array in a specific way, and returns the resulting shuffled array.
Conclusion:
Both the approaches have the same time and space complexities i.e., O(n) & O(n).Compared to the First code that used an ArrayList, the Second implementation has a better space complexity because it does not use an additional data structure to hold the shuffled elements.
Thanks for reading......๐




