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.
- A. 17
- B. 21
- C. 13
- 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.
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 index quadruples i < j < k < l in 1 -1 2 -2 3 -3 have A[i] + A[j] + A[k] + A[l] = 0?
- How many index pairs i < j in 4 9 5 3 6 6 10 satisfy |A[i] − A[j]| = 1?
- How many contiguous windows of length exactly 3 in 4 1 7 3 9 5 2 have their MIDDLE element equal to the median of the window?
- How many contiguous windows of length exactly 4 in 6 2 5 9 4 7 3 satisfy (maximum − minimum) ≤ 3?
- What is the minimum product of two ADJACENT elements of 5 3 8 2 9 4?
- How many index pairs i < j in 1 2 3 4 5 6 satisfy A[i] AND A[j] = 0 (bitwise AND)?