Lucky Triples

Medium
array triple-counting multiplication nested-loops
Problem Description

Given an array of positive integers, count the number of triples (i, j, k) where i < j < k and A[i] × A[j] = A[k]. These are called "lucky triples" because the product of two earlier elements equals a later element.

Input Format
First line contains n (array size). Second line contains n space-separated positive integers.
Output Format
Number of lucky triples in the array.
Constraints
• 3 ≤ n ≤ 1000
• 1 ≤ A[i] ≤ 1000
• Indices must satisfy i < j < k
• Count all valid triples
• Handle duplicate values correctly
Sample Input/Output
Input:
5
2 3 6 4 12
Output:
2
Explanation

Lucky triples: (0,1,2) where A[0]×A[1] = 2×3 = 6 = A[2], and (1,3,4) where A[1]×A[3] = 3×4 = 12 = A[4]. Total: 2 triples.

Solution Hints
90
Points
2500ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback