
Valid Triangle Number (medium)
Problem Statement
Given an array of integers nums, return the number of triplets we can make using array elements that can create triangle if we take them as side lengths of a triangle.
A set of three numbers can constitute a triangle if and only if the sum of any two sides is greater than the third side. This condition must hold true for all three combinations of summed sides.
Examples
-
Example 1:
- Input:
nums = [4, 2, 3, 1] - Expected Output:
1 - Justification: The valid combinations that can form a triangle are (2, 3, 4). The other combinations either do not meet the triangle inequality theorem or are duplicates in a different order.
- Input:
-
Example 2:
- Input:
nums = [5, 1, 3, 4, 7] - Expected Output:
3 - Justification: There are 3 combinations that meet the triangle condition, such as (3, 4, 5), (3, 5, 7), (4, 5, 7) that satisfy the triangle inequality.
- Input:
-
Example 3:
- Input:
nums = [10, 21, 22, 100, 101, 200, 300] - Expected Output:
6 - Justification: There are 6 combinations that meet the triangle condition, such as (10, 21, 22), (10, 100, 101), (21, 100, 101), (22, 100, 101), (100, 101, 200), and (101, 200, 300) that satisfy the triangle inequality.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory