Program to find array by swapping consecutive index pairs in Python

Given a list of numbers, we need to swap consecutive pairs at even indexes with each other, and consecutive pairs at odd indexes with each other. This creates a specific pattern where elements at positions (0,2), (1,3), (4,6), (5,7), etc. are swapped.

Problem Understanding

The swapping pattern works as follows ?

  • Swap elements at indexes 0 and 2 (first even pair)
  • Swap elements at indexes 1 and 3 (first odd pair)
  • Swap elements at indexes 4 and 6 (second even pair)
  • Swap elements at indexes 5 and 7 (second odd pair)
  • Continue this pattern for the entire array
Original: 8 0 5 1 3 2 4 3 8 4 9 5 Result: 3 4 8 5 3 6 Even indexes Odd indexes

Algorithm Steps

To solve this problem, we follow these steps ?

  • Iterate through the array with a step of 4 (to process groups of 4 elements)
  • For each group, swap elements at positions i and i+2 (even indexes)
  • Swap elements at positions i+1 and i+3 (odd indexes)
  • Handle boundary conditions to avoid index out of range errors

Example

Let us see the implementation to get better understanding ?

def solve(nums):
    for i in range(0, len(nums) - 2, 4):
        if i + 2 < len(nums):
            nums[i], nums[i + 2] = nums[i + 2], nums[i]
        if i + 3 < len(nums):
            nums[i + 1], nums[i + 3] = nums[i + 3], nums[i + 1]
    
    return nums

nums = [8, 5, 3, 4, 8, 9, 3, 6, 4, 7]
result = solve(nums.copy())  # Use copy to preserve original
print("Original:", nums)
print("Result:  ", result)
Original: [8, 5, 3, 4, 8, 9, 3, 6, 4, 7]
Result:   [3, 4, 8, 5, 3, 6, 8, 9, 4, 7]

How It Works

The algorithm processes the array in chunks of 4 elements. For the input [8, 5, 3, 4, 8, 9, 3, 6, 4, 7] ?

  • First iteration (i=0): Swap nums[0] with nums[2] ? [3, 5, 8, 4, 8, 9, 3, 6, 4, 7]
  • First iteration (i=0): Swap nums[1] with nums[3] ? [3, 4, 8, 5, 8, 9, 3, 6, 4, 7]
  • Second iteration (i=4): Swap nums[4] with nums[6] ? [3, 4, 8, 5, 3, 9, 8, 6, 4, 7]
  • Second iteration (i=4): Swap nums[5] with nums[7] ? [3, 4, 8, 5, 3, 6, 8, 9, 4, 7]
  • Third iteration (i=8): Only nums[8] and nums[9] remain, no swapping occurs

Edge Cases

The boundary conditions handle arrays that don't have complete groups of 4 elements ?

# Test with different array sizes
test_cases = [
    [1, 2],           # Length 2
    [1, 2, 3],        # Length 3  
    [1, 2, 3, 4, 5],  # Length 5
]

for i, nums in enumerate(test_cases):
    original = nums.copy()
    result = solve(nums)
    print(f"Test {i+1}: {original} ? {result}")
Test 1: [1, 2] ? [1, 2]
Test 2: [1, 2, 3] ? [3, 2, 1]
Test 3: [1, 2, 3, 4, 5] ? [3, 4, 1, 2, 5]

Conclusion

This algorithm efficiently swaps consecutive pairs at even and odd indexes by iterating through the array in steps of 4. The boundary checks ensure it works correctly with arrays of any length, making it a robust solution for the given problem.

Updated on: 2026-03-26T16:40:52+05:30

556 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements