Problem:
 Score : 400 points    Problem Statement  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.       Constraints   3 \leq N \leq 2 \times 10^5  1 \leq A_i \leq N  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output  For each k=1,2,...,N , print a line containing the answer.        Sample Input 1 5
1 1 2 1 2      Sample Output 1 2
2
3
2
3  Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2 . From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal. Thus, the answer for k=1 is 2 .       Sample Input 2 4
1 2 3 4      Sample Output 2 0
0
0
0  No two balls have equal numbers written on them.       Sample Input 3 5
3 3 3 3 3      Sample Output 3 6
6
6
6
6  Any two balls have equal numbers written on them.       Sample Input 4 8
1 2 1 4 2 1 4 1      Sample Output 4 5
7
5
7
7
5
7
5

Solution (Python):
import math
from collections import Counter

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

def comb2(n):
    return n * (n - 1) // 2

c = Counter(A)
ans = 0
for m in c.values():
    if m >= 2:
        ans +=comb2(m)

for i in range(N):
    print(ans - c[A[i]] + 1)

Solution (Python):
N = int(input())
arr = [0] + list(map(int,input().split()))
cnt_arr = [0]*(N+1)
for i in arr:
  cnt_arr[i] += 1
def cmb2(n):
  if n<2:
    return 0
  else:
    return n*(n-1)//2
cmb_arr = [0]*(N+1)

_sum = 0
for idx,cnt in enumerate(cnt_arr):
  cmb_arr[idx] = cmb2(cnt)
  _sum += cmb_arr[idx]

for k in range(1,N+1):
  print(_sum - cmb2(cnt_arr[arr[k]]) + cmb2(cnt_arr[arr[k]]-1))

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    cnt = dict()
    ans = dict()
    for i in a:
        cnt[i] = cnt.get(i, 0) + 1

    for (idx, v) in cnt.items():
        ans[idx] = v * (v - 1) // 2

    sum_ = sum(ans.values())

    for k in range(len(a)):
        res = sum_ - (cnt[a[k]] - 1)
        print(res)

Solution (Python):
import math
def comb(n, r):
  return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))

check_list = []
for i in range(N + 1):
  check_list.append([i, 0, 0])

for a in A:
  check_list[a][1] += 1

count = 0
for check in check_list:
  if (check[1] < 2):
    continue
  else:
    c = comb(check[1], 2)
    check_list[check[0]][2] = c
    count += c

for k in range(1, N + 1):
  rid = A[k - 1]
  if (check_list[rid][1] >= 1):
    minus = check_list[rid][1] - 1
  else:
    minus = 0
  result = count - minus
  print(result)

Solution (Python):
from collections import Counter

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

def comb2(n):
    return n * (n - 1) // 2

cnt = Counter(A)
ans = 0

for c in cnt.values():
    if c >= 2:
        ans += comb2(c)

for i in range(N):
    print(ans - cnt[A[i]] + 1)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
m=max(a)
l=[0]*(m+1)
c=[0]*(m+1)
for i in a:
  l[i]+=1
for i in set(a):
  c[i]=l[i]*(l[i]-1)/2
s=sum(c)
for i in range(n):
  print(int(s-c[a[i]]+(l[a[i]]-1)*(l[a[i]]-2)/2))

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
d = defaultdict(int)
for x in a:
  d[x] += 1
  
s = 0
for k,v in d.items():
  s += v * (v-1) // 2
for x in a:
  c = d[x] - 1
  print(s - c)

Solution (Python):
def main():
    n = int(input())

    def nc2(n):
        return n*(n-1)//2

    dic = {}
    array = input().split()
    for k in array:
        if k in dic:
            dic[k] += 1
        else:
            dic[k] = 1

    ans = 0
    for k in dic:
        ans += nc2(dic[k])

    for a in array:
        print(ans - dic[a] + 1)

if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
A = list(map(int, input().split()))
X = [0]*(n+1)
for a in A:
    X[a] += 1
s = 0
for x in X:
    s += x*(x-1)//2
for a in A:
    print(s-X[a]+1)

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
count=[0]*(n+1)
for i in range (n):
    count[a[i]]+=1
m=0
for i in range (1,n+1):
    m+=count[i]*(count[i]-1)//2
for i in range (n):
   print(m-(count[a[i]]-1))

Solution (Python):
def comb(val):
    if(val > 0):
        return int(val*(val-1) / 2)
    else:
        return 0
    
def make_map(balls):
    balls_map = {}
    for ball in balls:
        if(ball in balls_map):
            balls_map[ball] += 1
        else:
            balls_map[ball] = 1
    return balls_map

def main():
    args = input().rstrip().split(' ')
    N = int(args[0])
    balls = [val for val in input().rstrip().split(' ')]
    balls_map = make_map(balls)
    
    sum_ = 0
    for val in balls_map.values():
        sum_ += comb(val)
    for ball in balls:
        print(sum_ - (balls_map[ball]-1))
    
if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
from operator import mul
from functools import reduce
def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


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

hoge=Counter(A)
ans=0
for i in hoge.values():
    if i>=2:
        ans += combinations_count2(i, 2)
for i in range(N):
    print(ans-hoge[A[i]]+1)

Solution (Python):
from collections import Counter
from operator import mul
from functools import reduce
def combinations_count2(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


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

hoge=Counter(A)
ans=0
for i in hoge.values():
    if i>=2:
        ans += combinations_count2(i, 2)
for i in range(N):
    print(ans-hoge[A[i]]+1)

Solution (Python):


import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def banned(al):
    d = {}
    for a in al:
        if a in d.keys():
            d[a] += 1
        else:
            d[a] = 1
    
    ans = 0
    for k, v in d.items():
        if v > 1:
            ans += comb(v, 2)
            
        
            
    
    for a in al:
        
        
        
        
        
        
        print(int(ans-(d[a]-1)))

n = int(input())
al = [int(i) for i in input().split()]

banned(al)

Solution (Python):
import collections

def comb2(n):
    return int(n * (n - 1) / 2)

def combdict(dic, dp):
    ans = 0
    for v in dic.values():
        ans += dp[v]
    return ans

def solve(a, dic, dp):
    allsum = combdict(dic,dp)
    for i in a:
        print(allsum - dp[dic[i]] + dp[dic[i]-1])

def main():
    dp = [comb2(i) for i in range(2 * ( 10 ** 5) + 1)]
    n = int(input())
    a = list(map(int,input().split()))
    d = collections.Counter(a)
    solve(a, d, dp)

if __name__ == '__main__':
    main()

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

num_dic = {}

for x in A:
    if x in num_dic:
        num_dic[x] += 1
    else:
        num_dic[x] = 1

tot = 0
for key, val in num_dic.items():
    tot += val * (val - 1) // 2

for x in A:
    print(tot - num_dic[x] + 1)

Solution (Python):
from collections import Counter
def choose2(n):
    return n*(n-1)/2
sum1=0
sum2=0
N = int(input())
A = [int(N) for N in input().split()]
S = Counter(A)
for i in  S.values():
    sum1+=int(choose2(i))
for j in range(N):
    sum2+=int(sum1-choose2(S[A[j]])+choose2(S[A[j]]-1))
    print(sum2)
    sum2=0

Solution (Python):
from collections import Counter

n = int(input())
l = [int(i) for i in input().split()]

S = Counter(l)

ans = 0
for i in S.values():
    ans += int(i * (i - 1) / 2)

for i in range(n):
    val = S[l[i]]
    print(ans - val + 1)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)



def in_int():
    return (int(input()))


def in_list():
    return (list(map(int, input().split())))


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


def in_ints():
    return (map(int, input().split()))




n = in_int()

a = in_list()

m = {}
max_1 = 0
val_1 = -1
max_2 = 0
val_2 = -1
for xx in a:
    if xx in m:
        m[xx]+=1
    else:
        m[xx] = 1


ans = 0

for xx in m:
    tt = m[xx]
    ans += tt*(tt-1)//2



for xx in a:
    tt = m[xx]

    print(ans - (tt)*(tt-1)//2 + (tt-1)*(tt-2)//2)

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))

sub = collections.Counter(a)
ans = 0
for i in sub:
    ans += sub[i] * (sub[i] - 1) // 2
for i in range(n):
    ad = sub[a[i]] * (sub[a[i]] - 1) // 2 - ((sub[a[i]] - 1) * (sub[a[i]] - 2) // 2)
    print(ans - ad)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
li=[0 for i in range(n+1)]
for i in a:
  li[i]+=1
co=[]
cok=[]
for i in li:
  co.append(int(i*(i-1)/2))
  cok.append(int((i-1)*(i-2)/2))
su=sum(co)
for i in a:
  print(su-co[i]+cok[i])

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))


a_ol = Counter(a)
def_count = 0
for item in a_ol.items():
    a_co = item[1]
    def_count += a_co * (a_co - 1)

def_count //= 2

for k in a:
    print(def_count - a_ol[k] + 1)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
d=Counter(A)
dic={}
total=0
for item in d.items():
    counter=item[1]
    s1=(counter - 1)*(counter - 2)/2 
    s2=(counter)*(counter - 1)/2
    dic[item[0]]=int(s1 - s2)
    total+=int(s2)

for a in A:
    print(total + dic[a])

Solution (Python):


import collections

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

c = collections.Counter(A)
values = c.keys()
counts = c.values()

subs = {}
a_sum = 0
for v, c in zip(values, counts):
    subs[v] = c
    a_sum += (c * (c-1)) // 2

for i in range(N):
    print(a_sum - subs[A[i]] + 1)

Solution (Python):
from collections import Counter

def nC2(n):
    return n * (n - 1) // 2

n = int(input())
balls = [int(i) for i in input().split()]

c = Counter(balls)
total = 0
for k, v in c.items():
    total += nC2(v)

for i in range(n):
    ball = balls[i]
    num = c.get(ball)
    ans = total - nC2(num) + nC2(num - 1)
    print(ans)

Solution (Python):
from collections import Counter

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

    c = Counter(A)
    vals = c.keys()
    cnts = c.values()

    subs = {}
    a_sum = 0
    for v, c in zip(vals, cnts):
        subs[v] = c - 1
        a_sum += (c * (c-1)) // 2
    
    for a in A:
        print(a_sum - subs[a])


if __name__ == '__main__':
    main()

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))
counter=collections.Counter(A)
count=list(counter.values())
s=0
for i in range(len(count)):
    k=count[i]
    s+=k*(k-1)//2
for i in range(N):
    l=counter[A[i]]
    print(s-(l-1))

Solution (Python):
def main():
    N = int(input())
    A = list(map(int,input().split()))

    count = [0 for i in range(N+1)]

    num = [0 for i in range(N+1)]
    sum_all = 0

    for i in range(N):
        count[A[i]] += 1
    
    for i in range(1,N+1):
        if count[i] >= 2:
            num[i] = int(count[i]*(count[i]-1)/2)
            sum_all += num[i]

    for i in range(0,N):
        ans = sum_all - num[A[i]]
        if count[A[i]]-1 >= 2:
            ans += int((count[A[i]]-1)*(count[A[i]]-2)/2)
        print(ans)

main()

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
import collections
import math
def combinations_count2(n):
    if(n<=1):
        answer=0
    else:
        answer=n*(n-1)/2
    return answer

table=collections.Counter(a)
keytable=table.keys()
sums=0
for value in table.values():
    sums+=combinations_count2(value)




for i in range(len(a)):
    answer=sums-table[a[i]]+1
    print(int(answer))

Solution (Python):

n=int(input())
a=list(map(int,input().split()))

bunpu=dict()
for x in a:
    if x not in bunpu:
        bunpu[x]=1
    else:
        bunpu[x]+=1
    
def choose2(num):
    return num*(num-1)//2

total=0
for value in bunpu.values():
    total+=choose2(value)
for x in a:
    ans=total
    ans-=choose2(bunpu[x])
    ans+=choose2(bunpu[x]-1)
    print(ans)

Solution (Python):
from collections import Counter
N=int(input())
A=[int(item)for item in input().split()]
c=Counter(A)
full = {i: c[i] * (c[i] - 1) / 2 for i in c.keys()}



sumA = sum(full.values())
val = {i:int(sumA-full[i]+(c[i]-2) * (c[i]-1) / 2 )for i in c.keys()}




for a in A:
    print(val[a])

Solution (Python):
import collections

n = int(input())

nums = list(map(int, input().split()))

cnt = collections.Counter(nums)

def c2(n):
    return int(n * (n-1) / 2)

total = 0

for c in cnt.values():
    total += c2(c)

for i in range(n):
    d = cnt[nums[i]]
    diff = c2(d) - c2(d-1)
    print(total - diff)

Solution (Python):
from collections import defaultdict

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

counter = defaultdict(list)
for i in range(len(nums)):
  counter[nums[i]].append(i)

total = 0
for _,v in counter.items():
  v = len(v)
  total += (v * (v-1)) // 2

for k in range(len(nums)):
  print(total - (len(counter[nums[k]])-1))

Solution (Python):
from functools import reduce
n=int(input())
a=list(map(int,input().split()))
c=list(range(1,n+1))
num=[0]*n
for i in range(n):
    num[a[i]-1]=num[a[i]-1]+1
preans=reduce(lambda x,y:x+y*(y-1)/2,num,0)

for i in range(n):
    print(int(preans-num[a[i]-1]+1))

Solution (Python):
import math

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

L = [[] for _ in range(N)]
for i in range(N):
    L[A[i]-1].append(i)

com = []
for i in range(N):
    n = len(L[i])
    com.append(n * (n - 1) // 2)
s = sum(com)

for k in range(N):
    ans = s - com[A[k]-1]
    m = len(L[A[k]-1]) - 1
    ans = ans + m * (m - 1) // 2
    print(ans)

Solution (Python):
n=int(input());arr = list(map(int,input().split()))
from collections import Counter
cnt = Counter(arr)
total = 0
for i in cnt:
  total += (cnt[i]**2 - cnt[i])//2
for i in arr:
  print(total-(cnt[i]**2-cnt[i])//2 + (cnt[i]-1)*(cnt[i]-2)//2)

Solution (Python):
import collections

n = int(input())
a_lst = [int(elem) for elem in input().split()]
c = collections.Counter(a_lst)
res1 = 0
for k in c.keys():
  res1 += c[k] * (c[k] - 1) // 2

for i in range(n):
  res = res1
  res -= c[a_lst[i]] - 1
  print(res)

Solution (Python):
import numpy as np
N = input()
As = np.array(input().split(), dtype=int)


from collections import defaultdict
d = defaultdict(int)
for a in As:
    d[a] += 1
    

patterns = sum([v * (v - 1) // 2 for v in d.values()])


for num in As:
    times = d[num]
    print(patterns - times + 1)

Solution (Python):
def main():
  from collections import Counter
  n=int(input())
  a,b=tuple(map(int,input().split())),[0]*n
  c=Counter(a)
  x=0
  for i in set(a):
    b[i-1]=c[i]
    x+=(b[i-1]*(b[i-1]-1))//2
  b=tuple(b)
  for i in a:
    y=b[i-1]-1
    print(x-y if x>=y else 0)
if __name__=='__main__':
  main()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
C = [0] * (N+1)
sum = 0

for i in range (0,N):
    C[A[i]]+=1




for i in range(1, N+1):
    sum += C[i]*(C[i]-1)/2

sum = int(sum)




for i in range(0,N):
    
    print(sum - (C[A[i]] -1))

Solution (Python):


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

from collections import Counter

pairs = {}

ca = Counter(A)

for k,v in ca.items():
  
  if v>1:
    pairs[k] = int(v*(v-1)/2)
  else:
    pairs[k] = 0

pairs_sum = int(sum(pairs.values()))

for k in range(N):
  t = ca[A[k]]
  print(pairs_sum-(t-1))

Solution (Python):
import collections

n = int(input())
data = list(map(int, input().split()))
c = collections.Counter(data)


sum = 0
for key in c.keys():
  sum += int( c[key] * (c[key]-1) / 2 )



for i in range(n):
  out = sum
  out -= int( c[data[i]] * (c[data[i]]-1) / 2 )
  
  out += int( (c[data[i]]-1) * (c[data[i]]-2) / 2)
  
  print(out)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

b = [0]*n
c = 0
d = 0

for i in range(n):
    c = a[i] - 1
    b[c] = b[c] + 1

for j in range(n):
    d = d + b[j]*(b[j]-1)/2

for k in range(n):
    c = a[k] - 1
    print(int(d-b[c]+1))

Solution (Python):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
B = np.zeros(N+1)

for i in range(N):
  B[A[i]]+=1


all_c = 0
for i in range(1,N+1):
  all_c += int(B[i]*(B[i]-1)/2)			
for i in range(N):
  if B[A[i]] != 0:
    print(int(all_c-(B[A[i]]-1)))

Solution (Python):
import numpy as np
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))
number = np.zeros(N, dtype = int)
total = 0

dic = {}
a = sorted(A)
now_key = a[0]
value = 1
dic[now_key] = 1
for i in range(1,N):
  if a[i] == now_key:
    value += 1
  else:
    dic[now_key] = value
    if value >= 2:
      total += value * (value-1) // 2
    now_key = a[i]
    value = 1
dic[now_key] = value
if value >= 2:
  total += value * (value-1)//2
for j in A:
  print(total - dic[j] + 1)

Solution (Python):
n = int(input())

cnt =[0]*(n+1)

a = list(map(int,input().split()))

for i in range(n):
    cnt[a[i]]+=1

pairs = 0

b = []
for i in range(n+1):
    c = cnt[i]*(cnt[i]-1)/2
    b.append(c)
    pairs += c

for i in a:
    print(int(pairs - b[i] +(cnt[i]-1)*(cnt[i]-2)/2))

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
from collections import Counter

c = Counter(A)

cou = 0
for x in c:
    
    if c[x]>1:
        y = c[x]
        cou+=y*(y-1)//2



for i in A:
    X = c[i]
    ans = cou - X*(X-1)//2 + (X-1)*(X-2)//2
    print(ans)

Solution (Python):
n = int(input())
an = list(map(int, input().split()))

dic = {}
for i in range(n):
  if an[i] not in dic.keys():
    dic[an[i]] = 1
  else:
    dic[an[i]] += 1

ans = {}
all_pattern = 0
for k,v in dic.items():
    if v >= 2:
        all_pattern += v*(v-1)//2
for k,v in dic.items():
    ans[k] = all_pattern - (v-1)
for v in an:
    print(ans[v])

Solution (Python):
import collections

N = int(input())
A = list(map(int,input().split()))
c = [0]*N
p = 0


c = collections.Counter(A)

for i in range(N):
  p += (c[i+1])*(c[i+1]-1)//2

for i in range(N):
  print(p-c[A[i]]+1)

Solution (Python):
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    ac = collections.Counter(a)
    t = 0
    for k, v in ac.items():
        t += v * (v - 1) // 2
    for v in a:
        print(t - (ac[v] * (ac[v] - 1) // 2) + ((ac[v] - 1) * (ac[v] - 2) // 2))


if __name__ == '__main__':
    solve()

Solution (Python):
def comb_2(a):
    return int(a*(a-1)/2)

n = int(input())
a = list(map(int,input().split()))
a_cnt = [0 for i in range(n)]
for i in a:
    a_cnt[i-1] += 1

sum_all = 0
for i in a_cnt:
    sum_all += comb_2(i)

for i in a:
    print(sum_all - comb_2(a_cnt[i-1]) + comb_2(a_cnt[i-1] - 1))

Solution (Python):

N = int(input())
A = [int(s) for s in input().split()]

def com(x):
    return int(x * (x - 1) / 2)

data_dict = {}
for x in A:
    if x not in data_dict:
        data_dict[x] = 1
    else:
        data_dict[x] += 1


com_dict = {}
com_sum = 0
for x in data_dict.keys():
    com_dict[x] = com(data_dict[x])
    com_sum += com_dict[x]



result_dict = {}
for x in A:
    if x not in result_dict:
        result_dict[x] = com_sum - com_dict[x] + com(data_dict[x] - 1)
    print(result_dict[x])

Solution (Python):
from collections import Counter
import math

def main():
    r=2
    def comb2(n):
        if n < 2:
            return 0
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    
    all_c = sum([comb2(count[i]) for i in count])
    
    for b in A:
        print(all_c - (count[b]-1))
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
 
from collections import Counter
 
N = int(input())
 
A = list(map(int, input().split()))
 
c = Counter(A) 
 
s = 0
for key in c:
    s += c[key] * (c[key] - 1) // 2  
 
for num in A:
    print(s - c[num] * (c[num]-1) // 2 + (c[num]-1) * (c[num]-2) // 2)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
a=sorted(A)
counter=1
ans=0
value=a[0]
dict={}
for i in range(1,n):
    if a[i]==value:
        counter+=1
    else:
        dict[value]=counter
        if counter<2:
            pass
        else:
            ans+=counter*(counter-1)//2
        counter=1
        value=a[i]
dict[value]=counter
if counter<2:
    pass
else:
    ans+=counter*(counter-1)//2
for j in range(n):
    count=dict[A[j]]-1
    if count==0:
        print(ans)
    elif count==1:
        print(ans-1)
    else:
        c=ans-(count+1)*count//2+count*(count-1)//2
        print(c)

Solution (Python):
from collections import Counter as C
N = int(input())
A = list(map(int,input().split()))
c = C(A)
s = 0
for i in c.values():
    s += i*(i-1)//2
for j in A:
    print(s-(c[j]-1))

Solution (Python):
import collections
import math

n = int(input())
a = list(map(int, input().split()))

c = collections.Counter(a)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

all_comb = 0
for v in c.values():
    if v != 1:
        all_comb += combinations_count(v,2)

for i in a:
    print(all_comb-(c[i]-1))

Solution (Python):
from collections import defaultdict


ball_num = int(input().rstrip())
points = [int(n) for n in input().rstrip().split()]
point_dict=defaultdict(int)
total_pattern=0

for i in range(ball_num):
    point_dict[points[i]]+=1
for j in point_dict:
    total_pattern += point_dict[j]*(point_dict[j]-1)//2
    
total_pattern0=total_pattern
for ban_i in range(ball_num):
    total_pattern0 -= point_dict[points[ban_i]]*(point_dict[points[ban_i]]-1)//2         
    total_pattern0 += (point_dict[points[ban_i]]-1)*(point_dict[points[ban_i]]-2)//2
        
    print(total_pattern0)
    total_pattern0=total_pattern

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
tot=0
dic={}
for i in a:
  if i not in dic:
    dic[i]=1
  else:
    dic[i]+=1
for i in dic:
  tot+=int((dic[i]*(dic[i]-1))/2)

for i in range(N):
  c=dic[a[i]]-1
  print(tot-c)

Solution (Python):
n = int(input().strip())
A = list(map(int, input().strip().split()))

cnt=[0]*(n+1)
for a in A:
    cnt[a]+=1

total=0
for c in cnt:
    total+=c*(c-1)
total//=2

for k in range(n):
    print(total-cnt[A[k]]+1)

Solution (Python):
from collections import defaultdict


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for x in a:
        d[x] += 1
    diff = {}
    sum_ = 0
    for key, value in d.items():
        num = value * (value - 1) // 2
        sum_ += num
        diff[key] = num - (value - 1) * (value - 2) // 2
    for x in a:
        print(sum_ - diff[x])


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for x in a:
        d[x] += 1
    diff = {}
    sum_ = 0
    for key, value in d.items():
        num = value * (value - 1) // 2
        sum_ += num
        diff[key] = num - (value - 1) * (value - 2) // 2
    for x in a:
        print(sum_ - diff[x])


if __name__ == "__main__":
    main()

Solution (Python):

N = input()

K = list(map(int, input().split()))























ball_counts = {}
for i in range(len(K)):
    ball_value = K[i]
    if ball_value in ball_counts:
        ball_counts[ball_value] += 1
    else:
        ball_counts[ball_value] = 1


total_sum = 0
for value, count in ball_counts.items():
    total_sum += (count * (count - 1)) / 2


for i in range(len(K)):
    ball_value = K[i]
    print(int(total_sum - (ball_counts[ball_value] - 1)))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

l=[0]*(n+1)
for i in a:
    l[i]+=1

ans=0
for i in range(n+1):
    ans+=l[i]*(l[i]-1)//2

for i in a:
    print(ans-l[i]*(l[i]-1)//2+(l[i]-1)*(l[i]-2)//2)

Solution (Python):
n = int(input()); arr = list(map(int,input().split()))

from collections import Counter
countdic = Counter(arr)
sumdic = dict()
sum = 0

for i in countdic:
    sumdic[i] = (countdic[i]**2-countdic[i])//2
    sum += (countdic[i]**2-countdic[i])//2

for i in arr:
    print(sum-sumdic[i]+(countdic[i]-1)*(countdic[i]-2)//2)

Solution (Python):
n = int(input())
inp = list(map(int,input().split()))
d = {}
for x in inp:
    d[x] = d.get(x, 0) + 1
v = [0 for _ in range(n+1)]
for x, y in d.items():
    v[x] = (y)*(y-1)//2
tot = sum(v)
for i in range(n):
    print(tot-v[inp[i]]+((d[inp[i]]-1)*(d[inp[i]]-2)//2))

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
a=n1()

def comb(n):
    if n<=1:
        return 0
    if n==2:
        return 1
    return (n*(n-1))//2

from collections import Counter
c=Counter(a)

d={i:comb(j) for i,j in c.items()}

s=sum(d.values())


for i in a:
    print(s-d[i]+comb(c[i]-1))

Solution (Python):
from collections import Counter

n = int(input())
a = input().split()
freq = Counter(a)

total=0
    
for key,val in freq.items():
    total+=(val*(val-1))//2

for k in range(n): 

    kth = (freq[a[k]]*(freq[a[k]]-1))//2
    newkth = ((freq[a[k]]-2)*(freq[a[k]]-1))//2  

    total -= kth
    total += newkth
    
    print(total)
    
    total -=newkth
    total +=kth

Solution (Python):
def choose2(number):
	if number>=2:
		return (number*(number-1))/2
	else:
		return 0
N = int(input())
k = list(map(int,input().split()))
valArr = list(set(k))
occurence = []
occurenceIndex = []
mx = 0
for l in k:
	if l>mx:
		mx=l
for i in range(mx+1):
	occurence.append(0)
for j in range(N):
	occurence[k[j]]+=1
	
			
'''6*5/2
5*4/2
N*(N-1)/2
(N-2)*(N-1)/2
N*(N-1)/2*(N-2)/N
occurence = [1,2,3]
ans = 0+2*1/2+3*2/2
k val = 3
correct = 0+2*1/2+2*1/2

'''
ans = 0
for time in range(len(occurence)):
	ans+=choose2(occurence[time])
for ball in range(N):
	kans = ans-choose2(occurence[k[ball]])+choose2(occurence[k[ball]]-1)
	
	print(int(kans))

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))

def ch_2(n):
	
	return n*(n-1)/2

count=Counter(A)

ans=0
for i in count.values():
	ans+=ch_2(i)

for j in range(N):
	ttl=ans
	ttl-=ch_2(count[A[j]])
	ttl+=ch_2(count[A[j]]-1)
	print(int(ttl))

Solution (Python):
from collections import Counter

n=int(input())
a=list(map(int,input().split()))
s=0
m=Counter(a)
for i in m.values():
    s+=i*(i-1)//2
for i in a:
    print(s-m[i]+1)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
l = Counter(A)
al = 0
for i in l:
  al += l[i]*(l[i]-1)//2
for i in range(N):
    print(al - (l[A[i]]-1))

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
l = Counter(A)
al = sum([x*(x-1)//2 for x in l.values()])
for i in range(N):
    print(al - (l[A[i]]-1))

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
l = collections.Counter(A)
al = sum([x*(x-1)//2 for x in l.values()])
for i in range(N):
    print(al - (l[A[i]]-1))

Solution (Python):


n = int(input())
a = list(map(int, input().split()))

counts = {}

for ai in a:
    if ai in counts:
        counts[ai] += 1
    else:
        counts[ai] = 1

tbcomb = 0
for k in counts:
    c = counts[k]
    if c >= 2:
        tbcomb += c * (c - 1) // 2

for i in range(n):
    c = counts[a[i]]
    if c == 1:
        print(tbcomb)
    elif c == 2:
        print(tbcomb - 1)
    else:
        print(tbcomb - (c * (c - 1) // 2) + ((c - 1) * (c - 2) // 2))

Solution (Python):


'''
5
1 1 2 1 2
'''


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

import collections

a_0 = a[1:]
first_val = a[0]


c = collections.Counter(a_0)



cnt_0 = 0
for val in c.values():
    cnt_0 += int(val * (val-1) /2)
print(cnt_0)




for i in a_0:
    if i == first_val:
        print(cnt_0)
    else:
        print(cnt_0 + c[first_val] - (c[i]-1))

Solution (Python):
import math
inp = [input() for i in range(2)]
N = int(inp[0])
A = list(map(int, inp[1].split()))

d = {}
for k in A:
    
    
    if k in d.keys():
        d[k] += 1
    else:
        d[k] = 1






p = {}
for k in d.keys():
    cnt = d[k]
    p[k] = cnt * (cnt-1) / 2

total = sum(p.values())

for item in A:
    print(int(total - d[item] + 1))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

d = dict()
for key in a:
  if key in d:
    d[key] += 1
  else:
    d[key] = 1

sm = 0
for key in d:
  sm += d[key]*(d[key]-1)//2

for key in a:
  print(sm-d[key]+1)

Solution (Python):
import math
input()
items = list(map(int,input().split()))
d = {}
for i in items:
    if i in d.keys():
        d[i] = d[i]+1
    else:
        d[i] = 1

p = {}
for k in d.keys():
    cnt = d[k]
    p[k] = (cnt*(cnt-1))/2

total = sum(p.values())

for item in items:
    print(int(total - d[item] + 1))

Solution (Python):
from math import factorial
from collections import defaultdict


def cmb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


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

dA = defaultdict(int)
for i in A:
    dA[i] += 1

sm = 0

for i in dA.values():
    if i >= 2:
        sm += cmb(i, 2)
    else:
        sm += 0

for j in A:
    print(sm - (dA[j] - 1))

Solution (Python):
from math import factorial
from collections import defaultdict


def cmb(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))


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

dA = defaultdict(int)
for i in A:
    dA[i] += 1

sm = 0

for i in dA.values():
    if i >= 2:
        sm += cmb(i, 2)
    else:
        sm += 0

for j in A:
    print(sm - (dA[j] - 1))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
d = {}
for a in A:
    d[a] = d.get(a, 0) + 1
ans = sum([v * (v - 1) // 2 for v in d.values()])
for a in A:
    print(ans - d[a] + 1)

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

num = [0]*(N)
for i in range(N):
    num[A[i]-1]+=1
Ans_ = 0
for i in range(N):
    Ans_ += num[i]*(num[i]-1)//2
    A[i]-=1
for i in range(N):
    print(Ans_-num[A[i]]+1)