Skip to content

Commit d2b1b0b

Browse files
committed
added Shuffle the Array (easy)
1 parent 6162e3f commit d2b1b0b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Easy/ShuffleArray/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# Shuffle the Array
3+
[Leetcode Link](https://leetcode.com/problems/shuffle-the-array/)
4+
5+
## Problem:
6+
7+
Given the array `nums` consisting of `2n` elements in the form `[x1,x2,...,xn,y1,y2,...,yn]`.
8+
9+
Return the array in the form `[x1,y1,x2,y2,...,xn,yn]`.
10+
11+
## Example:
12+
13+
```
14+
Input: nums = [2,5,1,3,4,7], n = 3
15+
Output: [2,3,5,4,1,7]
16+
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
17+
```
18+
```
19+
Input: nums = [1,2,3,4,4,3,2,1], n = 4
20+
Output: [1,4,2,3,3,2,4,1]
21+
```
22+
```
23+
Input: nums = [1,1,2,2], n = 2
24+
Output: [1,2,1,2]
25+
```
26+
27+
## Note:
28+
29+
- `1 <= n <= 500`
30+
- `nums.length == 2n`
31+
- `1 <= nums[i] <= 10^3`

Easy/ShuffleArray/solution.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
class Solution:
4+
def shuffle(self, nums: List[int], n: int) -> List[int]:
5+
result = list()
6+
for x in range(n):
7+
result.append(nums[x])
8+
result.append(nums[x+n])
9+
return result
10+
11+
12+
# test driver
13+
sol = Solution()
14+
nums = [2,5,1,3,4,7]
15+
n = 3
16+
print(sol.shuffle(nums, n))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Languages used: Java and Python
3636
- [Rank Transform of an Array](Easy/RankTransformArray)
3737
- [Add to Array-Form of Integer](Easy/AddToArrayFormInteger)
3838
- [Sqrt(x)](Easy/SqrtX)
39+
- [Shuffle the Array](Easy/ShuffleArray)
3940
- Medium
4041
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
4142
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)