Data Structures & AlgorithmsHardTCS Digital · Infosys SP
Product of all elements except self, using prefix and suffix products (no division). What is printed?
Product of all elements except self, using prefix and suffix products (no division). What is printed?
a = [2, 3, 3, 2, -1] n = len(a); out = [1] * n p = 1 for i in range(n): out[i] = p; p *= a[i] s = 1 for i in range(n - 1, -1, -1): out[i] *= s; s *= a[i] print(out)
- A. [−18, −12, −12, −18, 36]
- B. [−18, −12, −12, 36, −18]
- C. [−36, −36, −36, −36, −36]
- D. [−12, −18, −12, −18, 36]
Correct answer: A. [−18, −12, −12, −18, 36]
out[i] must take the product BEFORE multiplying in a[i]; a zero in the array makes every other position 0 except the zero's own. Output: [−18, −12, −12, −18, 36].
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 does this loop-counting code print? (Python) python n, c = 16, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- For the sorted array [1, 3, 4, 6, 6, 9, 9, 11, 13, 14, 14, 15] (0-based), what are lowerbound(14) (first index with value ≥ 14) and…
- Records are sorted with a composite key. What is printed? python recs = [('kabir', 80), ('dev', 70), ('meera', 70), ('ravi', 70), ('zoya'…
- Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. The span of day i is the number of consecutive days ending at day i…
- You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?
- The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] are sorted by start and merged (intervals that touch, like [2, 4] and…