Valid Triangle Number

Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Three lengths a <= b <= c form a triangle exactly when a + b > c.

30:00

Valid Triangle Number
medium
Free written solution
Topics
Companies

Given an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Three lengths a <= b <= c form a triangle exactly when a + b > c.

Example 1:
Input: {"nums":[2,2,3,4]}
Output: 3
Constraints:
  • 1≤nums.length≤10001 \leq \text{nums.length} \leq 1000

  • 0≤nums[i]≤10000 \leq \text{nums}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[2,2,3,4]}

These are 4 stick lengths. Three sticks a <= b <= c close into a triangle only when a + b > c.

sticks as given

2

2

3

4

count

0

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/11