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)?
- A. 10
- B. 6
- C. 9
- 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.
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
- What is the length of the longest contiguous substring of the binary string "1100100110111000101" that has equally many 0s and 1s?
- What is the smallest positive period p of the string abcabcabca, meaning the smallest p such that s[i] = s[i+p] for every valid i?
- How many distinct cyclic rotations does the string abcabc have (rotations are the strings obtained by moving a prefix to the end…
- How many subarrays of [1, 2, 1, 2, 2, 1] read the same forwards and backwards (as sequences of values)?
- What is the length of the longest palindromic substring of xabacdcabay?
- What is the minimum number of cuts needed to partition the string abacdcdad into palindromic pieces?