Say you want to iterate over a sequence [0 to n] in a random order, visiting every element exactly once. Is there any way to do this in O(1) memory, i.e. without creati
I've just built a structure for this sort of thing - I generate a Heap structure (min or max, doesn't matter). But for the comparison, instead of using a key value, I use a random number. Items inserted into the heap are thus placed in random order. Then you can either return the array that forms the heap's base structure (which will be randomly ordered), or you can pop the elements off one by one, and get them back in random order. If this type of your container is used as your primary storage (rather than having an array separate from the heap), there is no additional memory complexity, since it's just an array anyhow. Time complexity is O(log N) for insertion, O(log N) for popping the top element. Shuffling is as simple as popping and reinserting each element, O(N log N).
I even built a fancy Enumerator (it's C#, but you could do the same with a C++ Iterator) that auto-shuffles after you've iterated through to the end. That means that every time you can iterate through the list (without popping) multiple times and get a different order every time, at the cost of an O(N log N) shuffle after each full iteration. (Think like a deck of cards. After every card has gone to the discard pile, you reshuffle the deck so as to not get them in the same order next time around.)