Move all odd positioned element to left half and even positioned to right half in-place
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In the realm of data structures and algorithms, manipulating arrays based on specific conditions is a common task. An intriguing problem is moving all odd-positioned elements of an array to the left half, while the even-positioned elements are relocated to the right half of the array. This task must be achieved in-place, meaning without using additional memory for duplicates of the array. This article will delve into an approach for solving this problem, offering a detailed explanation and implementation example.
Problem Statement
Given an array, rearrange its elements so that the elements that were in odd positions move to the left half of the array and elements from even positions move to the right half of the array. This must be done in-place, which means using O(1) extra space.
For clarity:
- Odd-positioned elements are those appearing at 1st, 3rd, 5th positions, etc., when counting starts from index 0.
- Even-positioned elements appear at 2nd, 4th, 6th positions, etc.
Approach and Algorithm
An efficient way to solve this problem is by using a two-pointer technique that enables in-place modification of the array.
Steps:
- Initialize Pointers:
- Use two pointers, `i` and `j`. Initialize `i` at the beginning (index 0) of the array, and `j` at the halfway point of the array if the number of elements is even or if odd, the division result of `(n // 2) + 1`.
- Segregate Elements:
- Iterate over the array:
- If the current index is odd (i.e., index % 2 != 0), the element is considered odd-positioned, and it should be placed from the beginning indexed by `i`.
- If the index is even (i.e., index % 2 == 0), place these elements from the `j` position in the array.
- Increment `i` when adding to the left portion.
- Increment `j` when adding to the right portion.
- Iterate and Swap:
- Execute comparisons and place elements in their respective positions by swapping elements where necessary to reduce unnecessary empty spaces or overlaps.
Here's a concise implementation of this algorithm in Python:
- Time Complexity: The approach involves a simple traversal of the array, leading to a time complexity of . The array is traversed once to place elements into their new positions.
- Space Complexity: The task requires additional space since we ensure the array itself is utilized through in-place operations to achieve rearrangement. However, the use of a temporary array in the above code increases space usage to . An optimal method would involve direct swaps.
- Initially, the array [1, 2, 3, 4] is given.
- During Iteration 1, the odd index is 0, so 1 stays.
- Iteration 3 moves 4 to the end, switching its place with the original element at index 1.
- The final array is [1, 3, 4, 2], satisfying the rearrangement conditions.

