#K53672. Shuffle the Array

    ID: 29584 Type: Default 1000ms 256MiB

Shuffle the Array

Shuffle the Array

You are given an array of integers. Your task is to implement a Fisher–Yates shuffle algorithm to generate a new permutation of the array. The algorithm is defined by the following formula:

[ for; i = n-1 ; downto ; 1:\quad{j = Random(0,i)} \quad{swap(a[i], a[j])} ]

For reproducibility, a random seed is provided as input. Note that the original array should not be modified; instead, produce a new shuffled array.

Example: If the input is a seed and an array of integers, your program should output a permutation of those integers as described.

inputFormat

The input is read from standard input (stdin). The first line contains two integers: the random seed and the number of elements ( n ). The second line contains ( n ) space-separated integers representing the array. ( n ) can be zero.

outputFormat

Print to standard output (stdout) a single line containing the ( n ) shuffled integers in order, separated by a single space. If ( n = 0 ), output an empty line.## sample

42 5
1 2 3 4 5
2 3 5 1 4