CT062

Fisher-Yates Shuffle

EasyAcceptance: 0.0%

Given a function that generates perfectly random integers between 1 and k, implement a shuffle of an array using only swaps such that each of the n! permutations is equally likely (Fisher-Yates shuffle). For testing, given an array and seed, output the shuffled array. Input: first line n, second line n integers, third line seed. Output: shuffled array.

Example 1:

Input: 5 1 2 3 4 5 42
Output: 3 1 5 2 4

Example 2:

Input: 3 10 20 30 1
Output: 10 30 20

Constraints:

1 <= n <= 10^4 0 <= arr[i] <= 10^9 0 <= seed <= 10^9

Tags:

randomized-algorithm array
Loading...
Test Cases:No test cases
No test cases available.