Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created August 18, 2020 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bparanj/dea69b3205ddaa05b4b286e752937999 to your computer and use it in GitHub Desktop.
Save bparanj/dea69b3205ddaa05b4b286e752937999 to your computer and use it in GitHub Desktop.
# @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)
@bparanj
Copy link
Author

bparanj commented Aug 18, 2020

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.

@bparanj
Copy link
Author

bparanj commented Aug 18, 2020

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