Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

611. Valid Triangle Number

Medium51.6% Acceptance
ArrayTwo PointersBinary Search
Asked by:
L
LinkedIn
ProblemSolutions (12)VideosCompanies (3)Notes

Problem Statement

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.

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

Approach

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
B
Bloomberg

In #611 Valid Triangle Number, the task is to count how many triplets in an array can form a valid triangle. A triangle is valid when the sum of any two sides is greater than the third side. A brute force approach would check all triplets, but this leads to O(n^3) time and is inefficient for larger inputs.

A more optimal strategy begins by sorting the array. Once sorted, you can fix the largest side and use the two-pointer technique to find pairs that satisfy the triangle inequality. Because the array is ordered, when a valid pair is found, multiple combinations can be counted at once, significantly reducing checks.

Another alternative uses binary search to locate the farthest index that satisfies the triangle condition for each pair. However, the two-pointer approach is typically more efficient in practice. Sorting simplifies comparisons and enables structured traversal, resulting in a much faster solution.

This optimized approach balances readability and performance while reducing unnecessary combinations.

Complexity

ApproachTime ComplexitySpace Complexity
Sorting + Two PointersO(n^2)O(1) (excluding sort space)
Sorting + Binary SearchO(n^2 log n)O(1)
Brute Force (Check all triplets)O(n^3)O(1)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Two-Pointer Technique

First, sort the array. Then, iterate through the array by selecting the third side of the triplet. Use a two-pointer technique to find valid pairs for the first and second sides. For each third side, initialize two pointers, one starting at the beginning of the list and the other just before the third side. Check the sum of the elements at the two pointers and compare it to the third side. If they form a valid triangle, move the inner pointer inward; otherwise, adjust the pointers accordingly.

Time Complexity: O(n^2), where n is the length of the input array because the main operations involve sorting and a nested loop.
Space Complexity: O(1) since it uses only a fixed amount of extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int compare(const void *a, const void *b) {


Explanation

This solution sorts the array and uses nested loops; the outer loop selects the third side, and the inner loop uses two pointers to find valid pairs for the other two sides. By moving the pointers towards each other based on the condition, it efficiently finds all valid triplets.

Brute Force Approach

This approach involves checking every possible triplet from the array to see if they can form a triangle by comparing their sums directly. This solution is straightforward but less efficient since it considers all combinations without optimizations such as sorting or double pointers.

Time Complexity: O(n^3), as it checks every combination of three numbers in the array.
Space Complexity: O(1), requiring minimal additional storage.

CC++JavaPythonC#JavaScript
1#


Video Solutions

Watch expert explanations and walkthroughs

Triangle - Dynamic Programming made Easy - Leetcode 120

NeetCode
15:0656,666 views

Asked By Companies

3 companies
L
LinkedIn
A
Amazon
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersBinary SearchGreedySorting

Problem Stats

Acceptance Rate51.6%
DifficultyMedium
Companies3

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why do we sort the array in Valid Triangle Number?

Sorting helps enforce the triangle inequality condition efficiently. When the array is sorted, you can easily determine whether two smaller sides can form a triangle with a larger side and avoid unnecessary checks.

Can binary search be used in Valid Triangle Number?

Yes, binary search can be used after sorting the array to find the largest valid third side for a given pair. This approach works but usually results in O(n^2 log n) complexity, which is slightly slower than the two-pointer method.

Is Valid Triangle Number asked in coding interviews?

Yes, this problem is commonly discussed in coding interviews because it tests understanding of sorting, two-pointer techniques, and array traversal optimizations. Variants of the triangle inequality problem appear in interviews at major tech companies.

What is the optimal approach for Valid Triangle Number?

The optimal approach is to first sort the array and then use a two-pointer technique. By fixing the largest side and scanning the remaining elements with two pointers, you can count multiple valid combinations efficiently in O(n^2) time.

5
return
*
(
int
*
)
a
-
*
(
int
*
)
b
;
6
}
7
8
int
triangleNumber
(
int
*
nums
,
int
numsSize
)
{
9
if
(
numsSize
<
3
)
return
0
;
10
qsort
(
nums
,
numsSize
,
sizeof
(
int
)
,
compare
)
;
11
int
count
=
0
;
12
for
(
int
i
=
numsSize
-
1
;
i
>=
2
;
i
--
)
{
13
int
left
=
0
,
right
=
i
-
1
;
14
while
(
left
<
right
)
{
15
if
(
nums
[
left
]
+
nums
[
right
]
>
nums
[
i
]
)
{
16
count
+=
right
-
left
;
17
right
--
;
18
}
else
{
19
left
++
;
20
}
21
}
22
}
23
return
count
;
24
}
25
26
int
main
(
)
{
27
int
nums
[
]
=
{
2
,
2
,
3
,
4
}
;
28
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
29
printf
(
"%d\n"
,
triangleNumber
(
nums
,
size
)
)
;
30
return
0
;
31
}
include
<stdio.h>
2
#
include
<stdbool.h>
3
4
bool
isValidTriangle
(
int
a
,
int
b
,
int
c
)
{
5
return
a
+
b
>
c
&&
a
+
c
>
b
&&
b
+
c
>
a
;
6
}
7
8
int
triangleNumber
(
int
*
nums
,
int
numsSize
)
{
9
int
count
=
0
;
10
for
(
int
i
=
0
;
i
<
numsSize
-
2
;
i
++
)
{
11
for
(
int
j
=
i
+
1
;
j
<
numsSize
-
1
;
j
++
)
{
12
for
(
int
k
=
j
+
1
;
k
<
numsSize
;
k
++
)
{
13
if
(
isValidTriangle
(
nums
[
i
]
,
nums
[
j
]
,
nums
[
k
]
)
)
count
++
;
14
}
15
}
16
}
17
return
count
;
18
}
19
20
int
main
(
)
{
21
int
nums
[
]
=
{
2
,
2
,
3
,
4
}
;
22
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
23
printf
(
"%d\n"
,
triangleNumber
(
nums
,
size
)
)
;
24
return
0
;
25
}

Explanation

This Brute Force C solution uses three nested loops to examine every triplet and checks if they satisfy the triangle inequality condition.