'''
keys: 
Solutions:
Similar:
T:
S:
'''
from typing import List
import collections


class Solution:
    """
    @param ages: The ages
    @return: The answer
    """
    # use hashmap to accelerate
    def friendRequest(self, ages):
        # Write your code here
        ages_cnt = collections.defaultdict(int)
        for age in ages:
            ages_cnt[age] += 1
        
        unique_ages = list(ages_cnt.keys())
        len_unique_ages = len(unique_ages)
        results = 0
        for i in range(len_unique_ages):
            A = unique_ages[i]
            A_count = ages_cnt[A]
            if A_count > 1:
                A_to_Aself = True
                if A <= A // 2 + 7:
                    A_to_Aself = False
                if A_to_Aself:
                    results += A_count * (A_count - 1)
            for j in range(i + 1, len_unique_ages):
                B = unique_ages[j]
                A_to_B, B_to_A = True, True
                if B <= A // 2 + 7 or B > A or (B < 100 and A > 100):
                    A_to_B = False
                if A <= B // 2 + 7 or A > B or (A < 100 and B > 100):
                    B_to_A = False
                B_count = ages_cnt[B]
                if A_to_B:
                    results += A_count * B_count
                if B_to_A:
                    results += B_count * A_count
        return results
    
    # self-made
    def friendRequest1(self, ages):
        # Write your code here
        res = 0
        if not ages:
            return res
            
        for i in range(len(ages)):
            for j in range(len(ages)):
                if i != j:
                    a = ages[i]
                    b = ages[j]
                    print (a, b)
                    if b <= 0.5 * a + 7:
                        pass
                    elif b > a:
                        pass
                    elif b < 100 and a > 100:
                        pass
                    else:
                        res += 1
                
        return res