Skip to content

Commit b22e366

Browse files
Shuffle the array
1 parent ce6de8c commit b22e366

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

array/ans2.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Shuffle the Array
2+
/*
3+
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
4+
5+
Return the array in the form [x1,y1,x2,y2,...,xn,yn].
6+
7+
8+
9+
Example 1:
10+
11+
Input: nums = [2,5,1,3,4,7], n = 3
12+
Output: [2,3,5,4,1,7]
13+
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
14+
*/
15+
16+
17+
//Solution
18+
19+
var shuffle = function(nums, n) {
20+
const result = [];
21+
for (let i = 0; i < n; i++) {
22+
result.push(nums[i], nums[i+n]);
23+
}
24+
return result;
25+
};
26+
27+
28+
console.log(shuffle([2,5,1,3,4,7],3))//Output: [2,3,5,4,1,7]

0 commit comments

Comments
 (0)