Valid Triangle Number
Problem Description
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.
Examples
Example 1:
Input: nums = [2,2,3,4]
Output: 3
Explanation: Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3
Example 2:
Input: nums = [4,2,3,4]
Output: 4
Constraints
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
Solution for Valid Triangle Number
Approach 1 Brute Force [Time Limit Exceeded]
The condition for the triplets (a,b,c) representing the lengths of the sides of a triangle, to form a valid triangle, is that the sum of any two sides should always be greater than the third side alone. i.e. a+b>c, b+c>a, a+c>b.
The simplest method to check this is to consider every possible triplet in the given nums array and checking if the triplet satisfies the three inequalities mentioned above. Thus, we can keep a track of the count of the number of triplets satisfying these inequalities. When all the triplets have been considered, the count gives the required result.
Caution: The brute force approach is included here because it is an intuitive way to approach this problem. However, when there are numbers, the if statement will be checked approximately times. Thus, this approach will result in TLE. In the following approaches we will discuss ways to optimize our solution.
Code in Different Languages
- C++
- Java
- Python
#include <vector>
class Solution {
public:
int triangleNumber(std::vector<int>& nums) {
int count = 0;
for (size_t i = 0; i < nums.size() - 2; i++) {
for (size_t j = i + 1; j < nums.size() - 1; j++) {
for (size_t k = j + 1; k < nums.size(); k++) {
if (nums[i] + nums[j] > nums[k] &&
nums[i] + nums[k] > nums[j] &&
nums[j] + nums[k] > nums[i]) {
count++;
}
}
}
}
return count;
}
};
class Solution {
public int triangleNumber(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length - 2; i++) {
for (int j = i + 1; j < nums.length - 1; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] + nums[j] > nums[k] &&
nums[i] + nums[k] > nums[j] &&
nums[j] + nums[k] > nums[i]) {
count++;
}
}
}
}
return count;
}
}
class Solution:
def triangleNumber(self, nums):
count = 0
for i in range(len(nums) - 2):
for j in range(i + 1, len(nums) - 1):
for k in range(j + 1, len(nums)):
if nums[i] + nums[j] > nums[k] and \
nums[i] + nums[k] > nums[j] and \
nums[j] + nums[k] > nums[i]:
count += 1
return count
Complexity Analysis
Time Complexity:
Reason: Three nested loops are there to check every triplet.
Space Complexity:
Reason: Constant space is used.
Approach 2 Using Binary Search
Algorithm
If we sort the given nums
array once, we can solve the problem more efficiently. This is because if we consider a triplet (a, b, c)
such that , we need not check all three inequalities for the validity of the triangle formed by them. Only the condition is necessary. This happens because and . Thus, adding any number to c will always produce a sum greater than either a or b considered alone. Therefore, the inequalities and are satisfied implicitly by the property .
From this, we get the idea that we can sort the given nums
array. Then, for every pair (nums[i], nums[j])
considered from the beginning of the array, such that (leading to , we can find the count of elements nums[k]
(where ), which satisfy the inequality . We can do this for every pair (i, j) considered to get the required result.
We can also observe that, since we've sorted the nums
array, as we traverse towards the right for choosing the index k
(for number nums[k]
), the value of nums[k]
could increase or remain the same (doesn't decrease relative to the previous value). Thus, there will exist a right limit on the value of index k
, such that the elements satisfy . Any elements beyond this value of k
won't satisfy this inequality as well, which is obvious.
Thus, if we can find this right limit value of k
(indicating the element just greater than ), we can conclude that all the elements in the nums
array in the range (both included) satisfy the required inequality. Thus, the count of elements satisfying the inequality will be given by .
Since the nums
array has been sorted, we can use Binary Search to find this right limit of k
.
Another point to be observed is that once we find a right limit index for a particular pair (i,j) chosen, when we choose a higher value of j for the same value of i, we need not start searching for the right limit from the index j+2j. Instead, we can start off from the index directly where we left off for the last j chosen.
This holds correct because when we choose a higher value of j(higher or equal nums[j] than the previous one), all the nums[k], such that will obviously satisfy nums[i]+nums[j]>nums[k] for the new value of j chosen.
By taking advantage of this observation, we can limit the range of Binary Search for k to shorter values for increasing values of j considered while choosing the pairs (i,j).
Code in Different Languages
- Java
- Python
public class Solution {
int binarySearch(int nums[], int l, int r, int x) {
while (r >= l && r < nums.length) {
int mid = (l + r) / 2;
if (nums[mid] >= x)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
public int triangleNumber(int[] nums) {
int count = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int k = i + 2;
for (int j = i + 1; j < nums.length - 1 && nums[i] != 0; j++) {
k = binarySearch(nums, k, nums.length - 1, nums[i] + nums[j]);
count += k - j - 1;
}
}
return count;
}
}
class Solution:
def binary_search(self, nums, l, r, x):
while r >= l and r < len(nums):
mid = (l + r) // 2
if nums[mid] >= x:
r = mid - 1
else:
l = mid + 1
return l
def triangleNumber(self, nums):
count = 0
nums.sort()
for i in range(len(nums) - 2):
k = i + 2
for j in range(i + 1, len(nums) - 1):
if nums[i] == 0:
continue
k = self.binary_search(nums, k, len(nums) - 1, nums[i] + nums[j])
count += k - j - 1
return count
Complexity Analysis
Time Complexity:
Reason: In worst case inner loop will take nlogn (binary search applied N times).
Space Complexity:
Reason: Sorting takes O(logn) space.
References
-
LeetCode Problem: Valid Triangle Number
-
Solution Link: Valid Triangle Number