Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

1470. Shuffle the Array 🌟

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn].

O(N) Time and O(N) Space solution

Code

class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector<int> ans(2*n);
        int l=0,r=n;
        for(int i=0;i<2*n;i+=2){
            ans[i]=nums[l];
            ans[i+1]=nums[r];
            l++,r++;
        }
        return ans;
    }
};