Shuffle an Array

MediumArrayHash TableBacktracking

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]
Explanation:

One valid shuffle result is [1,2,3], so the result is [1,2,3].

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

Edge case with a single-element array.

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

Shuffling the duplicate elements still yields [5,5], so the result is [5,5].

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!