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 sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
from io import BytesIO, IOBase
 



 
def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r
 
def divs(n, start=1):
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors
 
def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
 
def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)
 
def ceil(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
 
 

 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
	newlines = 0
 
	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = "x" in file.mode or "r" not in file.mode
		self.write = self.buffer.write if self.writable else None
 
	def read(self):
			while True:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					if not b:
							break
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines = 0
			return self.buffer.read()
 
	def readline(self):
			while self.newlines == 0:
					b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
					self.newlines = b.count(b"\n") + (not b)
					ptr = self.buffer.tell()
					self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
			self.newlines -= 1
			return self.buffer.readline()
 
	def flush(self):
			if self.writable:
					os.write(self._fd, self.buffer.getvalue())
					self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
	def __init__(self, file):
			self.buffer = FastIO(file)
			self.flush = self.buffer.flush
			self.writable = self.buffer.writable
			self.write = lambda s: self.buffer.write(s.encode("ascii"))
			self.read = lambda: self.buffer.read().decode("ascii")
			self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 

'''for _ in range(ii()):
    n=ii()
    a=li()
    m,k,md=0,0,0
    mn,mx=10**9+1,-10**9-1
    for i in range(n-1):
        if a[i]!=-1 and a[i+1]!=-1:
            m=max(m,abs(a[i]-a[i+1]))
    for i in range(1,n-1):
        if a[i]==-1:
            if a[i+1]!=-1:
                mn=min(mn,a[i+1])
                mx=max(mx,a[i+1])
            if a[i-1]!=-1:
                mn=min(mn,a[i-1])
                mx=max(mx,a[i-1])
    if mn==10**9+1 and mx==-10**9-1:
        print(0,0,sep=' ')
    else:
        md=(mn+mx+1)//2
        m=max(abs(mx-md),abs(mn-md),m)
        print(m,md,sep=' ')'''

'''for _ in range(ii()):
    n,m=mi()
    tg=n//2
    n0=n-m
    total=(n*(n+1))//2
    res=0
    if n0<n:
        if m>=tg:
            res=total-n0
        else:
            i=tg-m
            if n&1:
                nn0=2*i+1
            else:
                nn0=2*i
            res=total-((nn0*(nn0+1))//2)-(n0-nn0)
            
    print(res)'''



'''def solve():
    n=int(input())
    a=list(map(int,input().split()))
    diff=len(set(a))
    if diff==1: print (int((n*(n+1))/2))
    elif diff==n: print (1)
    else:
        i=j=ans=0
        ahead=True
        distinct={}
        while j<n:
            if ahead:
                distinct[a[j]]=distinct[a[j]]+1 if a[j] in distinct else 1
                if len(distinct)==diff:
                    ahead=False
                else:
                    j+=1
            else:
                ans+=n-j
                if distinct[a[i]]>1:
                    distinct[a[i]]-=1
                else:
                    distinct.pop(a[i])
                    j+=1
                    ahead=True
                i+=1
        print (ans)
 
solve()'''



'''s=input()
n=len(s)
i,cntl,cntg=0,0,0
res,fl,fg=0,0,0
while(i<n):
    if s[i]=='<':
        while(i<n and s[i]!='>'):
            fl=1
            cntl+=1
            i+=1
    else:
        while(i<n and s[i]!='<'):
            fg=1
            cntg+=1
            i+=1
    if fl==1 and fg==1:
        res+=max(cntl,cntg)
        cntl-=1
        cntg-=1
        res+=cntl*(cntl+1)//2
        res+=cntg*(cntg+1)//2
        fl,fg,cntl,cntg=0,0,0,0
if fl:
    res+=cntl*(cntl+1)//2
if fg:
    res+=cntg*(cntg+1)//2
print(res)'''




def nc2(n):
    return (n*(n-1))//2
n=ii()
a=li()
d=dd()
for i in range(n):
    d[a[i]]+=1
res=0
for k in d:
    res+=nc2(d[k])
for i in range(n):
    print(res-d[a[i]]+1)

Solution (Python):
import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(input())


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


def LI():
    return list(MI())


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    _ = I()
    a_list = input().split()

    c = Counter(a_list)
    n = {}
    t = 0
    for k, v in c.items():
        n[k] = v - 1
        if v >= 2:
            t += int(v * (v - 1) / 2)

    for a in a_list:
        print(t - n[a])
    pass


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
l =  input()
s= l.split(" ")
for i in range(len(s)):
    s[i] = int(s[i])
v = dict()
sumi = 0
for i in s:
    if not i in v:
        v[i] = 1
    else:
        v[i]+=1
for i in v:
    if v[i]>=2:
        sumi+=v[i]*(v[i]-1)/2
for i in s:
    if v[i]>=2:

        if v[i]==2:
            print(int(sumi-1))
        else:
            print(int(sumi-v[i]*(v[i]-1)/2+(v[i]-2)*(v[i]-1)/2))
    else:
        print(int(sumi))

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

for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1

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


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

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

num_count = {}
count_comb = {}
num_ans = {}
sum_combs = 0

def comb2(n):
    if n not in count_comb:
        count_comb[n] = (n * (n - 1)) // 2
    return count_comb[n]

def ans(ex_num):
    ex_count = num_count[ex_num]
    return sum_combs + comb2(ex_count - 1) - comb2(ex_count)
    
    

    
    
    
    
    
    
    
    


for a in As:
    if a not in num_count:
        num_count[a] = 0
    num_count[a] += 1

for _, c in num_count.items():
    sum_combs += comb2(c)

for a in As:
    print(ans(a))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = {}
c = {}
for i in range(n):
  try:
    l[a[i]] += 1
  except:
    l[a[i]] = 1

for k,v in l.items():
  c[k] = v*(v-1)/2

sum_val = sum(c.values())

for j in a:
  v = l[j] - 1
  print(int(sum_val - c[j] + v*(v-1)/2))

Solution (Python):
import collections
import numpy as np
from math import factorial

N = int(input())

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

cntr = collections.Counter(A)

uni_A = np.unique(A)

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

comb_d = dict()

for a in uni_A:
    n = cntr[a]
    comb_d[a] = comb(n, 2) if n > 1 else 0

d = dict()
S = sum(comb_d.values())
for a in uni_A:
    s = S - (cntr[a] - 1)
    d.update({a:s})

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

Solution (Python):




import numpy as np

from collections import Counter



def getIntList(inp):
	return [int(_) for _ in inp]


from sys import stdin



def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs


def unique(x):
	count=Counter()
	for xi in x:
		count[xi]+=1
	u,c=zip(*count.items())
	return u,c



def main(inputs):
	n=int(inputs[0][0])
	aa=getIntList(inputs[1])



	




	aUnique, count=unique(aa)
	aUnique=np.array(aUnique, np.int64)
	count=np.array(count, np.int64)
	




	

	countUnique,countCount=unique(count)
	countCount=np.array(countCount, np.int64)
	
	pat={}
	countAns=np.empty(len(countUnique), np.int64)
	countAns1=np.empty(len(countUnique), np.int64)
	for ci,c in enumerate(countUnique):
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns[ci]=pat[c]
		
		c-=1
		if c not in pat:
			ni=c*(c-1)//2
			pat[c]=ni
		countAns1[ci]=pat[c]
	




	
	countUniqueIndex=dict([(c,i) for i,c in enumerate(countUnique)])

	

	nums=np.empty(n, np.int64)
	ans={}
	for cui,cu in enumerate(countUnique):
		countCount[cui]-=1
		num0=(countAns*countCount).sum()
		countCount[cui]+=1
		num1=countAns1[cui]
		num=num0+num1

		countMask=count==cu

		targetA=aUnique[countMask]

		for a in targetA:
			ans[a]=num








	for a in aa:
		print(ans[a])
	

if __name__=="__main__":
	inputs=getInputs()

	main(inputs)

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
b=[x for x in a]
b.sort()
d={}
m=0
for c in range(n):
    if b[c]==m:
        d[m]=d[m]+1
    else:
        m=b[c]
        d[m]=1
s=0
for c in d:
    s=s+(d[c]*(d[c]-1))//2
for c in a:
    print(s-d[c]+1)

Solution (Python):
n =int(input())
a = list(map(int,input().split()))
b = []
x = {}
def fx(x) :
    ans = 0
    for i in range(1,x):
        ans += i
    return ans
for i in a :
    if i in x :
        x[i] += 1
        if x[i] == 2:
            b.append(i)
    else :
        x[i] = 1
anskari = 0
for i in b :
    anskari += fx(x[i])
for i in a :
    if x[i] == 1:
        print(anskari)
    else :
        print(anskari - x[i] +1)

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

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

Solution (Python):
from collections import Counter

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

if len(a) == len(set(a)):
    for i in range(n):
        print(0)
else:
    kw = Counter(a)
    ans = sum([x * (x - 1) // 2 for x in kw.values()])

    for i in range(n):
        print(ans - kw[a[i]] + 1)

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

counts = {}
for i in range(N):
    a = A[i]
    if a in counts:
        counts[a] += 1
    else:
        counts[a] = 1



ans_memory = {}
ans_memory_val = {}
for k in range(N):
    if A[k] in ans_memory:
        print(ans_memory[A[k]])
        continue
    if counts[A[k]] in ans_memory_val:
        print(ans_memory_val[counts[A[k]]])
        continue

    counts_tmp = counts.copy()
    counts_tmp[A[k]] -= 1

    
    ans = 0
    for v in counts_tmp.values():
        ans += v * (v - 1) // 2
    ans_memory[A[k]] = ans
    ans_memory_val[counts[A[k]]] = ans
    print(ans)

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

d = {}
for a in A:
  d[a] = d.get(a, 0) + 1
sum = sum([v*(v-1)/2 for v in d.values()]) 

for i in range(N):
  print(int(sum-d[A[i]]+1))

Solution (Python):
import bisect,collections,copy,itertools,math,string
def I(): return int(input())
def S(): return input()
def LI(): return list(map(int,input().split()))
def LS(): return list(input().split())

N = I()
A = LI()
_123 = [0]*N
_123count = [0]*N
for x in A:
    _123[x-1] += 1
_123copy = copy.copy(_123)
_123copy.sort(reverse=True)
_count = {}
for i,x in enumerate(_123copy):
    if x==0:
        break
    if x==1:
        _123count[i] = 0
    elif i!=0 and _123copy[i]==_123copy[i-1]:
        _123count[i] = _123count[i-1]
    else:
        _123count[i] = math.factorial(x)//((math.factorial(x-2))*(math.factorial(2)))
        _count[x] = _123count[i]
_sum = sum(_123count)
for x in A:
    temp = _123[x-1]-1
    if temp<=0:
        print(_sum)
    elif temp==1:
        print(_sum-1)
    else:
        if (temp+1) in _count:
            tempminus = _count[temp+1]
        else:
            tempminus = math.factorial(temp+1)//((math.factorial(temp-1))*(math.factorial(2)))
            _count[temp+1] = tempminus
        if (temp) in _count:
            tempplus = _count[temp]
        else:
            tempplus = math.factorial(temp)//((math.factorial(temp-2))*(math.factorial(2)))
            _count[temp] = tempplus
        print(_sum-tempminus+tempplus)

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

def n_c_2(val):
  ret = 0
  if val > 1:
    ret = (val)*(val-1)//2
  return ret

org_nc2 = {}
org_minus1_nc2 = {}
sumall = 0
for k in list(c.keys()):
  org_nc2[k] = n_c_2(c[k])
  org_minus1_nc2[k] = n_c_2(c[k] - 1)
  sumall += org_nc2[k]
for d in a:
  ans = sumall - org_nc2[d] + org_minus1_nc2[d]
  print (ans)

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

a = list(map(lambda x:int(x), a))

counter = [0] * 200005
choice = [0] * 200005
choice_minus1 = [0] * 200005
table = [-1] * 2000005
sum = 0

for i in range(0, len(a)):
    counter[a[i]] = counter[a[i]] + 1

for i in range(0, len(counter)):
    tmp_choice = int(counter[i] * (counter[i]-1) / 2)
    if tmp_choice <= 0:
        choice[i] = 0
    else:
        choice[i] = tmp_choice
        sum = sum + tmp_choice
    
    tmp_choice = int((counter[i]-1) * (counter[i]-2) / 2)
    if tmp_choice <= 0:
        choice_minus1[i] = 0
    else:
        choice_minus1[i] = tmp_choice

for i in range(0, len(a)):
    if table[a[i]] == -1:
        print(sum-choice[a[i]]+choice_minus1[a[i]])
        table[a[i]] = sum-choice[a[i]]+choice_minus1[a[i]]
    else:
        print(table[a[i]])

Solution (Python):


MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

N = int(input())
A = list(map(int, input().split()))
dic = {}
for a in A:
    if a in dic: dic[a] += 1
    else: dic[a] = 1

C = {0:0}

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

D = {}
for k, v in dic.items():
    sum_ni += C[v]


for k, v in dic.items():
    D[k] = sum_ni - C[dic[k]] + C[dic[k]-1]

for i in range(N):
    print(D[A[i]])

Solution (Python):
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
N=int(input())
count=[0]*N
A=list(map(int,input().split()))
for i in range(N):
    count[A[i]-1]+=1

k=max(count)
"""
kari=count.copy()
kari.sort()
tekitou=kari[1]"""

ans=0
for i in range(N):
    if count[i]>=2:
        ans+=cmb(count[i],2)

pin=cmb(k,2)
pink=cmb(k-1,2)

for i in range(N):
    unti=count[A[i]-1]
    
    if unti==k:
        print(ans-pin+pink)
    else:
        if unti>2:
            print(ans-cmb(unti,2)+cmb(unti-1,2))
        elif unti==2:
            print(ans-1)
        else:
            print(ans)

Solution (Python):
from collections import Counter

N = int(input())
A = tuple(map(int, input().split()))
C = Counter(A)

MX = sum(v*(v-1) for v in C.values())//2
for i in range(N):
    print(MX-C[A[i]]+1)

Solution (Python):
def compute(x):
	if x>=2:
		return (x*(x-1)//2)
	else:
		return 0

n = int(input())
a = list(map(int,input().split()))
d = {}
for i in a:
	if i not in d:
		d[i] = 1
	else:
		d[i] += 1
s = 0
for j in d:
	s += compute(d[j])

for i in a:
	temp = s - compute(d[i])
	temp = temp + compute(d[i]-1)
	print(temp)

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


dict_A={}
B=list(set(A))
sum=0


for a in A:
    if a not in dict_A.keys():
        dict_A[a]=1
    else:
        dict_A[a]+=1
for b in B:
    sum+=dict_A[b]*(dict_A[b]-1)//2

for a in A:
    print(sum-dict_A[a]+1)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
c=[0]*n
ans=0

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

for k in range (n):
  h=c[l[k]-1]
  if h==0 or h==1:
    print (ans)
  else:
    print(ans-int(h*(h-1)/2)+int((h-1)*(h-2)/2))

Solution (Python):
import math

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

num = [0] * N

for i in A:
    num[i - 1] = num[i - 1] + 1

a = [0] * N
b = [0] * N

for i in range(N):
    if num[i] >= 2:
        a[i] = (num[i] - 1) * (num[i] - 2) // 2
        b[i] = num[i] - 1

sum_a = sum(a)
sum_b = sum(b)

for i in A:
    print(sum_a + sum_b - b[i - 1])

Solution (Python):
import collections as col

def comb(n):
	ret = 0
	if n == 0:
		ret = 0
	else:
		ret = n * (n-1) / 2

	return ret

if __name__ == '__main__':
	N = int(input())
	v = [int(a) for a in input().split()]

	count = col.Counter(v)

	ans = 0

	for i in range(N):
		k = i + 1
		c = count[k]
		if c > 1:
			ans += comb(c)

	for i in range(N):
		c = count[v[i]]
		if c > 1:
			ANS = ans - (c - 1)
		else:
			ANS = ans
		print(int(ANS))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0] * 200001

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

for i in range(200001):
    y = B[i]
    ans = ans +(y * (y - 1) / 2 )
    
for i in range(N):
    j = A[i]
    p = ans - B[j] + 1
    p = int(p)
    print(p)

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

l_dic = {}
for i in l:
  if i in l_dic:
    l_dic[i] += 1
  else:
    l_dic[i] = 1
cnt_dic = {}
for i, j in l_dic.items():
  cnt_dic[i] = j*(j-1)//2
ans = 0
for i, j in cnt_dic.items():
  ans += j

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

Solution (Python):
import numpy as np

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

D = np.zeros(2*(10**5)+1,dtype=int)


for i in L:
    D[i] += 1

Dcopy = D


D = D*(D-1)/2
temp = int(D.sum())

for j in range(len(L)):
    if Dcopy[L[j]] > 1:
        print(temp - Dcopy[L[j]] + 1)
    else:
        print(temp)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

def resolve():
    N = ir()
    
    A = lr()
    A = [[A[i], i] for i in range(N)]
    A.append([2*10**5+1, N+1])
    A = sorted(A, key=lambda x: x[0])
    c = []
    d = []
    e = []
    tmp = 1
    st = 0
    for i in range(N+1):
        if tmp == A[i][0]:
            st += 1
        else:
            if st > 1:
                c.append(tmp)
                d.append(st)
                e.append(st*(st-1)//2)
            else:
                c.append(tmp)
                d.append(st)
                e.append(0)
            tmp = A[i][0]
            st = 1
    ans = []
    tmp = 0
    
    se = sum(e)
    for i in range(N):
        if A[i][0] > c[tmp]:
            tmp += 1
        num = d[tmp]
        plu = 0
        if num > 2:
            plu = (num-1)*(num-2)//2
        ans.append([se-e[tmp]+plu, A[i][1]])
    
    
    ans = sorted(ans, key=lambda x: x[1])
    for i in range(N):
        print(ans[i][0])

resolve()

Solution (Python):
import collections
N = int(input())
l = list(map(int,input().split()))
c = collections.Counter(l)
cnt = 0
for v in c.values():
    cnt += v*(v-1)//2
    
for i in range(N):
  print(cnt - c[l[i]] + 1)

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

d=[0]*(N+1)

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

for x in range (1,N+1):
    z+=d[x]*(d[x]-1)//2
    
for y in range (0,N):
    r=A[y]
    print(z+1-d[r])

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

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

s = 0
for i in range(N):
  s += B[i]*(B[i]-1)//2

for i in range(N):
  print(s-B[A[i]-1]+1)

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

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

import collections

c = collections.Counter(a)

combinations = [0] + [0] * n
for i in range(0, n+1):
    combinations[i] = (i * (i-1)) // 2
memo = [0] + [0] * n

for k in c:
    memo[k] = combinations[c[k]]

total = sum(memo)
for i in a:
    print(total - memo[i] + combinations[c[i] - 1])

Solution (Python):
from operator import mul
from functools import reduce

def ncr(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under


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

numList = [0] * (n+1)
numSet = set()

ncrList = [0] * (n+1)

resultList = [0] * (n+1)
resultSet = set()

for i in range(len(a)):
    numList[a[i]] += 1
    if(numList[a[i]]==2):
        numSet.add(a[i])

sums = 0
for i in numSet:
    ncrList[i] = ncr(numList[i], 2)
    sums += ncrList[i]

for i in range(len(a)):
    if(a[i] in resultSet):
        print(resultList[a[i]])
        continue
    s = sums
    if(a[i] in numSet):
        if(numList[a[i]] == 2):
            s -= 1
        else:
            s -= ncrList[a[i]]
            s += ncr(numList[a[i]]-1, 2)
    print(s)
    resultList[a[i]] = s
    resultSet.add(a[i])

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
ma = {}
for l in li:
    if l in ma:
        ma[l] += 1
    else:
        ma[l] = 1
all_sum = 0
for m in ma.values():
    if all_sum == 0 and m >= 2:
        all_sum = m*(m-1)//2
    elif m >= 2:
        all_sum = all_sum + m*(m-1)//2
for l in li:
    if ma[l] >=2:
        tmp_sum = all_sum - (ma[l]*(ma[l]-1)//2)
        if ma[l] >=3:
            tmp_sum += ((ma[l]-1)*(ma[l]-2)//2)
        print(tmp_sum)
    else:
        print(all_sum)

Solution (Python):

def select2( val ):
  return( val*(val-1) // 2 ) 

N = int(input())
elms = input().split(" ")


ielms = []
for elm in elms:
  ielms.append( int(elm ))



test_dict = {}

for ielm in ielms:
  if( ielm in test_dict.keys() ):
    test_dict[ielm] += 1
  else:
    test_dict[ielm] = 1

total = 0
for dkey in test_dict.keys():      
  total += select2( test_dict[dkey] )
      
for ielm in ielms:
  print(total - select2( test_dict[ielm] ) + select2( test_dict[ielm]-1 ))

Solution (Python):
def nc2(n):
	if n<2:
		return 0
	else:
		return (n * (n-1))//2
n = int(input())
li = list(map(int, input().split()))
s=0; 
d2={}
import collections as cl
d = cl.Counter(li)
for k, v in d.items():
	d2[k] = [nc2(v), nc2(v-1)]
	for v in d2[k]:
		s+=v
		break

for i in li:
	print(s-(d2[i][0])+d2[i][1])

Solution (Python):
import collections
temp = 0
n = input()
li = list(map(int,input().split()))
c = collections.Counter(li)
c_items = list(c.items())
for j in c_items:
    temp += ((j[1] * (j[1]-1)) // 2)
for i in li:
    print(temp - (c[i]-1))

Solution (Python):
from collections import Counter
import sys

sys.setrecursionlimit(10**6)

def fn(n):
    if n <= 1:
        return 0
    return n - 1 + fn(n-1)

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

c = Counter(A)
kumi = 0
for k, v in c.items():
    kumi += fn(v)

for ai in A:
    print(kumi - (c[ai] - 1))

Solution (Python):
from operator import mul
from functools import reduce
def cmb(n,r):
    r=min(n-r,r)
    if r==0:
        return 1
    over=reduce(mul,range(n,n-r,-1))
    under=reduce(mul,range(1,r+1))
    return over//under

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

data=[0]*(N+1)
for a in A:
    data[a]+=1

num=0
for i in range(N+1):
    if data[i]>=2:
        num+=cmb(data[i],2)
        
for k in range(N):
    if data[A[k]]>=2:
        print(num-data[A[k]]+1)
    else:
        print(num)

Solution (Python):
A = input()
B = list(input().split())
import collections
C = collections.Counter(B)
list_after = {}
list_n = C.items()
total = 0
for i in list_n:
    total += i[1] * (i[1]-1) / 2
for i in list_n:
    list_after[i[0]] = total - (i[1] * (i[1]-1) / 2 - (i[1]-1) * (i[1]-2) / 2)
for i in B:
    print(int(list_after[i]))

Solution (Python):
import math
 


def nC2(i):
    if i<=1:
        return 0
    return i*(i-1)//2

def compute(count):
    total = 0
    for i in count:
        total += nC2(i)
    return total


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

count = [0] * (N+1)

for value in data:
    count[value] += 1
    
    
total = compute(count)    

for value in data:
    remove = nC2(count[value])
    add = nC2(count[value]-1)

    print(total-remove+add)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
ans = 0
ansl = 0
d = {}
if len(l) == len(set(l)):
  for i in range(len(l)):
    print(0)
else:
  for s in l:
    if s not in d:
      d[s] = 1
    else:
      d[s] += 1
  for j,k in d.items():
    ans += k*(k-1)//2
  ansl = ans
  for i in range(n):
    ans = ansl
    x = d[l[i]]
    ans -= x - 1
    print(ans)

Solution (Python):
import math
n=int(input())
l=list(map(int,input().split()))
d={}
for i in l:
    if i in d:
        d[i]+=1 
    else:
        d[i]=1 
count=0
for k in d.keys():
        x=d[k]
        if x<2:
            count+=0
        else:
            count+=x*(x-1)//2

for i in l:
    x=d[i]
    
    if x>=2:
        val=x*(x-1)//2
        ans=count-val
        if x>=3:
            x-=1
            val1=x*(x-1)//2
        else:
            val1=0
        ans+=val1
    else:
        if x==2:
            ans=count-1 
        else:
            ans=count 
    print(int(ans))

Solution (Python):
import collections
import math
 
def comb(n, r):
    if n < 2:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
 
 
N = int(input())
L = input().split(" ")
 
c = collections.Counter(L)
 
d = {}
tmp_total = 0 
 
memo1 = {}
L2 = {}
 
for i in c:
    if c[i] not in memo1.keys():
        tmp = comb(c[i], 2)
        memo1[c[i]] = tmp
        tmp_total += tmp
    else:
        tmp_total += memo1[c[i]]
        
total = tmp_total
 
for i in L:
    ans = total - memo1[c[i]] + memo1[c[i]]*(c[i]-2)//c[i]
    print(ans)

Solution (Python):
import collections

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

C = collections.Counter(a)

ans = [0] * (10 ** 6)
cnt = 0

for i, j in C.items():
    cnt += j * (j - 1) // 2



for i in range(len(a)):
    if C[a[i]] > 0:
        print(cnt - (C[a[i]]-1))
    else:
        print(cnt)

Solution (Python):
import collections
n = int(input())
a = list(map(str,input().split()))
c = collections.Counter(a)
all_answer = 0
for _,b in c.items():
	all_answer += b*(b-1)//2
for i in a:
	b = c[i]
	print(all_answer-b*(b-1)//2+(b-1)*(b-2)//2)

Solution (Python):
from operator import mul
from functools import reduce
import copy
def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n = int(input())
A = list(map(int,input().split()))
aset = set(A)
aDict = {}
for aName in aset:
    aDict.update({aName:0})
for a in A:
    aDict[a] += 1

combNumDict = {}
sum = 0
for i in aset:
    if aDict[i] == 0 or aDict[i] == 1:
        tmpsum = 0
    else:
        tmpsum = cmb(aDict[i],2)
    combNumDict.update({i:tmpsum})
    sum += tmpsum
for i in range(n):
    k = A[i]
    if aDict[k] == 1:
        print(sum)
    else:
        if aDict[k] == 2:
            tmp = 1
        else:
            tmp = cmb(aDict[k],2)-cmb(aDict[k]-1,2)
        print(sum-tmp)

Solution (Python):
n = int(input())
list = list(map(int,input().split()))
di = {}
for i in range(n):
	k = list[i]
	if k not in di:
		di[k]=1
	else:
		di[k]+=1
"""
list_del=[]
for key in di.keys():
	if di[key]==1:
		list_del.append(key)
for i in range(len(list_del)):
	di.pop(list_del[i])
"""
	
su=0
for valu in di.values():
	su += valu*(valu-1)/2
	
for i in range(n):
	k = list[i]
	l = di[k]
	su_ch = su
	su_ch -= l*(l-1)/2
	su_ch += (l-1)*(l-2)/2
	print(int(su_ch))

Solution (Python):
from collections import defaultdict
import math
n = int(input())
a = [int(x) for x in input().split()]
ans = []
tmp_sum = 0
m_dic,c_dic = defaultdict(int),defaultdict(int)
for x in a:
    m_dic[x] += 1

for value in m_dic.values():
    if value > 1:
        tmp_sum += math.factorial(value) // (math.factorial(value - 2) * math.factorial(2))

for x in a:
    print(tmp_sum-m_dic[x]+1)

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

from collections import Counter
myc=Counter(al)
val=list(myc.values())
key=list(myc.keys())
val2=list(map(lambda x:x-1,val))

nc2=[0]*n
nc22=[0]*n

for i in range(len(key)):
    p=val[i]
    q=val2[i]
    if q>=2:
        nc2[key[i]-1]=p*(p-1)//2
        nc22[key[i]-1]=q*(q-1)//2
    elif q==1:
        nc2[key[i]-1]=1
        nc22[key[i]-1]=0
    else:
        pass

sum1=sum(nc2)

for i in range(n):
    j=al[i]-1
    print(sum1-nc2[j]+nc22[j])

Solution (Python):



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


cur_dic = {}
cur_sum = 0
for a in a_list:
    if a in cur_dic:
        cur_dic[a] += 1
    else:
        cur_dic[a] = 1

for c in cur_dic:
    num = cur_dic[c]
    cur_sum += num * (num-1) / 2

for i in range(len(a_list)):
    a = a_list[i]
    d = cur_dic[a]
    
    if d > 1:
        cur_sum -= d * (d-1) / 2
    cur_dic[a] -= 1

    
    tmp = cur_sum
    if cur_dic[a] > 1:
        tmp = cur_sum + cur_dic[a] * (cur_dic[a] - 1) / 2 
    print("%d"%(tmp))

    
    if d > 1:
        cur_sum += d * (d-1) / 2
    cur_dic[a] += 1

Solution (Python):
import sys
import collections

n = int(input())
a = list(map(int, sys.stdin.readline().split()))
c = collections.Counter(a)
ans = [0 for i in range(sorted(c.keys())[-1]+1)]

for i in c.keys():
    ans[i] = c[i]*(c[i]-1)//2

sans = sum(ans)
for i in a:
    print(sans - ans[i] + ans[i]*(c[i]-2)//c[i])