Created
August 18, 2020 21:21
-
-
Save bparanj/dea69b3205ddaa05b4b286e752937999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param {Integer[]} nums | |
# @param {Integer} n | |
# @return {Integer[]} | |
def shuffle(nums, n) | |
result = [] | |
first = 0 | |
second = n | |
index = 0 | |
while result.size < 2*n | |
result[index] = nums[first] | |
first += 1 | |
index += 1 | |
result[index] = nums[second] | |
second += 1 | |
index += 1 | |
end | |
result | |
end | |
nums = [2,5,1,3,4,7] | |
n = 3 | |
p shuffle(nums, n) |
Find the relationship between the given input parameters. This relationship is used in the terminating condition of the while loop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use two pointers to create the new array of 2n elements. The first starting at the beginning and the other starting at (n+1)th position. Alternate between them and create the new array.