package leetcode;

/**
 * https://leetcode.com/problems/friends-of-appropriate-ages/
 */
public class Problem825 {
    public int numFriendRequests(int[] ages) {
        int answer = 0;
        for (int i = 0; i < ages.length; i++) {
            for (int j = i + 1; j < ages.length; j++) {
                if (accept(ages[i], ages[j])) {
                    answer++;
                }
                if (accept(ages[j], ages[i])) {
                    answer++;
                }
            }
        }
        return answer;
    }

    private static boolean accept(int a, int b) {
        return (!((a <= 0.5 * b + 7) || (a > b) || (a > 100 && b < 100)));
    }

    public static void main(String[] args) {
        Problem825 prob = new Problem825();
        System.out.println(prob.numFriendRequests(new int[]{16, 16})); // 2
        System.out.println(prob.numFriendRequests(new int[]{16, 17, 18})); // 2
        System.out.println(prob.numFriendRequests(new int[]{20, 30, 100, 110, 120})); // 3
    }
}

@@ -179,16 +179,143 @@
 er = 0;%0A
+        int%5B%5D counts = new int%5B121%5D; // there are 120 ages%0A        for (int age : ages) %7B%0A            counts%5Bage%5D++;%0A        %7D%0A
         
@@ -331,18 +331,20 @@
 i = 
-0
+1
 ; i %3C 
-age
+count
 s.le
@@ -384,20 +384,16 @@
  j =
- i +
  1; j %3C 
 ages
@@ -388,19 +388,21 @@
  1; j %3C 
-age
+count
 s.length
@@ -434,31 +434,77 @@
 if (
-accept(ages%5Bi%5D, ages%5Bj%5D
+counts%5Bi%5D %3E 0 && counts%5Bj%5D %3E 0) %7B%0A                    if (reject(i, j
 )) %7B
@@ -520,34 +520,42 @@
             
+   
-answer++;%0A
+ continue;%0A    
             
@@ -580,69 +580,145 @@
     
-if (accept(ages%5Bj%5D, ages%5Bi%5D)) %7B%0A                    answer++;
+    answer += counts%5Bi%5D * counts%5Bj%5D;%0A                    if (i == j) %7B%0A                        answer -= counts%5Bi%5D;%0A                    %7D
 %0A   
@@ -817,13 +817,13 @@
 ean 
-accep
+rejec
 t(in
@@ -855,10 +855,8 @@
 urn 
-(!
 ((a 
@@ -910,353 +910,9 @@
 00))
-);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem825 prob = new Problem825();%0A        System.out.println(prob.numFriendRequests(new int%5B%5D%7B16, 16%7D)); // 2%0A        System.out.println(prob.numFriendRequests(new int%5B%5D%7B16, 17, 18%7D)); // 2%0A        System.out.println(prob.numFriendRequests(new int%5B%5D%7B20, 30, 100, 110, 120%7D)); // 3
+;
 %0A   

Solve problem 825