Not getting correct output

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int[] arr = new int[num];
for(int i = 0; i<arr.length; i++) {
arr[i] = sc.nextInt();
}
subarr(arr);
}

public static void subarr(int[] arr) {
	Set<Integer> s = new HashSet<Integer>();
	int j = 0;
	int ans = 0;
	for(int i = 0; i<arr.length; i++) {
		while(j<arr.length && !s.contains(arr[i])) {
			s.add(arr[j]);
			j++;
		}
		ans+=((j-i+1)*(j-i+2))/2;
		s.remove(arr[i]);
	}
	System.out.println(ans);
}

@prateekad99
In your while loop check if it contains arr[j] not arr[i]

use long instead of int for the constraints are bigger

you also have to take mod 10^9+7 of your answer. In case the answer is 10^9+8. Your code will give answer as 10^9+8 but the correct answer will be 1.

refer to


please dont share raw code and instead use ide.codingblocks.com to do the same