https://www.geeksforgeeks.org/maximum-sum-subarray-such-that-start-and-end-values-are-same/
Given an array of N positive numbers, the task is to find a contiguous subarray (L-R) such that a[L]=a[R] and sum of a[L] + a[L+1] +…+ a[R] is maximum.
Examples:
Input: arr[] = {1, 3, 2, 2, 3} Output: 10 Subarray [3, 2, 2, 3] starts and ends with 3 and has sum = 10
Approach: For every element in the array, let’s find 2 values: First (Leftmost) occurrence in the array and Last (Rightmost) occurrence in the array. Since all numbers are positive, increasing the number of terms can only increase the sum. Hence for every number in the array, we find the sum between it’s leftmost and rightmost occurrence, which can be done quickly using prefix sums. We can keep track of the maximum value found so far and print it in the end.