Rearrange an array so that arri becomes arrarri with O1 extra space
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computer science, array manipulation is a fundamental concept with myriad applications in data processing, optimization, and problem-solving. A particularly interesting array transformation challenge is rearranging an array such that each element at index i is replaced by the element at the index currently pointed by arr[i], using only extra space. This problem is often encountered in coding interviews and competitive programming.
Problem Statement
Given an array arr[] where each element is within the range of to $n-1$``, rearrange the array so that each element arr[i]becomesarr[arr[i]] using ``$O(1)$ extra space.
Explanation & Approach
The main challenge in solving this problem within the extra space constraint is altering the array without losing the original values since they are required to position the reordered values correctly. The key is to refine the approach by encoding additional information into the array elements themselves.
Let’s step through this problem using an example:
Example:
Suppose we have an array arr[] = {4, 0, 2, 1, 3}.
- Objective: Transform the array so that each element at
ibecomesarr[arr[i]]. - Encode two numbers at each index: You will store the new array values alongside the original ones by encoding them in a single integer. Assume all numbers fit in the array are between and .
The formula to encode two numbers x and y is:
In this context, x is the old value, and (original_value) is the new value to be stored.
- Decoding the updated value: You can extract the new value and the original value by:
- New value (arr[arr[i]]):
- Original value:
Steps to Implement
- Encode the new and old values together:
- Decode the new values while replacing the array:
- Driver function: Combine encoding and decoding.
Final Output:
For arr[] = {4, 0, 2, 1, 3}, post transformation, the result is arr[] = {3, 4, 2, 0, 1}.
Key Considerations
- Time Complexity: The algorithm operates in time, where is the number of elements in the array due to its single pass encoding and decoding steps.
- Space Complexity: Since the transformation uses only a constant amount of extra space beyond the input array, the space complexity is .
- Constraints: The approach is limited by integer size since
arr[i] + (arr[arr[i]] \% n) * nmust fit in the variable type used (typicallyint).
Use-Cases
- Permutation problems: The technique is useful in scenarios where permutations of the array need to be transformed without auxiliary storage.
- Optimal Memory Applications: Situations requiring in-place transformations, like embedded systems or low-memory environments, benefit greatly from this approach.
Summary Table
| Concept | Description |
| Objective | Rearrange array such that arr[i] becomes arr[arr[i]]. |
| Time Complexity | |
| Space Complexity | |
| Encoding Formula | |
| Decoding Formula | Extract recycled values from: (new value), (original value) |
| Example Input | arr[] = {4, 0, 2, 1, 3} |
| Example Output | arr[] = {3, 4, 2, 0, 1} |
By understanding these principles and employing the outlined strategy, one can effectively tackle this complex array rearrangement challenge in interviews and other algorithm-intensive tasks.
Related reading
- Reason for the number 5381 in the DJB hash function?
- Rebalancing an arbitrary BST?
- Recommendation algorithm and implementation for finding similar items and users
- Recommendation Algorithms for tweets in C
- Reasons for using a Bag in Java
- Rebalancing rate when new node is added
- Recommendations for Fast Multipole Method implementation?
- Recommendations for using graphs theory in machine learning?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.