Skip to main content

Palindromic Subarray Sum - Solution & Explanation

Practice this problem

Problem Statement

You are given an integer array nums.

Return the maximum possible sum of a subarray of nums that is a palindrome.

 

Example 1:

Input: nums = [10,10]

Output: 20

Explanation:

The whole array [10,10] is a palindrome. Therefore, the maximum sum is 10 + 10 = 20.

Example 2:

Input: nums = [1,2,3,2,1,5,6]

Output: 9

Explanation:

The contiguous subarray [1,2,3,2,1] is a palindrome. Its sum is 1 + 2 + 3 + 2 + 1 = 9 and it is the maximum sum.

Example 3:

Input: nums = [7,1,2,1,7,3,4,3,4]

Output: 18

Explanation:

The contiguous subarray [7,1,2,1,7] is a palindrome. Its sum is 7 + 1 + 2 + 1 + 7 = 18 and it is the maximum sum.

Example 4:

Input: nums = [1,2,3,4,5]

Output: 5

Explanation:

No subarray with length greater than 1 is a palindrome. The largest element in the array is 5. Therefore, the answer is 5.

Example 5:

Input: nums = [1000]

Output: 1000

Explanation:

The subarray with only one element is a palindrome. Therefore, the answer is 1000.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 10​​​​​​​9

Approach Overview

Problem Overview: You need to find subarrays whose total sum is a palindrome number. The challenge is avoiding repeated subarray scans and expensive palindrome checks across all possible ranges.

Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)

Iterate over every possible subarray using two nested loops, then compute the subarray sum with another loop. After calculating the sum, convert the value into a string or reverse the digits numerically to verify whether it is palindromic. This approach is useful for validating small test cases and building intuition, but it becomes unusable once the array size grows beyond a few thousand elements.

Approach 2: Prefix Sum + Direct Palindrome Check (O(n^2 * log M) time, O(n) space)

Build a prefix sum array so each subarray sum can be computed in constant time using prefix[r + 1] - prefix[l]. This removes the inner summation loop and reduces the complexity significantly. The remaining bottleneck is checking whether every generated sum is a palindrome. Numeric reversal works better than string conversion because it avoids extra allocations and reduces constant overhead.

Approach 3: Prefix Sum + Precomputed Palindrome Values (O(n * P) time, O(n) space)

The optimal solution generates all palindrome numbers within the possible subarray sum range ahead of time. While iterating through the array, maintain cumulative sums and use a hash map to count previously seen prefix sums. For every palindrome value p, check whether currentPrefix - p exists in the map. This transforms the problem into a frequency lookup problem and avoids checking palindrome properties repeatedly. The approach combines array traversal, prefix sums, and hash-based counting efficiently.

Recommended for interviews: Interviewers usually expect you to begin with brute force to demonstrate correctness, then optimize using prefix sums. The strongest solution precomputes valid palindrome sums and uses hash lookups for constant-time frequency checks. That progression shows both problem-solving depth and practical optimization skills.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n^3)O(1)Small arrays or debugging correctness
Prefix Sum + Palindrome CheckO(n^2 * log M)O(n)Medium constraints where direct checking is acceptable
Prefix Sum + Precomputed PalindromesO(n * P)O(n)General optimal solution for large inputs

Video Solution

Q3. Divisible Game, Q4. Palindromic Subarray Sum || Easy Solution || Leetcode Weekly 509 || Watch2XšŸš€ • Rajan Keshari ( CSE - IIT Dhanbad ) • 774 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Palindromic Subarray Sum easy or hard?
Palindromic Subarray Sum is generally classified as a hard problem because brute force passes only very small inputs. The difficulty comes from combining prefix sums, palindrome generation, and efficient counting into one optimized solution. Candidates who know hash-based prefix techniques usually solve it much faster.
Palindromic Subarray Sum Python/Java solution
Python solutions typically use dictionaries for prefix frequency counting and helper functions for palindrome generation. Java implementations use HashMap<Integer, Integer> with iterative palindrome checks or precomputed lists. Both languages support the same optimized prefix-sum strategy with O(n * P) complexity.
How to solve Palindromic Subarray Sum in O(n)?
A strict O(n) solution is usually not possible because you still need to consider valid palindrome sums. The closest optimized approach precomputes all palindrome numbers in range and performs O(1) hash lookups while scanning prefix sums. In practice, this behaves close to linear for typical constraints.
What is the best approach for Palindromic Subarray Sum?
The best approach uses prefix sums, precomputed palindrome values, and a hash map for frequency counting. Instead of checking every subarray individually, you transform the problem into finding matching prefix sums. This reduces repeated palindrome validation and gives near-linear performance depending on the number of palindrome values.
Is Palindromic Subarray Sum asked at Google/Amazon/Meta?
Prefix sum and hash-map problems with palindrome constraints appear frequently in interviews at companies such as Google, Amazon, and Meta. Variants often test optimization skills, range queries, and efficient counting techniques. Interviewers usually care more about the progression from brute force to optimized counting.
What data structure is used in Palindromic Subarray Sum?
The main data structures are prefix sum arrays and hash maps. Prefix sums allow constant-time subarray sum computation, while the hash map stores frequencies of previously seen cumulative sums. Some implementations also use sets or vectors to store generated palindrome numbers.
What is the time complexity of Palindromic Subarray Sum?
The brute force solution runs in O(n^3) time because every subarray and its sum are recomputed repeatedly. Using prefix sums improves this to O(n^2 * log M), where M is the maximum possible subarray sum. The optimized hash-map approach runs in O(n * P), where P is the number of generated palindrome values.

Ready to solve this problem?

Practice Palindromic Subarray Sum with our built-in code editor and test cases.

Practice on FleetCode