You are viewing a single comment's thread. Return to all comments →
Second test failed ((((
public class NewYearPresent { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for(int arr_i=0; arr_i < n; arr_i++){ arr[arr_i] = in.nextInt(); } System.out.println(calculate(n, arr)); } private static long calCombination(int n, int k){ long res = 1; for (int i = n; i >= (n - k + 1); i--) res *= i; for (int i = 1; i <= k; i++) { res /= i; } return res; } private static long calculate(int n, int[] arr){ int SIZE = 10000001; int[] buckets = new int[SIZE]; for(int i = 0;i < n; i++) { buckets[arr[i]]++; } long count = 0; for(int i=2; i< SIZE; i++) { int n2 = buckets[i]; int sum = i; if (n2 >= 2) { long m = calCombination(n2, 2); int foundTwo = 1; boolean foundFirst = false; boolean foundSecond = false; int start = 1; while (foundTwo < 3) { for (int j = start; j < n2; j++) { int v1 = buckets[i]; if (v1 == 0 || v1 >= n2) continue; for (int k = j + 1; k < n2; k++) { int v2 = buckets[k]; if (v2 == 0 || Math.abs(v1 + v2 - sum) > 0) continue; if (foundTwo == 1) { foundFirst = true; start = j; } else if (foundTwo == 2) foundSecond = true; } } foundTwo++; } if(foundFirst && foundSecond) count += m; } if(n2>=3) { long m = calCombination(n2, 3); for (int j = 1; j < n2; j++) { int v1 = buckets[i]; if (v1 == 0 || v1>= n2) continue; for (int k = j+1; k < n2; k++) { int v2 = buckets[k]; if (v2 == 0 || (v1+v2) >= n2) continue; for (int l = k + 1; l < n2; l++) { int v3 = buckets[l]; if (buckets[l] == 0 || Math.abs(v1+v2+v3-sum) > 0) continue; count += m; } } } } } return count; } }
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Present
You are viewing a single comment's thread. Return to all comments →
Second test failed ((((