#C8095. Maximum Distinct Subarray Sum
Maximum Distinct Subarray Sum
Maximum Distinct Subarray Sum
Given an array of integers, your task is to find the maximum sum of a contiguous subarray where all elements are distinct. A subarray is a contiguous segment of the array. In this problem, you need to consider only those subarrays in which no element appears more than once.
Formally, for an array \(A = [a_1, a_2, \dots, a_n]\), find indices \(i\) and \(j\) with \(1 \le i \le j \le n\) such that all elements in \(\{ a_i, a_{i+1}, \dots, a_j \}\) are distinct and the sum \(S = \sum_{k=i}^{j} a_k\) is maximized. That is, find \(\max S \).
It is recommended to use a sliding window technique to solve this problem efficiently.
inputFormat
The first line contains an integer (T), the number of test cases. For each test case, the first line contains an integer (N) representing the number of elements, and the second line contains (N) space-separated integers.
outputFormat
For each test case, output a single line containing the maximum sum of a contiguous subarray with all distinct elements.## sample
4
5
1 2 2 3 4
4
-1 2 -1 3
3
1 1 1
6
1 2 3 4 5 6
9
4
1
21
</p>