FastPrepMinimum Adjacent Merges to Form a Palindrome

Minimum Adjacent Merges to Form a Palindrome

Arista Networks logoArista Networks● MediumFULLTIMEOA
Learn

Problem statement

In one operation, choose two adjacent array elements and replace them with their sum. Return the minimum number of operations needed to make the resulting array a palindrome.

Function

minMergesToPalindrome(nums: int[]) → int

Examples

Example 1

nums = [12,14,8,13,5]return = 3

Three adjacent merges can produce [26, 26], which is a palindrome.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

More Arista Networks problems

See Arista Networks hiring insights
public int minMergesToPalindrome(int[] nums) {
    // Write your solution here.
}
nums[12,14,8,13,5]
expected3
Checking account…