-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTripletSum.py
More file actions
37 lines (26 loc) · 798 Bytes
/
TripletSum.py
File metadata and controls
37 lines (26 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# You have been given a random integer array/list(ARR) and a number X.
# Find and return the number of triplets in the array/list which sum to X.
from sys import stdin
def findTriplet(arr, n, x) :
#Your code goes here
count = 0
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
if arr[i]+arr[j]+arr[k] == x:
count += 1
return count
#Taking Input Using Fast I/O
def takeInput() :
n = int(stdin.readline().strip())
if n == 0 :
return list(), 0
arr = list(map(int, stdin.readline().strip().split(" ")))
return arr, n
#main
t = int(stdin.readline().strip())
while t > 0 :
arr, n = takeInput()
x = int(stdin.readline().strip())
print(findTriplet(arr, n, x))
t -= 1