Data Structures & AlgorithmsHardLeetCode-style · TCS NQT

How many contiguous subarrays of [1, 2, 3, 4, 7, 10, 13, 14, 15, 16] are arithmetic (at least 3 elements, with a constant difference between neighbours)?

  1. A. 10
  2. B. 6
  3. C. 9
  4. D. 7

Correct answer: C. 9

For each index with an equal consecutive difference, the number of arithmetic subarrays ending there is one more than the run of equal differences before it. Counting only maximal runs, or counting only triples, undercounts the longer ones.

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

Exams that ask this topic