Everything happens on a copy, int[] a = (int[])nums.Clone(). If a were sorted largest first the answer would sit at index target = k - 1, and the loop shrinks a half-open window [lo, hi) around that index. Each pass takes the middle element as the pivot, a fixed choice so the same input gives the same partitions on every run, then does a three-way partition with the cursors gt, i and lt: values above the pivot move to the front, values below it to the back, equal values stay in the middle. C# tuple assignment, (a[gt], a[i]) = (a[i], a[gt]), swaps without a helper method. If target falls in the front band the window closes on it, if it falls in the equal band the pivot is the answer, otherwise the back band is kept. The equal band is what makes [5, 5, 5, 5] finish after one pass. On typical input the window roughly halves each pass, so the work is O(n) on average, and beyond the O(n) clone nothing is allocated.