Skip to main content

Command Palette

Search for a command to run...

Lets Code Everyday - Day 7

Updated
โ€ข3 min read
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:

  1. Create an empty ArrayList named shuffledList to hold the shuffled elements.

  2. Iterate over the first n elements of the nums array. In each iteration:

    • Add the current element at index i to the shuffledList.

    • Add the element at index i+n to the shuffledList.

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.

  1. Convert the shuffledList to an array of integers using the stream() method, mapToInt() method, and toArray() method of the Java 8 Stream API.

  2. 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:

  1. Create an empty integer array shuffled with a length of 2n to hold the shuffled elements.

  2. Initialize an integer variable index to 0, which will be used to keep track of the current index in the shuffled array.

  3. Iterate over the first n elements of the nums array. In each iteration:

    • Add the current element at index i to the shuffled array at index index.

    • Add the element at index i+n to the shuffled array at index index+1.

    • Increment the index by 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.

  1. 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......๐Ÿ˜

More from this blog

Learn-->Share-->Learn

31 posts