How can I optimize the below code for problem "Subarray with distinct Elements"? It is giving TLE

#include
#include

using namespace std;

int main()
{

int n;

cin>>n;
int arr[n];
for(int i = 0; i < n; i++)
{
    cin>>arr[i];
}
int count = 0;

for(int i=0;i<n;i++)
{
    int j=i;
    set<int>s;
    while(j<n && s.find(arr[j])==s.end())
    {

        s.insert(arr[j]);
        count+=s.size();
        j++;
    }
    s.clear();
}
cout<<count % 1000000007<<endl;

}

Hello @tanvesh you can optimise your approach using 1 dimensional dp.
In that you will store till previous index and for the present the max of previous,current +1.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.