diff --git a/go/1470-shuffle-the-array.go b/go/1470-shuffle-the-array.go new file mode 100644 index 000000000..cd3c91ac4 --- /dev/null +++ b/go/1470-shuffle-the-array.go @@ -0,0 +1,25 @@ +package main + +import "math" + +func main() { + +} + +func shuffle(nums []int, n int) []int { + for i := 0; i < n; i++ { + nums[i] = nums[i] << 10 + nums[i] = nums[i] | nums[i+n] + } + + j := 2*n - 1 + + for i := n - 1; i > -1; i-- { + y := nums[i] & (int(math.Pow(2.,10.)) - 1) + x := nums[i] >> 10 + nums[j] = y + nums[j-1] = x + j -= 2 + } + return nums +} \ No newline at end of file diff --git a/javascript/1470-shuffle-the-array.js b/javascript/1470-shuffle-the-array.js new file mode 100644 index 000000000..af377ea1e --- /dev/null +++ b/javascript/1470-shuffle-the-array.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @param {number} n + * @return {number[]} + */ +var shuffle = function (nums, n) { + for (let i = 0; i < n; i++) { + nums[i] = nums[i] << 10; + nums[i] = nums[i] | nums[i + n]; + } + + let j = 2 * n - 1; + + for (let i = n - 1; i > -1; i--) { + let y = nums[i] & (2 ** 10 - 1); + let x = nums[i] >> 10; + nums[j] = y; + nums[j - 1] = x; + j -= 2; + } + + return nums; +}; diff --git a/typescript/1470-shuffle-the-array.ts b/typescript/1470-shuffle-the-array.ts new file mode 100644 index 000000000..fb44d99e3 --- /dev/null +++ b/typescript/1470-shuffle-the-array.ts @@ -0,0 +1,18 @@ +function shuffle(nums: number[], n: number): number[] { + for (let i = 0; i < n; i++) { + nums[i] = nums[i] << 10; + nums[i] = nums[i] | nums[i + n]; + } + + let j = 2 * n - 1; + + for (let i = n - 1; i > -1; i--) { + let y = nums[i] & (2 ** 10 - 1); + let x = nums[i] >> 10; + nums[j] = y; + nums[j - 1] = x; + j -= 2; + } + + return nums; +}