Number of subarray such that every element within the subarray appears at least twice
Anonymous User
326

Saw this question in a book and need some help from the community.
The title basically explain the whole story but I will still provide some example here.

Take the example [3,3,3,4,4]
We can have the following subarray such that there are at least 2 of each item within the subarray.
[3,3],[3,3],[3,3,3],[4,4],[3,3,4,4],[3,3,3,4,4] so the solution is 6.

Note that although [3,3] appeared twice, they represent different index of begin and end so is still considered a different subarray.

Currently, I am using a O(n^3) (N^2 for selecting start and end and N for checking if it's valid) method to solve this problem. I am hoping to find a better solution or even a better Idea to improve the time complexity.

Any help or suggestion are welcomed.

Comments (2)