โ† Paytm questions

Maximum sum of subarray with no duplicates

Filed under Paytm

Free read 1 of 3

Confirm if elements are strictly positive and whether subarrays must be contiguous.

The question

Given an array of positive integers, find the maximum sum of a contiguous subarray that contains no duplicate elements.

Constraints

  • 1 <= array length <= 10^5
  • 1 <= array elements <= 10^4

Examples

Input
arr = [1, 2, 3, 1, 4]
Output
10
Why
The subarray [2, 3, 1, 4] has no duplicates and sums to 10.

Approach

  1. Confirm if elements are strictly positive and whether subarrays must be contiguous.

Solution

O(N) time, O(N) space

Python
class Solution:
    def maximumUniqueSubarraySum(self, nums: list[int]) -> int:
        seen = {}
        left = 0
        max_sum = 0
        curr_sum = 0
        for right, num in enumerate(nums):
            if num in seen and seen[num] >= left:
                while left <= seen[num]:
                    curr_sum -= nums[left]
                    left += 1
            seen[num] = right
            curr_sum += num
            max_sum = max(max_sum, curr_sum)
        return max_sum

Tips

  • What they are testing: Test with all unique elements, all identical elements, and subarrays where duplicates appear multiple times.
  • Common mistake: Restarting the sliding window completely from the duplicate index rather than adjusting the left pointer past the previous occurrence.

Related Paytm questions

Collected from public candidate posts and coding forums. Updated Nov 2026.