Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
From the stick lengths [4, 2, 7, 5, 3, 6, 9], choose three different sticks that can form a non-degenerate triangle (the sum of any two sides must exceed the third). How many such triples are there?
- A. 19
- B. 21
- C. 16
- D. 20
Correct answer: A. 19
Sort the sticks; for each largest side c, count pairs of smaller sticks a < b with a + b > c using two pointers. Checking only the sum of the two smallest sides misses valid triples.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many integers x with 1 ≤ x < 300 have the same decimal digit sum as x + 1?
- Prices [3, 8, 2, 9, 1, 7] are listed for six days. You pick two different days, buy on one and sell on the other, with no time-order…
- Choose two NON-OVERLAPPING contiguous windows of [5, -2, 8, 1, -3, 6, 2, 4]: one of length exactly 2 and one of length exactly 3. What is…
- How many pairs of positions (i, j) with j − i ≥ 3 satisfy a[i] = a[j], for the array [1, 2, 1, 3, 2, 1, 3, 1]?
- How many non-empty contiguous subarrays of [2, 2, 5, -1, 3, 4, 1] have a sum that is a perfect square (0 counts as a square)?
- How many non-empty subsets of [3, 1, 4, 2, 5] (chosen by position) have a sum between 4 and 7 inclusive?