BackmediumArraysSalesforce

Galactic Treasure Hunt Solution

Problem Statement

You are navigating a linear sequence of N star systems, indexed from 0 to N-1. Each system i contains a cache of energy crystals with a value of A[i]. Due to gravitational constraints, your spacecraft can only traverse the systems in increasing index order. You may choose to skip any number of systems, but once you land on a system, you must collect all crystals present there. Your objective is to determine the maximum total energy value achievable by selecting a valid subsequence of systems. Note that a subsequence maintains the relative order of elements but does not require them to be contiguous. If all crystal values are negative, you may choose to visit no systems, resulting in a total value of 0.

Example 1
Input
A = [3, -1, 4, -1, 5]
Output
12

Explanation: The optimal subsequence is [3, 4, 5]. The sum is 3 + 4 + 5 = 12. Skipping the negative values -1 and -1 maximizes the total.

Example 2
Input
A = [-2, -3, -1, -4]
Output
0

Explanation: All values are negative. The optimal strategy is to select no systems, yielding a sum of 0.

Example 3
Input
A = [1, 2, 3, 4, 5]
Output
15

Explanation: All values are positive. The optimal subsequence includes all elements: 1 + 2 + 3 + 4 + 5 = 15.

Example 4
Input
A = [5, -10, 5, -10, 5]
Output
15

Explanation: The optimal subsequence is [5, 5, 5] (indices 0, 2, 4). The sum is 5 + 5 + 5 = 15. Skipping the -10s is beneficial.

Constraints

  • 1 <= A.length <= 10^5
  • -10^9 <= A[i] <= 10^9
  • The answer is guaranteed to fit in a 64-bit integer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Galactic Treasure Hunt — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(N^2)
|
SpaceO(N)

Problem Description

You are navigating a linear sequence of N star systems, indexed from 0 to N-1. Each system i contains a cache of energy crystals with a value of A[i]. Due to gravitational constraints, your spacecraft can only traverse the systems in increasing index order. You may choose to skip any number of systems, but once you land on a system, you must collect all crystals present there. Your objective is to determine the maximum total energy value achievable by selecting a valid subsequence of systems. Note that a subsequence maintains the relative order of elements but does not require them to be contiguous. If all crystal values are negative, you may choose to visit no systems, resulting in a total value of 0.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Galactic Treasure Hunt"

medium

WHY DOES IT MATTER?

The Longest Increasing Subsequence (LIS) pattern is essential because it models scenarios where order matters and you need to select a subset of elements that satisfy a monotonic constraint while optimizing a cumulative metric. It is a fundamental building block for more complex problems involving dynamic programming and data structures like Fenwick Trees.

OPTIMIZATION CHALLENGE

The key insight is to use dynamic programming to avoid recalculating the optimal value for each subsequence. By defining dp[i] as the maximum sum of an increasing subsequence ending at index i, you can compute it by looking at all previous indices j < i where A[j] < A[i]. This reduces the time complexity from O(2^N) to O(N^2). Further optimization using a Fenwick Tree or Segment Tree can reduce it to O(N log N).

REAL-WORLD CONNECTION

In distributed systems, this pattern is analogous to finding the optimal path for data replication across nodes where each node has a latency cost, and you can only replicate to nodes with higher latency thresholds. It also appears in financial trading algorithms where you seek the maximum profit from a sequence of trades with increasing prices.

In an interview, start by explaining the O(N^2) DP solution clearly, as it is easier to implement and debug. Then, mention the O(N log N) optimization using a Fenwick Tree as a follow-up, showing that you understand advanced data structures and can scale your solution. Always clarify the constraints (e.g., strict vs. non-strict increasing) before coding.

COMPLEXITY AT A GLANCE

⏱ Time:O(N^2)
💾 Space:O(N)

Core Theory — Why This Approach?

The problem 'Galactic Treasure Hunt' is a classic instance of the Longest Increasing Subsequence (LIS) variant, specifically optimized for finding the maximum sum of a strictly increasing subsequence. The naive approach involves checking all possible subsequences, which results in an exponential time complexity of O(2^N), making it infeasible for large N. This brute-force method fails because it does not exploit the overlapping subproblems inherent in the sequence structure, leading to redundant calculations of the same optimal values for suffixes of the array.

Interview Questions on This Problem

Q1At a fintech platform, we need to find the maximum profit from a sequence of stock prices where you can only buy and sell once, but the price must strictly increase over time. How would you adapt the LIS sum algorithm to this scenario?

You would treat the price differences as the values in the array. Instead of summing the prices directly, you sum the positive differences between consecutive valid points in the increasing subsequence. The DP state dp[i] represents the maximum profit achievable ending at index i. The transition is dp[i] = max(dp[j] + (A[i] - A[j]), A[i] - A[j]) for all j < i where A[j] < A[i]. This ensures you capture the cumulative gain from the optimal starting point to the current point.

Q2In a high-growth startup, we have a log of user engagement scores over time. We want to find the longest period where engagement strictly increased, and report the total engagement gain during that period. How does the space complexity of the DP solution change if we only need the final maximum value and not the subsequence itself?

If you only need the final maximum value, you can optimize the space complexity from O(N) to O(1) by maintaining a single variable for the global maximum and iterating through the array. However, since the transition dp[i] depends on all previous dp[j] values, you cannot simply use a sliding window. You must either keep the full dp array or use a Fenwick Tree/Segment Tree to query the maximum dp[j] for all j < i with A[j] < A[i] in O(log N) time, reducing space to O(N) for the tree but allowing for more efficient updates if the array is dynamic.

Q3At a global product company, we are processing a stream of sensor data. We need to find the maximum sum of a strictly increasing subsequence in real-time. How would you handle the case where the input is too large to fit in memory?

For streaming data that doesn't fit in memory, you can use an external sorting or partitioning strategy. However, for LIS, a common approach is to use a divide-and-conquer strategy or process the data in chunks. If the data is static but large, you can use a disk-based database to store the dp values and query them. Alternatively, if the values are bounded, you can use a Fenwick Tree to maintain the maximum dp value for each possible value of A[i], allowing you to process the stream in O(N log M) time where M is the range of values, and O(M) space.

Examples

Example 1

Input

A = [3, -1, 4, -1, 5]

Output

12

Explanation: The optimal subsequence is [3, 4, 5]. The sum is 3 + 4 + 5 = 12. Skipping the negative values -1 and -1 maximizes the total.

Example 2

Input

A = [-2, -3, -1, -4]

Output

0

Explanation: All values are negative. The optimal strategy is to select no systems, yielding a sum of 0.

Example 3

Input

A = [1, 2, 3, 4, 5]

Output

15

Explanation: All values are positive. The optimal subsequence includes all elements: 1 + 2 + 3 + 4 + 5 = 15.

Example 4

Input

A = [5, -10, 5, -10, 5]

Output

15

Explanation: The optimal subsequence is [5, 5, 5] (indices 0, 2, 4). The sum is 5 + 5 + 5 = 15. Skipping the -10s is beneficial.

Constraints

  • 1 <= A.length <= 10^5
  • -10^9 <= A[i] <= 10^9
  • The answer is guaranteed to fit in a 64-bit integer.

Optimal Approach & Strategy

Use dynamic programming where dp[i] stores the maximum sum of an increasing subsequence ending at index i. For each i, update dp[i] by considering all previous indices j < i where A[j] < A[i], and take the maximum of dp[j] + A[i]. The final answer is the maximum value in the dp array.

Brute Force Approach

Generate all possible subsequences of the array and check if each one is strictly increasing. For each valid subsequence, calculate its sum and keep track of the maximum sum encountered.

Verified Code Solutions

JavaScript Solution
Time: O(N^2)
// Returns the maximum sum obtainable by selecting any subset of elements
// while preserving the original order (i.e., a subsequence).
function maxTreasure(arr) {
    if (arr.length === 0) return 0;
    let sumPos = 0;
    let maxElem = -Infinity;
    for (const v of arr) {
        if (v > 0) sumPos += v;
        if (v > maxElem) maxElem = v;
    }
    return (sumPos > 0) ? sumPos : maxElem;
}

// Read input
const fs = require('fs');
const input = fs.readFileSync(0,'utf8').trim().split(/\s+/).map(Number);
let idx = 0;
const N = input[idx++]||0;
const arr = input.slice(idx, idx+N);
console.log(maxTreasure(arr));

Asked in Top Tech Interviews

Salesforce

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.