DDSA Solutions

3637. Trionic Array I

Time: O(n)
Space: O(1)
Advertisement

Approach

Check if prefix sums can be split at two points with equal thirds.

Key Techniques

Array

Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.

Simulation

Simulation problems require implementing the described process step by step. Focus on correctly handling edge cases and state transitions. Common in geometry, game problems, and string manipulation. Optimize only if the naive simulation exceeds the time limit.

3637.cs
C#
// Approach: Check if prefix sums can be split at two points with equal thirds.
// Time: O(n) Space: O(1)

public class Solution
{
    public bool IsTrionic(int[] nums)
    {
        int arrayLength = nums.Length;
        int currentIndex = 0;

        // Phase 1: Find the first strictly increasing segment
        // Move forward while elements are strictly increasing
        while (currentIndex < arrayLength - 2 && nums[currentIndex] < nums[currentIndex + 1])
            currentIndex++;

        // If no increasing segment found at the beginning, pattern is invalid
        if (currentIndex == 0)
            return false;

        // Phase 2: Find the strictly decreasing segment
        // Save the peak position where decreasing starts
        int peakIndex = currentIndex;

        // Move forward while elements are strictly decreasing
        while (currentIndex < arrayLength - 1 && nums[currentIndex] > nums[currentIndex + 1])
            currentIndex++;

        // Check if decreasing segment exists and doesn't reach the end
        // (we need room for the final increasing segment)
        if (currentIndex == peakIndex || currentIndex == arrayLength - 1)
            return false;

        // Phase 3: Find the final strictly increasing segment
        // Move forward while elements are strictly increasing
        while (currentIndex < arrayLength - 1 && nums[currentIndex] < nums[currentIndex + 1])
            currentIndex++;

        // Pattern is valid only if we've reached the end of the array
        return currentIndex == arrayLength - 1;
    }
}
Advertisement
Was this solution helpful?