2Brute2Best
🔥0

Valid Triangle Number

Medium·LeetCode #611 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an array nums of non-negative integers. Think of each number as the length of a stick.

Count how many ways you can pick three sticks at three different positions that can form a real triangle.

Three lengths a, b, c form a triangle when each pair adds up to more than the third: a + b > c, a + c > b and b + c > a. Equal values at different positions count as different picks.

Input

An integer array nums.

Output

One integer: the number of triplets of positions i < j < k whose values can be the sides of a triangle.

Example 1
Input: nums = [2, 2, 3, 4]
Output: 3

(2, 3, 4) using the first 2, (2, 3, 4) using the second 2, and (2, 2, 3). The triplet (2, 2, 4) fails because 2 + 2 is not more than 4.

Example 2
Input: nums = [4, 2, 3, 4]
Output: 4

(4, 2, 3), (4, 2, 4), (4, 3, 4) and (2, 3, 4) all work.

Constraints
  • • 1 ≤ nums.length ≤ 1000
  • • 0 ≤ nums[i] ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“pick three sticks”
→ Triplets → fix one, two-pointer the other two.
“each pair adds up to more than the third”
→ After sorting, only `smallest two > largest` matters → fix the largest side.
“count how many”
→ We need a count, not a list → one passing check can add a whole block (`j − i`) at once.
03

Approach Complexity Ladder

Brute Force — check every tripletO(N³)O(1)

Three loops; test all three triangle rules

Better — sort, fix two, binary search the thirdO(N² · log N)O(1)

Sort; for each pair, binary search how many third sides fit

Optimal — sort, fix the largest side, two pointersO(N²)O(1)

Sort; fix nums[k] as the largest; i/j squeeze and count j − i at once

Brute Force Intuition

Try every triplet i < j < k and test the three triangle rules. Count the ones that pass.

Recognition rule for this sub-pattern:

Sort, fix one element (or more), and solve the rest as a two-pointer pair problem. Skip duplicates carefully.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.