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



Sample input:
5
1 1 2 1 2


Sample output:
2
2
3
2
3

Submission status: Time Limit Exceeded

Solution code:
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
for i in range(N):
  tmp = A.count(i + 1)
  total += tmp * (tmp -1) // 2
  number[i] = tmp
for i in range(N):
  number[i] = total - number[i] + 1
for j in A:
  print(number[j-1])

Submission status: Time Limit Exceeded

Solution code:
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
for i in range(N):
  tmp = A.count(i + 1)
  total += tmp * (tmp -1) // 2
  number[i] = tmp
A = np.array(A)
for j in range(N):
  print(total - number[A[j]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
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)
for i in range(N):
  number[i] = A.count(i + 1)
total = 0
for k in range(N):
  tmp = number[k]
  total += tmp * (tmp-1) // 2
for j in range(N):
  print(total - number[A[j]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
number = np.zeros(N, dtype = int)
for i in range(N):
  number[A[i]-1] += 1
total = 0
for k in range(N):
  tmp = number[k]
  total += tmp * (tmp-1) // 2
for j in range(N):
  print(total - number[A[j]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
number = np.zeros(N, dtype = int)
for i in range(N):
  number[A[i]-1] += 1
total = 0
for k in range(N):
  total += number[k] * (number[k]-1) // 2
for j in range(N):
  print(total - number[A[j]-1] + 1)

Submission status: Time Limit Exceeded

Solution code:
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

for i in range(n):
  sum = 0
  for k,v in dic.items():
    if an[i] == k:
      sum += (v-1)*(v-2)//2
    else:
      sum += v*(v-1)//2
  print(sum)

Submission status: Time Limit Exceeded

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

for i in range(N):
  c[i] = A.count(i+1)

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

for i in range(N):
  if c[A[i]-1] > 1:
    print(p-(c[A[i]-1]-1))
  else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
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()))
    
    c = Counter(A)
    
    for ball in A:
        res = 0
        for num in c:
            res += comb2(c[num] - int(num == ball))
        print(res)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
def com_cnt(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
A = list(map(int,input().split()))
l = [0]*(N+1)
for a in A:
    l[a] += 1
for i in A:
    ans = 0
    l[i]-=1
    for j in l:
        if j>1:
            ans+=com_cnt(j, 2)
    print(ans)
    l[i]+=1

Submission status: Time Limit Exceeded

Solution code:
from math import factorial
from collections import defaultdict


ball_num = int(input().rstrip())
points = [int(n) for n in input().rstrip().split()]

for ban_i in range(ball_num):
    point_dict=defaultdict(int)
    pattern_num=0
    for i in range(ball_num):
        if i==ban_i:
            continue

        point_dict[points[i]]+=1
        
    for j in point_dict:
        if point_dict[j]>=2:
            
            pattern_num += factorial(point_dict[j])//(2*factorial(point_dict[j]-2))
            
        
    print(pattern_num)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# number of balls
N = input()
# ball values
K = list(map(int, input().split()))

# get ball counts
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

for i in range(len(K)):
    ball_value = K[i]
    two_equal_values_count = 0
    for value, count in ball_counts.items():
        # check if we can even come up with a pair
        if count > 1:
            if value == ball_value:
                # if we have three balls [1, 1, 1] and the current ball is 1, we want to remove our current 1 and only look at [1, 1]
                two_equal_values_count += (count - 1) * (count - 2) / 2
            else:
                two_equal_values_count += count * (count - 1) / 2

    print(int(two_equal_values_count))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lis = list(map(int,input().split()))

box = [lis[0]]
num = []

def Cn2(i):
    x = i*(i-1)/2
    return int(x)

#だぶっている数を消したbox作る
for i in range(len(lis)):
    for j in range(len(box)):
        if lis[i] == box[j]:
            break
        elif j == len(box)-1:
            if not lis[i] == box[j]:
                box.append(lis[i])
        else:continue

#boxに入っている数がlisとのダブリ数を数える
for i in range(len(box)):
    a = 0
    for j in range(len(lis)):
        if box[i] == lis[j]:
            a = a + 1
        else:continue
    num.append(a)


for k in range(len(lis)):
    al = 0
    for l in range(len(box)):
        if lis[k] == box[l]:
            lu = l
            num[l] = num[l] - 1

    for i in range(len(num)):
        if num[i] == 0 or num[i] == 1:
            continue
        al = al + Cn2(num[i])

    print(al)
    num[lu] = num[lu] + 1

Submission status: Time Limit Exceeded

Solution code:
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
for i in range(n):
    print(sum(v)-v[inp[i]]+((d[inp[i]]-1)*(d[inp[i]]-2)//2))

Submission status: Time Limit Exceeded

Solution code:
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()

from math import factorial
f={x:factorial(x) for x in range(n+1)}
def comb(n):
    if n<=1:
        return 0
    if n==2:
        return 1
    return f[n]//(factorial(2)*f[n-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))

Submission status: Time Limit Exceeded

Solution code:
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()

from math import factorial
def comb(n):
    if n<=1:
        return 0
    if n==2:
        return 1
    return factorial(n)//(factorial(2)*factorial(n-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))

Submission status: Time Limit Exceeded

Solution code:
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)]

def main():
    n=n0()
    a=n1()

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

    c=[a.count(i) for i in range(1,n+1)]
    d=[comb(m) for m in c]
    s=sum(d)

    for i in range(n):
        print(s-d[a[i]-1]+comb(c[a[i]-1]-1))
if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
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()

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

c=[a.count(i) for i in range(1,n+1)]
d=[comb(m) for m in c]
s=sum(d)

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

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
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()

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

c=[a.count(i) for i in range(1,n+1)]
d=[comb(m) for m in c]

for i in range(n):
    num=d[a[i]-1]
    d[a[i]-1]=comb(c[a[i]-1]-1)
    print(sum(d))
    d[a[i]-1]=num

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
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()

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

c=[a.count(i) for i in range(1,n+1)]
d=[comb(m) for m in c]

for i in range(n):
    num=a[i]-1
    if d[num]>1:
        e=d.copy()
        e[num]=d[num]*(c[num]-2)//c[num]
        print(sum(e))
    elif d[num]==1:
        print(sum(d)-1)
    else:
        print(sum(d))

Submission status: Time Limit Exceeded

Solution code:
L = int(input())
a = [int(i) for i in input().split()]

from functools import lru_cache

@lru_cache(maxsize=1000)
def h():
  for k in range(L):
    b = a[0:k] + a[(k+1):L]
    sum = 0
    for c in list(set(b)):
      d = int(b.count(c))
      if d != 1:
        sum += d*(d-1)/2
    print(int(sum))
h()

Submission status: Time Limit Exceeded

Solution code:
L = int(input())
a = [int(i) for i in input().split()]

for k in range(L):
  b = a[0:k] + a[(k+1):L]
  sum = 0
  for c in list(set(b)):
    d = int(b.count(c))
    if d != 1:
      sum += d*(d-1)/2
  print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

for k in range(n):  
    freq = Counter(a)
    freq[a[k]]-=1
    total = 0
  
    for key,val in freq.items():
        if( val>1):
            total+= (val*(val-1))//2
        
    print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = input().split()
add=''
for k in range(n):
    if(add!=''):
        a.insert(k-1,add)
    add=a.pop(k)
  
    freq = Counter(a)
    total = 0
  
    for key,val in freq.items():
        if( val>1):
            total+= (val*(val-1))//2
        
    print(total)

Submission status: Time Limit Exceeded

Solution code:
def choose2(number):
	if number>=2:
		return (number*(number-1))/2
	else:
		return 0
N = int(input())
klist = input()
k = []
for x in range(N-1):
	k.append(int(klist[:klist.index(" ")]))
	klist = klist[klist.index(" ")+1:]
k.append(int(klist))
valArr = list(set(k))
occurence = []
for i in range(len(valArr)):
	occurence.append(0)
for j in range(N):
	for x in range(len(valArr)):
		if k[j]==valArr[x]:
			occurence[x]+=1
for ball in range(len(k)):
	ans = 0
	for time in range(len(occurence)):
		if k[ball]==valArr[time]:
			ans+=choose2(occurence[time]-1)
		else:
			ans+=choose2(occurence[time])
	print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
				
def factorial(number):
	if number>2:
		return number*factorial(number-1)
	elif not number==0:
		return number
	else:
		return 1
def choose2(number):
	if number>=2:
		return factorial(number)/(2*factorial(number-2))
	else:
		return 0
N = int(input())
klist = input()
k = []
for x in range(N-1):
	k.append(int(klist[:klist.index(" ")]))
	klist = klist[klist.index(" ")+1:]
k.append(int(klist))
valArr = list(set(k))
occurence = []
for i in range(len(valArr)):
	occurence.append(0)
for j in range(N):
	for x in range(len(valArr)):
		if k[j]==valArr[x]:
			occurence[x]+=1
for ball in range(len(k)):
	ans = 0
	kArray = occurence[:len(occurence)]
	for index in range(len(valArr)):
		if valArr[index]==k[ball]:
			kArray[index]-=1
			break;
	for time in kArray:
		ans+=choose2(time)
	print(int(ans))

Submission status: Time Limit Exceeded

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

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


for i in range(N):
	an = A[i]
	count=Counter(A)
	count[an]-=1
	ans=0
	for j in count.items():
		ans+=ch_2(j[1])
	print(int(ans))

Submission status: Time Limit Exceeded

Solution code:

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

Submission status: Time Limit Exceeded

Solution code:

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

Submission status: Time Limit Exceeded

Solution code:
				
def factorial(number):
	if number>2:
		return number*factorial(number-1)
	elif not number==0:
		return number
	else:
		return 1
def choose2(number):
	if number>=2:
		return factorial(number)/(2*factorial(number-2))
	else:
		return 0
N = int(input())
klist = input()
k = []
for x in range(N-1):
	k.append(int(klist[:klist.index(" ")]))
	klist = klist[klist.index(" ")+1:]
k.append(int(klist))
valArr = []
occurence = []
for j in range(N):
	if len(valArr)>0:
		if not k[j] in valArr:
			valArr.append(k[j])
			occurence.append(1)
		else:
			for index in range(len(valArr)):
				if valArr[index]==k[j]:
					break;
			occurence[index]+=1
	else:
		valArr.append(k[j])
		occurence.append(1)
for ball in range(len(k)):
	ans = 0
	kArray = occurence[:len(occurence)]
	for index in range(len(valArr)):
		if valArr[index]==k[ball]:
			kArray[index]-=1
			break;
	for time in kArray:
		ans+=choose2(time)
	print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
N = int(input())
A= list(map(int, input().split()))
for i in range(N):
    f=0
    a=copy(A)
    a.remove(a[i])
    l=len(a)
    for j in range(l):
        for k in range(j+1,l):
            if a[j]==a[k]:
                f+=1
    print(f)

Submission status: Time Limit Exceeded

Solution code:
inp = [input() for i in range(2)]
N = int(inp[0])
A = list(map(int, inp[1].split()))
B = 0
for k in A:
    a = A.count(k)
    B += (a - 1) / 2

for k in range(N):
    c = A.count(A[k]) - 1
    counter = B - c
    print(int(counter))

Submission status: Time Limit Exceeded

Solution code:
inp = [input() for i in range(2)]
N = int(inp[0])
A = inp[1].split()
A = [int(i) for i in A]
B = 0
for k in A:
    a = A.count(k)
    B += (a - 1) / 2

for k in range(N):
    c = A.count(A[k]) - 1
    counter = B - c
    print(int(counter))

Submission status: Time Limit Exceeded

Solution code:
import collections

inp = [input() for i in range(2)]
N = int(inp[0])
A = inp[1].split()
A = [int(i) for i in A]
B = 0
for k in A:
    a = A.count(k)
    B += (a - 1) / 2

for k in range(N):
    # counter = 0
    # other = A[:k] + A[k+1:]
    # for i in range(N-1):
    #     counter += other.count(other[i])
    #     counter -= 1
    c = A.count(A[k]) - 1
    counter = B - c
    print(int(counter))

Submission status: Time Limit Exceeded

Solution code:
inp = [input() for i in range(2)]
N = int(inp[0])
A = inp[1].split()
A = [int(i) for i in A]

for k in range(N):
    counter = 0
    A_k = A[k]
    other = A[:k] + A[k+1:]
    for i in range(N-1):
        counter += other.count(other[i])
        counter -= 1
    
    print(int(counter/2))

Submission status: Time Limit Exceeded

Solution code:
inp = [input() for i in range(2)]
N = int(inp[0])
A = inp[1].split()
A = [int(i) for i in A]

for k in range(N):
    counter = 0
    A_k = A[k]
    other = A[:k] + A[k+1:]
    for i in range(N-2):
        for j in range(i+1, N-1):
            if other[i] == other[j]:
                counter += 1
    
    print(counter)

Submission status: Time Limit Exceeded

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

ANSWER = []
for i in range(N):
    A_ = []
    for j in range(N):
        if j != i:
            A_.append(A[j])
            
        else: continue

    answer_i = 0
    for j in set(A_):
        answer_i += (A_.count(j) * (A_.count(j)-1)/2)
    
    ANSWER.append(int(answer_i))

for i in ANSWER:
    print(i)

Submission status: Time Limit Exceeded

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

ANSWER = []
for i in range(N):
    A_ = []
    for j in range(N):
        if j != i:
            A_.append(A[j])
            
        else: continue
            
    unique_i = list(set(A_))

    answer_i = 0
    for j in unique_i:
        answer_i += (A_.count(j) * (A_.count(j)-1)/2)
    
    ANSWER.append(int(answer_i))

for i in ANSWER:
    print(i)

Submission status: Time Limit Exceeded

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

ANSWER = []
for i in range(N):
    A_ = A.copy()
    del A_[i]
    unique_i = list(set(A_))

    answer_i = 0
    for j in unique_i:
        answer_i += (A_.count(j) * (A_.count(j)-1)/2)
    
    ANSWER.append(int(answer_i))

for i in ANSWER:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum([(a.count(i)*(a.count(i)-1))//2 for i in set(a)])
for i in a:
  y=a.count(i)-1
  print(max(0,x-y))

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from collections import defaultdict


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

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

dB = defaultdict(int)

for i in A:
    temp = 0
    for j in dA.keys():
        if i == j:
            a = list(range(dA[j] - 1))
            if not dB[len(a)]:
                temp += len(list(combinations(a, 2)))
                dB[len(a)] = len(list(combinations(a, 2)))
            else:
                temp += dB[len(a)]
        else:
            b = list(range(dA[j]))
            if not dB[len(b)]:
                temp += len(list(combinations(b, 2)))
                dB[len(b)] = len(list(combinations(b, 2)))
            else:
                temp += dB[len(b)]
    print(temp)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = int(0)
j = int(0)
B = [-1] * N
C = [-1] * N
sum = int(0)

for i in range(N):
    if B[i] == -1:
        count = int(0)
        for j in range(N):
            if A[i] == A[j]:
                count = count + 1
                B[j] = i
        j = j + 1
        num = count -1
        sum = sum + int(count*(count-1)/2)
        C[i] = num
    else:
        C[i] = C[B[i]]

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = int(0)
j = int(0)
B = [-1]*N
C = []
sum = int(0)

D = set(A)


if len(D) == N:
    for i in range(N):
        print("0")
elif len(D) == 1:
    i = 0
    sum = int((N-1)*(N-2)/2)
    for i in range(N):
        print(sum)
else:
    for i in range(N):
        if B[i] == -1:
            count = int(0)
            j = i
            for j in range(N):
                if A[i]==A[j]:
                    count = count + 1
                    B[j] = i
                j = j + 1
            pair = int(count*(count-1)/2)
            C.append([A[i],pair - int((count-1)*(count-2)/2)])
            sum = sum + pair
        i = i + 1

    i = 0
    L = len(C)

    for i in range(N):
        for j in range(L) :
            if A[i] == C[j][0]:
                print(sum-C[j][1])
                break
            else:
                j = j + 1
        i = i + 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = int(0)
j = int(0)
B = [-1]*N
C = []
sum = int(0)

for i in range(N):
    if B[i] == -1:
        count = int(0)
        j = i
        for j in range(N):
            if A[i]==A[j]:
                count = count + 1
                B[j] = i
            j = j + 1
        pair = int(count*(count-1)/2)
        C.append([A[i],pair - int((count-1)*(count-2)/2)])
        sum = sum + pair
    i = i + 1

i = 0
L = len(C)

for i in range(N):
    for j in range(L) :
        if A[i] == C[j][0]:
            print(sum-C[j][1])
            break
        else:
            j = j + 1
    i = i + 1

Submission status: Time Limit Exceeded

Solution code:
def countSameNumber(Ary):
    Ary.sort()
    c=Ary[0]
    n=0
    res=[]
    for a in Ary:
        if c==a:
            n+=1
        else:
            res+=[n]
            c=a
            n=1
    return res+[n]

combination_memo={}
def combination(A,B):
    global combination_memo
    if B>A:
        return 0
    elif A-B<B:
        return combination(A,A-B)
    elif (A,B) in combination_memo:
        return combination_memo[(A,B)]
    else:
        prodA=1
        prodB=1
        for b in range(1,B+1):
            prodB*=b
            prodA*=A-(b-1)
        res=int(prodA/prodB)
        combination_memo[(A,B)]=res
        return res

N=int(input())
A=[int(i) for i in input().split(" ")]
for k in range(N):
    B=A.copy()
    del B[k]
    print(sum([combination(i,2) for i in countSameNumber(B)]))

Submission status: Time Limit Exceeded

Solution code:
def countSameNumber(Ary):
    Ary.sort()
    c=Ary[0]
    n=0
    res=[]
    for a in Ary:
        if c==a:
            n+=1
        else:
            res+=[n]
            c=a
            n=1
    return res+[n]

def combination(A,B):
    if B>A:
        return 0
    if A-B<B:
        return combination(A,A-B)
    prodA=1
    prodB=1
    for b in range(1,B+1):
        prodB*=b
        prodA*=A-(b-1)
    return int(prodA/prodB)

N=int(input())
A=[int(i) for i in input().split(" ")]
for k in range(N):
    B=A.copy()
    del B[k]
    print(sum([combination(i,2) for i in countSameNumber(B)]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = [i for i in map(int, input().split())]

for k in range(n):
    count = [0 for i in range(n)]
    for i in range(n):
        if i != k:
            count[lis[i]-1] += 1
    ans = 0
    for i in count:
        ans += int(i * (i-1) / 2)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = int(0)
j = int(0)
C = []
B = sorted(A)
sum = int(0)
count = int(1)
for i in range(N):
    if i == N-1:
        pair = int(count*(count-1)/2)
        C.append([B[i],pair - int((count-1)*(count-2)/2)])
        sum = sum + pair
    elif B[i] == B[i + 1]:
        count = count + 1
    else:
        pair = int(count*(count-1)/2)
        C.append([B[i],pair - int((count-1)*(count-2)/2)])
        sum = sum + pair
        count = 1
    i = i + 1

i = 0
L = len(C)
for i in range(N):
    for j in range(L) :
        if A[i] == C[j][0]:
            print(sum-C[j][1])
        else:
            j = j + 1
    i = i + 1

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

def cmb2(n):
	if n < 2:
		return 0

	return n * (n-1) // 2

if __name__ == "__main__":

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

	S = Counter(A)
	for a in A:
		S[a] -= 1
		ans = 0
		for d in S.values():
			ans += cmb2(d)
		print(ans)
		S[a] += 1

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

def cmb2(n):
	result = n * (n - 1) / 2

	return int(result)

if __name__ == "__main__":

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

	S = Counter(A)
	for a in A:
		S[a] -= 1
		ans = 0
		for d in S.values():
			ans += cmb2(d)
		print(ans)
		S[a] += 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = input().split()
y = int(1)

for y in range(N):
    j = int(0)
    count = int(0)
    for j in range(N):
        if not j == y:
            k = int(0)
            for k in range(j):
                if not k == y:
                    if X[j] == X[k]:
                        count = count + 1
                k = k + 1
            j = j + 1
    print(count)
    y = y + 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
X = input().split()
y = int(1)
xnum = len(X)
for y in range(xnum):
    j = int(0)
    count = int(0)
    for j in range(xnum):
        if not j == y:
            k = int(0)
            for k in range(j):
                if not k == y:
                    if X[j] == X[k]:
                        count = count + 1
                k = k + 1
            j = j + 1
    print(count)
    y = y + 1

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = int(1)

def count(X,y):
    j = int(0)
    xnum = len(X)
    count = int(0)
    for j in range(xnum):
        if not j == y:
            k = int(0)
            for k in range(j):
                if not k == y:
                    if X[j] == X[k]:
                        count = count + 1
                k = k + 1
            j = j + 1
    print(count)

for i in range(N):
    count(A,i)
    i = i + 1

Submission status: Time Limit Exceeded

Solution code:
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()))
for i in range(N):
	A_i = A[:i] + A[i+1:]
	setA_i = set(A_i)
	total = 0
	for x in setA_i:
		n = A_i.count(x)
		total += comb(n, 2) if n >= 2 else 0
	print(total)

Submission status: Time Limit Exceeded

Solution code:
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()))
for i in range(N):
	A_i = A[:i] + A[i+1:]
	setA_i = set(A_i)
	total = 0
	for x in setA_i:
		n = A_i.count(x)
		total += comb(n, 2) if n >= 2 else 0
	print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
 
n = int(sys.stdin.readline())
 
li = list(map(int, sys.stdin.readline().split()))
 
ans = [0]*n
for i in range(n):
    li2 = li.copy()
    del li2[i]
    for j in range(n-1):
        for k in range(j+1, n-1):
            if li2[j] == li2[k]:
                ans[i] += 1
for l in range(len(ans)):
    print(ans[l])

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = sorted(a)
l=[1]
m=0
for i in range(n-1):
  if c[i]==c[i+1]:
    l[m]+=1 
  else:
    l.append(1)
    m+=1
x=0
p=int(len(l))
for i in range(p):
  if l[i]>=2:
    x+=l[i]*(l[i]-1)//2
for i in range(n):
  q = a.count(a[i])
  if q>=2:
    print(x-q+1)
  else:
    print(x)

Submission status: Time Limit Exceeded

Solution code:
input()
a=input().split()
b=set(a)
c=[a.count(i) for i in b]
d=list(map(lambda x:x*(x-1)//2,c))
e=[i-1 for i in c]
s=sum(d)
st=""
b=list(b)
for i in a:
    st+=str(s-e[b.index(i)])+"\n"

print(st[:-1])

Submission status: Time Limit Exceeded

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

count = [0] * (N+1)
for i in range(1,N+1):
    for j in range(N):
        if a[j] == i:
            count[i] += 1
            
for i in range(N):
    count_cp = count.copy()
    count_cp[a[i]] -= 1
    su = 0
    for j in range(1,N+1):
        if count_cp[j] > 1:
            su += count_cp[j]*(count_cp[j]-1)/2
    print(int(su))

Submission status: Time Limit Exceeded

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

for h in range(N):
    B = 0
    for i in range(N - 1):
        for j in range(N - i):
            if (A[i] == A[i + j]) and (i != h) and (i + j != h) and (j != 0):
                B = B + 1
    print(B)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ls = list(map(int, input().split()))
ans = [0]*N
for i in range(len(ls)):
    tmp = ls[i]
    ls[i] = 0
    for j in range(len(ls)):
        for k in range(j+1, len(ls)):
            if ls[j] == ls[k]:
                ans[i] += 1
    ls[i] = tmp
for item in ans:
  print(item)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))

length = len(A_list) - 1
for i in range(N):
  num = 0
  a = A_list.pop(i)
  for j in range(length):
    num += A_list[j + 1: length].count(A_list[j])
  A_list.insert(i, a)
  print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A_list = list(map(int, input().split()))

length = len(A_list) - 1
for i in range(N):
  num = 0
  a = A_list.pop(i)
  for j in range(length):
    for A in A_list[j + 1: length]:
      if A_list[j] == A:
        num += 1
  A_list.insert(i, a)
  print(num)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from scipy.misc import comb
from collections import Counter
n = int(input())
a = np.array(list(map(int, input().split())))

def count(a, k):
    mask = np.ones(n, dtype=bool)
    mask[k] = False
    counts = list(Counter(a[mask]).values())
    ans = 0
    for i in range(len(counts)):
        ans+=comb(counts[i], 2, exact=True)
    print(str(ans))
    
for k in range(n):
    count(a, k)