Array Reflection Height Maximization — Problem Statement & Solution Guide

ArraysMediumPattern recognition and array traversal
TimeO(n log n)
|
SpaceO(n)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Arrays and solve the Array Reflection Height Maximization problem optimally.

TopicArrays
PatternPattern recognition and array traversal
TimeO(n log n)
SpaceO(n)

Problem Description

You are given an integer array reflections. A strictly increasing subsequence is a sequence obtained by selecting some (possibly none) elements from reflections in their original order such that each chosen element is larger than the one before it. Your task is to determine the maximum possible length of such a subsequence and output that length as the height metric. The solution must run efficiently for large inputs.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Array Reflection Height Maximization"

medium

WHY DOES IT MATTER?

The LIS pattern appears in scheduling, version control merging, and stock‑price analysis where you need the longest chain of improving metrics; mastering it sharpens a candidate’s ability to convert quadratic DP into logarithmic solutions.

OPTIMIZATION CHALLENGE

The key insight is that only the smallest possible tail for each length matters; any larger tail can never lead to a longer subsequence, so we can discard it and use binary search to maintain these minima in O(log n) per element.

REAL-WORLD CONNECTION

Think of a production pipeline where each stage must handle a higher load than the previous one. Keeping the smallest possible maximum load for each pipeline length mirrors the tails array, allowing the system to accommodate larger future loads efficiently.

During an interview, write the binary‑search helper first, test it on a few numbers, then iterate through the array updating tails. If you get stuck, remember you only need the length, not the actual sequence.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log n)
💾 Space:O(n)

Core Theory — Why This Approach?

The longest strictly increasing subsequence (LIS) problem asks for the maximum length of a subsequence where each element is larger than its predecessor while preserving original order. A naïve solution enumerates all subsets, leading to exponential time (O(2^n)) and quickly becomes infeasible for n>30. The optimal paradigm leverages dynamic programming combined with binary search: we maintain an auxiliary array tails where tails[i] stores the smallest possible tail value of an increasing subsequence of length i+1 seen so far. For each element we binary‑search tails to find its position, replace the existing value, and possibly extend the array. This yields O(n log n) time and O(n) space, which is optimal for the classic LIS problem.

Interview Questions on This Problem

Q1How would you compute the length of the longest strictly increasing subsequence in O(n log n) time?

Maintain a tails array; for each number, binary‑search the first element in tails that is >= the number, replace it, and if the number is larger than all tails, append it. The final size of tails is the LIS length.

Q2Why does the binary‑search based LIS algorithm produce the correct length even though it does not construct the actual subsequence?

tails[i] always holds the minimal possible tail for any increasing subsequence of length i+1. Keeping tails minimal ensures future elements have the best chance to extend longer subsequences, guaranteeing that the length of tails equals the optimal LIS length.

Q3If the input array can contain duplicate values, how would you modify the LIS algorithm to enforce a strictly increasing subsequence?

When searching in tails, use lower_bound (first element >= x) for non‑decreasing LIS, but for strictly increasing you need lower_bound on values >= x and then replace; alternatively use upper_bound (first element > x) to skip equal values, ensuring duplicates are not placed in the same increasing chain.

Examples

Example 1

Input

[3, 1, 4, 2, 5]

Output

4

Explanation: One optimal subsequence is 1 → 2 → 4 → 5, which has length 4. No longer strictly increasing subsequence exists.

Example 2

Input

[9, 8, 7, 6]

Output

1

Explanation: All numbers decrease, so the longest strictly increasing subsequence can contain only a single element, giving length 1.

Example 3

Input

[10, 22, 9, 33, 21, 50, 41, 60]

Output

5

Explanation: A longest increasing subsequence is 10 → 22 → 33 → 50 → 60, yielding length 5. Other subsequences of length 5 also exist, but none longer.

Constraints

  • 1 <= reflections.length <= 100000
  • -1000000000 <= reflections[i] <= 1000000000
  • All calculations must fit in 64‑bit signed integer range

Optimal Approach & Strategy

Use a tails array with binary search to maintain minimal possible ends for each length, achieving O(n log n) time.

Brute Force Approach

Generate every subset, keep those that are strictly increasing, and track the longest length – exponential time.

Code Solutions

JavaScript Solution
Time: O(n log n)
function maxReflectionHeight(reflections) {
    const dp = [];
    for (const x of reflections) {
        let l = 0, r = dp.length;
        while (l < r) {
            const m = (l + r) >> 1;
            if (dp[m] < x) l = m + 1; else r = m;
        }
        if (l === dp.length) dp.push(x);
        else dp[l] = x;
    }
    return dp.length;
}

function main() {
    const fs = require('fs');
    const data = fs.readFileSync(0, 'utf8').trim().split(/\s+/).map(Number);
    if (data.length === 0) return;
    const n = data[0];
    const arr = data.slice(1, 1 + n);
    console.log(maxReflectionHeight(arr));
}

main();

Asked in Top Tech Interviews

PhonePe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.