Maximum Good Triplet Distance
Updated
Problem statement
You are given an integer array A. A triplet of indices (i, j, k) is good when i < j < k and both visibility conditions hold:
- For every index
pwithi < p < j,A[p] < A[i]andA[p] < A[j]. - For every index
qwithj < q < k,A[q] < A[j]andA[q] < A[k].
Return the maximum possible index distance k - i among all good triplets. Return -1 if no good triplet exists.
Function
maximumGoodTripletDistance(A: int[]) → intExamples
Example 1
A = [5,1,4,2,6]return = 4The triplet (0, 2, 4) is good. Value 1 is strictly below both 5 and 4, while value 2 is strictly below both 4 and 6. Its distance is 4 - 0 = 4.
Example 2
A = [3,3,1,3,3]return = 3Equal-height endpoints block visibility through one another because every interior value must be strictly smaller. One maximum-span good triplet is (0, 1, 3), with distance 3.
Constraints
3 <= A.length <= 200000-10^9 <= A[i] <= 10^9
More Google Online Assessment questions
- Book Reading EveningsOA · Seen Nov 2026
- Count Score CategoriesOA · Seen Nov 2026
- Minimum Cars for Rental RequestsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Shortest Path in a Binary MatrixOA · Seen Nov 2026
- Robot Assembly Line ConflictsOA · Seen Oct 2026
- Assign LockerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Biggest Number From DigitsOA · Seen Oct 2026
- Consolidate On-Call RotationsOA · PHONE SCREEN · Seen Oct 2026
- Decreasing SubsequencesOA · Seen Sep 2026
- Minimize Absolute Difference of Server LoadsOA · Seen Sep 2026
Similar questions at other companies
- Maximize Shipment Order by Sorting One WindowAmazon · OA
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Alternating Jumps to the Last IndexExpedia · OAExpedia interview questions
- Pipe Removal TurnsWells Fargo · OAWells Fargo interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Sum of Subarray MinimumsNVIDIA · OANVIDIA interview questions
- Shopkeeper Final Price SummaryMicrosoft · OAMicrosoft interview questions
- Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
- Maximum Capped Histogram Area After One RemovalMyntra · OAMyntra interview questions