def countTriplets(arr, n):
freq = [0 for i in range(100)]
# Loop to count the frequency for i in range(n): freq[arr] += 1 count = 0
# Loop to count for triplets for i in range(n):
for j in range(i + 1, n, 1): if(freq[arr + arr[j]]): count += 1 return count
# Driver Code
if name n = 4 arr = [1, 5, 3, 2]
# Function Call _main__':
print(countTriplets(arr, n)) Algorithm: Declare a frequency array to store the frequency of the numbers. Iterate over the elements of the array and increment the count of that number in the frequency array. Run two loops to choose two different indexes of the matrix and check if the sum of the elements at those indices has a frequency more than 0 in the frequency array.
Algorithm:
Declare a frequency array to store the frequency of the numbers. Iterate over the elements of the array and increment the count of that number in the frequency array. Run two loops to choose two different indexes of the matrix and check if the sum of the elements at those indices has a frequency more than 0 in the frequency array.
def countTriplets(arr, n): freq = [0 for i in range(100)] # Loop to count the frequency for i in range(n): freq[arr[i]]
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am