Skip to content
Discussion options

You must be logged in to vote

We need to find the number of triplets in an array that can form a triangle. The key property of a triangle is that the sum of any two sides must be greater than the third side.

Approach

  1. Sort the Array: Sorting helps in efficiently checking the triangle condition. After sorting, we can fix the largest side and then check for pairs of smaller sides that satisfy the triangle inequality.
  2. Two Pointers Technique: For each element as the potential largest side (starting from the end of the sorted array), use two pointers to find pairs of smaller sides such that their sum is greater than the largest side. If such a pair is found, all elements between the two pointers will also satisfy the condi…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Sep 26, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Sep 26, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants