Description

Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.

Examples

Input:nums = [1,2,3]
Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Explanation:

Any of the 6 permutations should be equally likely.

Input:nums = [1]
Output:[1]
Explanation:

Edge case with a single-element array.

Input:nums = [5,5]
Output:[[5,5],[5,5]]
Explanation:

With duplicate elements, there are still 2! = 2 possible arrangements by position, even though they appear identical. Each position swap should be equally likely.

Constraints

  • 1 ≤ nums.length ≤ 50
  • -10⁶ ≤ nums[i] ≤ 10⁶

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!