Can you tell me the mistake in code

import java.util.HashMap;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int[] arr = new int[n + 1];
	long sum = 0;
	HashMap<Integer, Integer> hp = new HashMap<>();
	for (int i = 1; i <= n; i++) {
		int item = scn.nextInt();
		arr[i] = arr[i - 1] + i;
		if (hp.containsKey(item))
			arr[i] -= hp.get(item);
		hp.put(item, i);
		sum += arr[i];
	}
	float ans = (float) (2 * sum - n) / (n * n);
	System.out.println(ans);
}

}

can u pls give ur code in c++