Find array with k number of merge sort calls in Python

Sometimes we need to construct an array that requires exactly k recursive calls when sorted using merge sort. This problem involves understanding how merge sort's recursive structure works and manipulating an array to achieve the desired call count.

Given two numbers a and b, we need to find an array containing values in range [1, a] that requires exactly b number of recursive merge sort calls.

Understanding the Problem

Merge sort makes recursive calls by dividing the array into halves. The total number of calls follows a pattern based on the array structure and element positions.

Algorithm Steps

To solve this, we follow these steps:

  • Check if b is odd (even values of b are impossible for this pattern)
  • Create an initial sorted array [1, 2, 3, ..., a]
  • Use a recursive function to swap elements strategically
  • Each swap operation adjusts the merge sort call count

Implementation

def solve(left, right, array, b):
    if b < 1 or left + 1 == right:
        return
    
    b -= 2
    mid = (left + right) // 2
    
    # Swap elements at mid-1 and mid positions
    temp = array[mid - 1]
    array[mid - 1] = array[mid]
    array[mid] = temp
    
    solve(left, mid, array, b)
    solve(mid, right, array, b)

def find_array(a, b):
    # Even values of b are not possible
    if b % 2 == 0:
        return None
    
    # Initialize array with values [1, 2, 3, ..., a]
    array = [0] * (a + 1)
    for i in range(a):
        array[i] = i + 1
    
    b -= 1
    solve(0, a, array, b)
    
    return array[:a]

# Example usage
a = 10
b = 15
result = find_array(a, b)

if result:
    print("Array:", result)
else:
    print("No solution possible")
Array: [3, 1, 4, 6, 2, 8, 5, 9, 10, 7]

How It Works

The algorithm works by:

  • Starting with sorted array: [1, 2, 3, ..., a]
  • Strategic swapping: Swaps elements at specific positions to increase merge sort calls
  • Recursive division: Applies the same logic to left and right subarrays
  • Call counting: Each swap operation adds exactly 2 to the total call count

Example Walkthrough

# Test with different values
test_cases = [(4, 5), (6, 9), (8, 11)]

for a, b in test_cases:
    result = find_array(a, b)
    if result:
        print(f"a={a}, b={b}: {result}")
    else:
        print(f"a={a}, b={b}: No solution")
a=4, b=5: [2, 1, 4, 3]
a=6, b=9: [3, 1, 2, 5, 4, 6]
a=8, b=11: [3, 1, 4, 2, 6, 5, 8, 7]

Key Points

  • Only odd values of b have valid solutions
  • The algorithm modifies a sorted array through strategic swaps
  • Each recursive call reduces the remaining call count by 2
  • The solution ensures exactly b merge sort calls are needed

Conclusion

This algorithm constructs arrays that require specific numbers of merge sort recursive calls by strategically swapping elements. The approach only works for odd values of b and uses recursive division to distribute the required calls across subarrays.

Updated on: 2026-03-25T09:14:10+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements