We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Merge Operations to Turn Array Into a Palindrome

Number: 2565

Difficulty: Medium

Paid? Yes

Companies: TikTok, Adobe, Amazon, Accolite


Problem Description

Given an array of positive integers, you can merge two adjacent elements by replacing them with their sum. The goal is to determine the minimum number of operations needed to convert the array into a palindrome.


Key Insights

  • Use a two-pointer approach with one pointer starting from the left and one from the right.
  • When the elements at both pointers are equal, move both pointers inward.
  • If the left element is less than the right, merge the left element with its adjacent neighbor (simulate by accumulating a sum) and count an operation.
  • Otherwise, merge on the right side.
  • Continue until the two pointers meet or cross.
  • The challenge is to merge in a way that eventually yields the same sequence reading from the left and right.

Space and Time Complexity

Time Complexity: O(n) — We make a single pass through the array with two pointers. Space Complexity: O(1) — Only a constant amount of extra space is used.


Solution

The solution uses a greedy two-pointer strategy. Two pointers are initialized at the beginning and end of the array. At each step, if the elements at both pointers are equal, both are already “matched” and the pointers move inward. If they are unequal, the smaller value is merged with its adjacent element (effectively adding its value to the next element in that direction) and an operation is counted. This process continues until the array is reduced to a valid palindrome. The merging effectively simulates the operation by accumulating sums, ensuring that the final result is palindromic with the minimum number of operations.


Code Solutions

# Python solution using a two-pointer approach
def minOperations(nums):
    # Initialize pointers and operations counter
    left = 0
    right = len(nums) - 1
    operations = 0

    # Loop until the two pointers meet
    while left < right:
        # If the elements at both pointers are equal, move inward
        if nums[left] == nums[right]:
            left += 1
            right -= 1
        # If left element is less than right, merge left side
        elif nums[left] < nums[right]:
            nums[left+1] += nums[left]   # merge with next adjacent number 
            left += 1                    # move left pointer to merged element
            operations += 1              # increment the operation count
        # If right element is less than left, merge right side
        else:
            nums[right-1] += nums[right] # merge with previous adjacent number
            right -= 1                   # move right pointer to merged element
            operations += 1              # increment the operation count
    return operations

# Example usage:
print(minOperations([4,3,2,1,2,3,1]))  # Expected output: 2
print(minOperations([1,2,3,4]))          # Expected output: 3
← Back to All Questions