Data Structures & AlgorithmsVery hard
Why does a circular window of length 3 require index arithmetic modulo n?
- A. Because circular arrays cannot be indexed at all.
- B. So that the sum is taken modulo n.
- C. To make the array shorter.
- D. So that windows can wrap past the last index and continue from the first, covering every circular block.
Correct answer: D. So that windows can wrap past the last index and continue from the first, covering every circular block.
A circular block may start near the end and continue at the beginning, so each start index i uses positions (i+t) mod n. Without the modulo, the wrap-around windows would be missed.
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
- Why is the number of Dyck-word stack sequences with n pushes and n pops equal to a Catalan number?
- A deque-based minimum of each sliding window runs in linear time because:
- How many ways are there to make 15 using coins of values 1, 2, 5 and 10, where the order of coins does not matter (so 5+5+5 and 5+10 are…
- A binary string "1011001010" should become non-decreasing (all 0s before all 1s). In one operation you flip a single character. What is…
- What is the length of the longest NON-DECREASING subsequence (equal neighbours allowed) of [5, 1, 3, 3, 2, 6, 6, 4, 7]?
- How many subsets of [2, 3, 5, 4, 6] (chosen by position) have a sum exactly equal to half of the total sum?