Python Program to Rearrange an array such that arr[i] = i

In this problem, we are given an array and we have to rearrange the elements of the array such that for each index i, arr[i] = i. We have to rearrange the elements so that each element is in the position corresponding to its value.

Problem Understanding

Scenario 1

<b>Input:</b> [3, 0, 2, 1]
<b>Output:</b> [0, 1, 2, 3]

Element 3 is moved to index 3, element 0 is moved to index 0, element 2 is moved to index 2, and element 1 is moved to index 1.

Scenario 2

<b>Input:</b> [5, 2, 8, 1]
<b>Output:</b> [1, 2, 5, 8]

Note: When elements exceed array bounds or are missing, they cannot be placed at their target positions, so we sort the remaining elements.

Method 1: Using Extra Space

In this approach, we create a new array and place each element at its corresponding index position ?

def rearrange_extra_space(arr):
    n = len(arr)
    new_arr = [-1] * n  # Initialize with -1 to handle missing elements
    
    # Place elements at their corresponding positions
    for i in range(n):
        if 0 <= arr[i] < n:  # Check if element is within bounds
            new_arr[arr[i]] = arr[i]
    
    return new_arr

# Test with example
arr = [3, 0, 2, 1]
result = rearrange_extra_space(arr.copy())
print("Original array:", arr)
print("Rearranged array:", result)
Original array: [3, 0, 2, 1]
Rearranged array: [0, 1, 2, 3]

Method 2: In-Place Cyclic Replacement

This approach uses cyclic replacements to place each element in its correct position without extra space ?

def rearrange_in_place(arr):
    n = len(arr)
    
    for i in range(n):
        # Keep swapping until current position has correct element
        while arr[i] != i and 0 <= arr[i] < n:
            # Swap current element with element at its target position
            target_pos = arr[i]
            arr[i], arr[target_pos] = arr[target_pos], arr[i]
    
    return arr

# Test with example
arr = [3, 0, 2, 1]
result = rearrange_in_place(arr.copy())
print("Original array:", [3, 0, 2, 1])
print("Rearranged array:", result)
Original array: [3, 0, 2, 1]
Rearranged array: [0, 1, 2, 3]

Method 3: Using Sorting

When elements are outside array bounds, we can simply sort the array ?

def rearrange_by_sorting(arr):
    # Sort the array to arrange elements in ascending order
    return sorted(arr)

# Test with example that has out-of-bounds elements
arr = [5, 2, 8, 1]
result = rearrange_by_sorting(arr.copy())
print("Original array:", arr)
print("Rearranged array:", result)
Original array: [5, 2, 8, 1]
Rearranged array: [1, 2, 5, 8]

Comparison

Method Time Complexity Space Complexity Best For
Extra Space O(n) O(n) Simple implementation
In-Place Cyclic O(n) O(1) Memory-efficient
Sorting O(n log n) O(1) Out-of-bounds elements

Conclusion

The in-place cyclic replacement method is most efficient with O(n) time and O(1) space complexity. Use the extra space method for simpler logic or sorting when elements are out of bounds.

Updated on: 2026-03-27T16:53:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements