What's wrong with this code .....not working for some test cases
hi @Narasimha
you dont have to subtract the duplicates otherwise code is working fine
//find the sum of all subarrays lengths with unique elements
import java.util.*;
class Kommi
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
int n=obj.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
int maincount=0;
HashMap<Integer,Integer> h2=new HashMap<Integer,Integer>();
int duplicates=0;
for(int i=0;i<a.length;i++)
{
if(h2.containsKey(a[i]))
{
duplicates=duplicates+1;
}
else
{
h2.put(a[i],1);
}
}
for(int i=0;i<a.length;i++)
{
int count=0;
int s=1;
HashMap<Integer,Integer> h1=new HashMap<Integer,Integer>();
for(int j=i;j<a.length;j++)
{
if(h1.containsKey(a[j]))
{
break;
}
else
{
count=count+s;
h1.put(a[j],s);
s++;
}
}
maincount=maincount+count;
}
System.out.println((maincount));
}
}
if you still have doubt you can ask 
but we need to subtract the duplicate sub arrays length right ?
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.
