Problem: We have N balls. The i-th ball has an integer A_i written on it.   For each k=1, 2, ..., N, solve the following problem and print the answer.    * Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Submission attempts (in order):
import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
A = list(map(int, input().split()))






np_A = np.array(A)


dic = {}



tmp_all = np.array(A)
unique = np.unique(tmp_all)
print("unique:", unique)
ans_all = 0
for u in unique:
    count_u = np.count_nonzero(tmp_all==u)
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)



for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)

    k_unique = np.unique(tmp_k)
    ans = 0

    k_num = A[k]
    count_u = np.count_nonzero(tmp_k==k_num)
    if count_u < 2:
        ans_k = 1
    else:
        ans_k = int(count_u*(count_u-1)/2)
    
    print(ans_all-ans_k)


import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
A = list(map(int, input().split()))






np_A = np.array(A)


dic = {}



tmp_all = np.array(A)
unique = np.unique(tmp_all)
print("unique:", unique)
ans_all = 0
for u in unique:
    count_u = np.count_nonzero(tmp_all==u)
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)



for k in range(N):
    k_num = A[k]
    count_u = np.count_nonzero(tmp_all==k_num)    
    print(ans_all-count_u+1)


import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


N = int(input())
A = list(map(int, input().split()))






np_A = np.array(A)


dic = {}

for k in range(N):
    before = A[:k]
    after = A[k+1:]

    before.extend(after)
    tmp_k = np.array(before)
    
    

    
    

    k_unique = np.unique(tmp_k)
    ans = 0

    
    
    maped_num = map(str, k_unique) 
    str_unique = "".join(maped_num.tolist())
    

    
    count_u_list = []
    for u in k_unique:
        count_u = np.count_nonzero(tmp_k==u)
        count_u_list.append(count_u)
    
    maped_num = map(str, count_u_list) 
    str_count = "".join(maped_num.tolist())
    # str_count = "".join(count_u_list)
    # print("str_count", str_count)

    key = str_unique+str_count

    if key in dic.keys():
        print(dic[key])
        continue
    else:
        for count_u in count_u_list:
            if count_u < 2:
                continue
            # (count_u) C 2
            tmp = comb(count_u,2)
            ans += tmp

    
    dic[key] = ans
    print(ans)


import numpy as np
import math
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
from collections import Counter


N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
np_A = np.array(A)


tmp_all = np.array(A)
unique = np.unique(tmp_all)
print("unique:", unique)
ans_all = 0
for u in unique:
    count_u = c[u]
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)



for i in A:
    print(ans_all - c[i] + 1)


import numpy as np
from collections import Counter


N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
np_A = np.array(A)


tmp_all = np.array(A)
unique = np.unique(tmp_all)

ans_all = 0
for u in unique:
    count_u = c[u]
    # print(f"u:{u}, count_u:{count_u}")
    if count_u < 2:
        continue
    # (count_u) C 2
    tmp = int(count_u*(count_u-1)/2)
    ans_all += tmp
# print("all", ans_all)



for i in A:
    print(ans_all - c[i] + 1)


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Accepted