2
3
4
4
5
[0][1][2][3][4]
Brute force
▸1sort arr2count ← 03for i ← 0 to n − 3:4 for j ← i + 1 to n − 2:5 for k ← j + 1 to n − 1:6 if arr[i] + arr[j] > arr[k]:7 count++8return count
state
- n5
warming up the animation
Given an array of non-negative integers representing side lengths, count how many triplets can form a valid triangle.
▸1sort arr2count ← 03for i ← 0 to n − 3:4 for j ← i + 1 to n − 2:5 for k ← j + 1 to n − 1:6 if arr[i] + arr[j] > arr[k]:7 count++8return count
Count triples (i, j, k) with i < j < k that form a valid triangle.