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?

  1. A. 19
  2. B. 21
  3. C. 16
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic