I am facing TLE while I am submitting my code.
I had used recursion.
Kindly have a look.
I also tried to code using the given hints but then I am facing WRONG ANSWERS error.
Time Limit Error
import java.util.*;
import java.util.HashMap;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int [n];
for(int i = 0 ; i< n ; i++)
{
arr[i] = sc.nextInt();
}
System.out.println(SubarraysDE(arr));
}
public static int SubarraysDE( int[] arr )
{
int j = 0 ;
int ans = 0;
HashSet< Integer > map = new HashSet<>();
for(int i = 0 ; i<arr.length ; i++)
{
while( j < arr.length && map.contains(arr[j]) == false )
{
map.add(arr[j]);
j++;
}
ans = ans + (map.size()*(map.size()+1)/2);
map.remove(arr[i]);
}
return ans;
}
}
hope this will help u
pls rate my work