Harmonic Triplets
Practice
3.8 (6 votes)
Binary search
Medium
Range queries
Segment trees
Sets
Problem
84% Success 3203 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an array A. A triplet is said to be a Harmonic triplet if it satisfies the following conditions :

  • \(i \lt j \lt k\)
  • \(A[i] \le A[j] \ge A[k]\)


You have to find the \(harmonic\) \( triplet\) with the maximum value of \(A[i] \times A[j] \times A[k]\).
You are given q queries, where in each query you are given \(j^{th}\) index and you have to find the \(harmonic\) \(triplet\) with value at \(j^{th}\) index which has maximum product.

Note
If there are no elements to the left or right of j which satisfy the given conditions, answer for the given index will be 0.

Input format

  • The first line contains single integer T denoting the number of test cases. T test cases follow.
  • The first line of each test case consists of two space-separated integers \(N, Q\) denoting size of the array and number of the queries respectively.
  • Next line contains N spaced integers denoting contents of the array.
  • The next Q lines contain values of \(j^{th}\) index for which you have to find harmonic triplet with maximum product.

Output format

  • For each test case, for each query print a single line consisting of the maximum possible product for that query.

Constraints

  • \(1 \le T \le 5\)
  • \(1 \le N,Q \le 10^5\)
  • \(1 \le A[i] \le 10^5\)
  • \(1 \le j \le N\)

Note
Use fast I/O techniques.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
12 votes
Tags:
Data StructuresMediumSegment TreesTrees
Points:30
17 votes
Tags:
CombinatoricsData StructuresMathMediumSegment TreesTrees
Points:30
754 votes
Tags:
ApprovedFenwick TreeMathMediumNumber TheorySegment Trees