Interview Questions/Coding/Unique Segment Energy

Unique Segment Energy

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Unique Segment Energy

A sensor strip records packet energy values. A contiguous segment is clean only if no value appears more than once inside that segment. The energy of a clean segment is the sum of its values. You may choose any clean contiguous segment, including a segment of length one. Input Format: You are given an integer array nums. Output Format: Return the maximum energy sum over all clean contiguous segments.

Examples

Example 1
Input:
nums = [4, 2, 4, 5, 6]
Output:
17

Explanation: The best unique segment is [2, 4, 5, 6], whose sum is 17. Therefore the output is 17.

Approach hint

Keep a window with unique values.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp