Count Triples That Can Form a Triangle
Updated
Problem statement
Given an array nums of stick lengths, count the triples of positions i < j < k whose three lengths can be the sides of a triangle.
Three lengths form a triangle when the sum of any two of them is strictly greater than the third. A flat triple, where two lengths add up exactly to the third, does not count. Equal lengths at different positions are different sticks, so they give different triples.
Function
triangleCount(nums: int[]) → intExamples
Example 1
nums = [6,4,9,3]return = 2The triples are (6,4,9): 4 + 6 = 10 > 9, yes; (6,4,3): 3 + 4 = 7 > 6, yes; (6,9,3): 3 + 6 = 9, not greater than 9, no; (4,9,3): 3 + 4 = 7 < 9, no. Two triples work.
Example 2
nums = [5,5,5,5]return = 4Any three of the four 5s make a triangle (5 + 5 > 5). There are 4 ways to choose 3 of 4 positions.
Example 3
nums = [0,1,1]return = 0A side of length 0 can never be part of a triangle: 0 + 1 = 1 is not greater than 1.
Constraints
1 <= nums.length <= 10000 <= nums[i] <= 10^6
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Count Winning Dice Face PairsGoogle · PHONE SCREENGoogle interview questions
- Count Centaur Friend RequestsMeta · PHONE SCREENMeta interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- 3SumAmazon · PHONE SCREEN
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Meeting Rooms IIUber · PHONE SCREENUber interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Find K Closest Elements in a Sorted ArrayNavi · PHONE SCREEN
- Sort ColorsVisa · PHONE SCREENVisa interview questions