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 = [-1, 0, 2, 2, 2] 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. [0, −8, 0, 0, 0]
- B. [−8, 0, 0, 0, 0]
- C. [0, 0, −8, 0, 0]
- D. [0, 0, 0, 0, 0]
Correct answer: A. [0, −8, 0, 0, 0]
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: [0, −8, 0, 0, 0].
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 maximum product of a non-empty contiguous subarray of [−2, 4, 3, −1, −2, −3, −2, 0]? (Track both the maximum and the minimum…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…
- What does this loop-counting code print? (Python) python n, c = 37, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [14, 8, 20, 57, 38, 4…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 4 positions. What is the final array?