Determine Count of Good Triplets

You are given two arrays ARR1 and ARR2, containing N and M elements respectively. There are two types of 'good triplets' that need to be identified in these arrays.

Type 1: A triplet (i, j, k) where the square of ARR1[i] equals the product of ARR2[j] and ARR2[k]. Indices follow the condition 0 ≤ i < N and 0 ≤ j < k < M.

Type 2: A triplet (i, j, k) where the square of ARR2[i] equals the product of ARR1[j] and ARR1[k]. Indices follow the condition 0 ≤ i < M and 0 ≤ j < k < N.

Your task is to calculate the total number of such 'good triplets' for each test case.

Input:

The first line of input consists of an integer T, representing the number of test cases. For each test case, the input is structured as follows: 
  
  • The first line contains two space-separated integers, N and M, indicating sizes of ARR1 and ARR2 respectively.
  • The second line contains N space-separated integers representing the elements of ARR1.
  • The third line contains M space-separated integers representing the elements of ARR2.

Output:

For each test case, output a single integer on a new line, representing the total number of good triplets found according to the conditions specified. Each test case's output should be on a separate line.

Example:

Input:
1
2 3
3 4
1 2 4
Output:
3

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 100
  • 1 ≤ M ≤ 100
  • 1 ≤ ARR1[i] ≤ 10^4
  • 1 ≤ ARR2[i] ≤ 10^4
  • Time limit: 1 second

Note:

You do not need to print anything explicitly. Your task is to implement the given function and return the results as specified.

AnswerBot
4mo

Calculate the total number of 'good triplets' in two arrays based on given conditions.

  • Iterate through all possible triplets in both arrays and check if they satisfy the conditions

  • Use nested loops to i...read more

Help your peers!
Select
Add answer anonymously...

LinkedIn Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Split the String Problem Statement You are given a string str consisting of N lo...read more
A Software Developer Intern was asked Q. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Software Developer Intern was asked Q. Check if Two Trees are Mirror Given two arbitrary binary trees, your task is to ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Split the String Problem Statement You are given a string str consisting of N lo...read more
A Software Developer Intern was asked Q2. Snake and Ladder Problem Statement Given a 'Snake and Ladder' board with N rows ...read more
A Software Developer Intern was asked Q3. Check if Two Trees are Mirror Given two arbitrary binary trees, your task is to ...read more
LinkedIn Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits