EPI Java, 6.13, p81 - Ways to choose elements to swap

#1

“The total number of ways in which we can choose the elements to swap is 3^3 = 27…”

I’m having a difficult time understanding what a “way to choose the elements” looks like?
An example of what a way to choose the elements for the array [1, 2, 3] would really help.

Thanks,
Joe

0 Likes

#2

Imagine you stop at each number in [1,2,3] and randomly choose one of the numbers in the array. For example, you stop at 1 and ask the random array entry picker to pick a number, and it picks 2 (options are (1,2,3)). Then you stop at 2 and randomly pick 2; stop at 3 and randomly pick 1. Now we’ve chosen [2,2,1]. Because at each position there were always 3 numbers to pick from, the total number of ways numbers could have been picked was 3 (at position 1) x 3 (at position 2) x 3 (at position 3) = 27. Make sense?

0 Likes

#3

Thanks for that, @lgtout. The description helped.

0 Likes

#4

Happy to help a fellow traveler!

0 Likes