27. Count the Number of Possible Triangles
β GFG solution to count possible triangles problem: find number of valid triangles using triangle inequality theorem with efficient two-pointer technique. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [4, 6, 3, 7]
Output: 3
Explanation: There are three triangles possible [3, 4, 6], [4, 6, 7] and [3, 6, 7].
Note that [3, 4, 7] is not a possible triangle since 3 + 4 = 7, which violates the triangle inequality.Example 2
Input: arr[] = [10, 21, 22, 100, 101, 200, 300]
Output: 6
Explanation: There can be 6 possible triangles: [10, 21, 22], [21, 100, 101], [22, 100, 101],
[10, 100, 101], [100, 101, 200] and [101, 200, 300].Example 3
π Constraints
β
My Approach
Sorting + Two Pointers (Backward Iteration)
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated