If you've spent time on LeetCode or GeeksforGeeks, you've probably run into the classic "Rearrange Array Alternately" problem.
The premise: given a sorted array of positive integers, rearrange it so the first element is the maximum, the second is the minimum, the third is the second maximum, the fourth is the second minimum, and so on.
For [1, 2, 3, 4, 5], the expected output is [5, 1, 4, 2, 3].
The Standard Solutions
There are usually two approaches taught for this:
Temporary array (O(N) time, O(N) space) — use two pointers and a separate array to pick the largest and smallest numbers alternately.
Modulo math trick (O(N) time, O(1) space) — encode two numbers into a single index using old_val + (new_val % M) * M.
The modulo trick is the standard "optimal" answer, but it feels more like a mathematical loophole than genuine array manipulation. While experimenting with pointer logic, I found a different way to hit O(1) space using pure structural manipulation inst
Discussion
Your thoughts matter!
Your input is valuable—be the first to share it!