def countTriplets(arr, n): freq = [0 for i in range(100)] # Loop to count the frequency for i in range(n): freq[arr[i]]

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

def countTriplets(arr, n): freq = [0 for i in range(100)] # Loop to count the frequency for i in range(n): freq[arr[i]]

Post by answerhappygod »

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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply