ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints the number of distinct values in the merged list. What is printed?
Two numbers are formed from the arrays a = [1, 3, 5] and b = [2, 3, 6] by merging them into one sorted list. The procedure below prints the number of distinct values in the merged list. What is printed?
merge the two sorted lists into one sorted list
count = 0; prev = none
for each x in merged
if x != prev then count = count + 1; prev = x
end for
Print count- A. 6
- B. 1
- C. 5
- D. 4
Correct answer: C. 5
The merged list is 1, 2, 3, 3, 5, 6, with the value 3 repeated, so there are 5 distinct values. Counting all elements gives 6, distinct values minus one gives 4, and a broken comparison reports a different count.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- For the array a = [-1, 4, -2, 6, -3, 5], the procedure below finds the maximum sum of a contiguous window of exactly 3 elements (windows…
- Array a = [4, 9, 2, 8, 2, 7]. The procedure below counts the prefix records, that is, the positions i where a[i] is strictly greater than…
- A rotated sorted array a = [4, 5, 6, 7, 0, 1, 2] has its smallest element at some index. The procedure below finds that index by a linear…
- The loops below print the pairs (i, j) with 1 <= i <= j <= 3 in order, and the procedure prints the total count of pairs. What is printed?…
- Array a = [3, 1, 2]. The procedure below performs a stable count of the number of ways the array can be read in increasing order using…
- Array a = [1, 3, 2, 3, 4, 1] is searched for the longest strictly increasing contiguous run. The procedure below returns its length. What…