Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 10000 <= nums[i] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search · Greedy
[2,2,3,4]
[4,2,3,4]
3sum-smaller)find-polygon-with-the-largest-perimeter)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #611: Valid Triangle Number
class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int res = 0;
for (int i = n - 1; i >= 2; --i) {
int l = 0, r = i - 1;
while (l < r) {
if (nums[l] + nums[r] > nums[i]) {
res += r - l;
--r;
} else {
++l;
}
}
}
return res;
}
}
// Accepted solution for LeetCode #611: Valid Triangle Number
func triangleNumber(nums []int) int {
sort.Ints(nums)
n := len(nums)
ans := 0
for i := 0; i < n-2; i++ {
for j := i + 1; j < n-1; j++ {
sum := nums[i] + nums[j]
k := sort.SearchInts(nums[j+1:], sum) + j + 1 - 1
if k > j {
ans += k - j
}
}
}
return ans
}
# Accepted solution for LeetCode #611: Valid Triangle Number
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
nums.sort()
ans, n = 0, len(nums)
for i in range(n - 2):
for j in range(i + 1, n - 1):
k = bisect_left(nums, nums[i] + nums[j], lo=j + 1) - 1
ans += k - j
return ans
// Accepted solution for LeetCode #611: Valid Triangle Number
impl Solution {
pub fn triangle_number(mut nums: Vec<i32>) -> i32 {
nums.sort();
let n = nums.len();
let mut ans = 0;
for i in 0..n.saturating_sub(2) {
for j in i + 1..n.saturating_sub(1) {
let sum = nums[i] + nums[j];
let mut left = j + 1;
let mut right = n;
while left < right {
let mid = (left + right) / 2;
if nums[mid] < sum {
left = mid + 1;
} else {
right = mid;
}
}
if left > j + 1 {
ans += (left - 1 - j) as i32;
}
}
}
ans
}
}
// Accepted solution for LeetCode #611: Valid Triangle Number
function triangleNumber(nums: number[]): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let ans = 0;
for (let i = 0; i < n - 2; i++) {
for (let j = i + 1; j < n - 1; j++) {
const sum = nums[i] + nums[j];
let k = _.sortedIndex(nums, sum, j + 1) - 1;
if (k > j) {
ans += k - j;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.