Data Structures & AlgorithmsVery hard

How many ways can three DIFFERENT sticks of lengths 2 3 4 5 6 7 (chosen by position) form a non-degenerate triangle? A triangle needs the sum of the two shorter sides to be strictly greater than the longest side.

  1. A. 17
  2. B. 21
  3. C. 13
  4. D. 14

Correct answer: C. 13

Sort the lengths: a triple forms a triangle exactly when the two shorter sides sum to more than the longest, and a two-pointer scan counts these in O(n²). Using ≥ admits degenerate triangles, and comparing against the wrong side changes the count.

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

Exams that ask this topic