algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
3 shown · 0/261 solved

Minimum Merges to Palindrome

SilverCommunity Beta
Asked atArista Networks
Solve problem →
2000ms256MBAdded Aug 24, 2026

You are given an integer array nums. In one operation you may pick two adjacent elements and replace them with their sum (shrinking the array by one).

Return the minimum number of operations needed to turn nums into a palindrome, an array that reads the same forwards and backwards. A single element is a palindrome.

Examples

Example 1

Input: nums = [12,14,8,13,5]
Output: 3
Explanation: Merge 12+14 → [26,8,13,5], merge 13+5 → [26,8,18], merge 8+18 → [26,26]. Three operations produce a palindrome; fewer is impossible.

Example 2

Input: nums = [1,2,3]
Output: 1
Explanation: Merge 1+2 → [3,3].

Constraints

  • 1≤1 \leq1≤ nums.length ≤105\leq 10^5≤105
  • 1≤1 \leq1≤ nums[i] ≤109\leq 10^9≤109

Details

Solved by4 people
Time limit2000ms
Memory256MB
AddedAug 24, 20262 weeks ago