🛩 Beginner-Friendly Guide 'Trionic Array II' - Problem 3640 (C++, Python, JavaScript)

Published: (February 4, 2026 at 06:42 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

Problem Overview

We are given an array of integers nums (length n).
A trionic subarray is a contiguous segment that follows a strict three‑part pattern:

  1. Strictly increasing – from the start up to a peak p.
  2. Strictly decreasing – from p down to a trough q.
  3. Strictly increasing – from q to the end of the segment.

The indices must satisfy l ≤ p < q < r for the segment [l, r]. The goal is to find the maximum possible sum of a trionic subarray.


C++ Implementation

class Solution {
    long long maxSumTrionic(vector<int>& nums) {
        const long long INF_NEG = static_cast(-2e18);
        int n = nums.size();
        int i = 0;
        long long ans = INF_NEG;

        while (i (nums[p - 1]) + nums[p];

            // 2️⃣ strictly decreasing part
            while (i  nums[i]) {
                cur += nums[i];
                ++i;
            }
            if (i == p + 1 || i == n || nums[i - 1] == nums[i]) continue;

            int q = i - 1;                         // trough index

            // 3️⃣ second increasing part (must contain at least one element)
            cur += nums[i];
            ++i;

            long long maxSuffix = 0, tmpSuffix = 0;
            while (i = lBound; --j) {
                tmpPrefix += nums[j];
                maxPrefix = max(maxPrefix, tmpPrefix);
            }
            cur += maxPrefix;                      // best left extension

            ans = max(ans, cur);
            i = q;                                 // restart from trough
        }
        return ans;
    }
};

Python Implementation

class Solution:
    def maxSumTrionic(self, nums: list[int]) -> int:
        n = len(nums)
        i = 0
        ans = float("-inf")

        while i  nums[i]:
                cur += nums[i]
                i += 1

            if i == p + 1 or i == n or nums[i - 1] == nums[i]:
                continue                  # invalid trough
            q = i - 1                     # trough

            # 3️⃣ second increasing segment (must have at least one element)
            cur += nums[i]
            i += 1
            max_suffix = tmp_suffix = 0
            while i  nums[i]) {
            cur += BigInt(nums[i]);
            ++i;
        }
        if (i === p + 1 || i === n || nums[i - 1] === nums[i]) continue;
        const q = i - 1;                       // trough

        // 3️⃣ second increasing part (must contain at least one element)
        cur += BigInt(nums[i]);
        ++i;

        let maxSuffix = 0n, tmpSuffix = 0n;
        while (i  maxSuffix) maxSuffix = tmpSuffix;
        }
        cur += maxSuffix;                      // best right extension

        // 4️⃣ best left extension
        let maxPrefix = 0n, tmpPrefix = 0n;
        for (let j = p - 2; j >= lBound; --j) {
            tmpPrefix += BigInt(nums[j]);
            if (tmpPrefix > maxPrefix) maxPrefix = tmpPrefix;
        }
        cur += maxPrefix;                      // best left extension

        if (cur

JavaScript Implementation

// Second Increasing
currentSum += BigInt(nums[i]);
i += 1;
let maxSuffix = 0n,
    tempSuffix = 0n;

while (i  maxSuffix) maxSuffix = tempSuffix;
}
currentSum += maxSuffix;

// 4. Backward Prefix
let maxPrefix = 0n,
    tempPrefix = 0n;

for (let j = p - 2; j >= lBound; j--) {
    tempPrefix += BigInt(nums[j]);
    if (tempPrefix > maxPrefix) maxPrefix = tempPrefix;
}
currentSum += maxPrefix;

if (ans === -Infinity || currentSum > ans) {
    ans = currentSum;
}
i = q;
return Number(ans);
};

Key Concepts

  • Two‑Pointer / Sliding Window – Locate the specific “mountain‑valley‑mountain” shape in linear time.
  • Greedy Sub‑problems – Use max(0, temp_sum) to decide whether extending a segment improves the sum (a Kadane‑style idea).
  • Linear Complexity – Although a backward scan is performed for the left extension, the main index only moves forward, guaranteeing O(n) time.

Context

Problems like “Trionic Array” are classic pattern‑matching questions often seen in technical interviews (e.g., Google, Amazon). They model scenarios such as signal‑processing or financial‑trend analysis where a system must detect a specific “peak‑valley‑peak” pattern before it completes. Mastering the ability to traverse an array while tracking multiple state changes is a vital skill for any software engineer.

Back to Blog

Related posts

Read more »