// SOLVING THIS WITH AN AI ASSISTANT (2026)

If you are working through this problem with an AI coding assistant — Claude, ChatGPT, Cursor chat, Gemini, GitHub Copilot, Aider, or any agent — the goal isn’t to ask for the answer. It is to use the tool to understand the pattern. The prompt sequence I’d run:

  1. Spec it back to me first. “In your own words, what is this problem actually testing? What’s the smallest example that fails the naive approach?”
  2. Brute-force first, optimize after. “Write the simplest correct solution, even if it’s O(n²). Don’t optimize. Just make it correct, with comments explaining each step.”
  3. Ask for the upgrade. “Now show me the optimal solution. What insight makes it possible? What pattern is this an instance of?”
  4. Stress-test it. “Generate 10 edge cases — empty input, single element, duplicates, max size, sorted, reverse-sorted. Run my solution against each.”

The pattern matters more than the answer. If the agent just hands you optimized code, you’ve trained yourself to lose interviews.

You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.

Return the maximum score you can get by erasing exactly one subarray.

An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).

Example 1:

Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].

Example 2:

Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 104

Solution 1:
This can be solved in multiple ways, the easiest is to maintain a queue and verify the value read from the given nums array is unique. If not remove the element and the count from the queue and the current count respectively.

class Solution {
    public int maximumUniqueSubarray(int[] nums) {
        Queue<Integer> q = new LinkedList<>();
        int maxCount = 0;
        int count = 0;
        for(int i =0;i< nums.length;i++){
            if(!q.contains(nums[i])){
                count += nums[i];
                q.add(nums[i]);
            }else {
                
                maxCount = count>maxCount ? count: maxCount;
                while(!q.isEmpty() && q.peek() != nums[i]){
                    int del = q.remove();
                    count -= del;
                }
                q.remove();
                q.add(nums[i]);
            }
            
        }
        maxCount = count>maxCount ? count: maxCount;
        return maxCount;
    }
}

Solution 2:
The optimal way to solve this would be to use two pointer approach, which can be solved using O(n) time and O(m) space where m is the number of unique elements.

public int maximumUniqueSubarray(int[] nums) {        
 int maxScore = 0, currScore = 0;
 Set<Integer> set = new HashSet<>();

 for (int l=0, r=0; r<nums.length; r++) {
  while (!set.add(nums[r])) {
   currScore -= nums[l];
   set.remove(nums[l++]);
  }
  currScore += nums[r];
  maxScore = Math.max(maxScore, currScore);
 }

 return maxScore;
}

For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.

rajendra

Recent Posts

HackerHeap is back: building with AI agents in 2026

HackerHeap is back: a multi-platform resource for working developers building with AI coding agents. We…

10 hours ago

Largest Unique Number Java Solution

// BUILDING THIS WITH AN AI AGENT (2026)Whether you are using Claude Code, Cursor, Windsurf,…

5 years ago

Jump Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Knuth Morris Pratt Pattern Search Algorithm

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Binary Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Leetcode Integer to Roman Java Solution

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

This website uses cookies.