Data Structures & AlgorithmsHardGATE CS · competitive programming
Four sorted lists of sizes 7, 5, 3 and 2 are merged pairwise, always merging the two currently smallest lists first. What is the total number of elements written over all the merges?
- A. 44
- B. 17
- C. 40
- D. 32
Correct answer: D. 32
Smallest first: 2 + 3 gives 5 written, 5 + 5 gives 10, and 7 + 10 gives 17, for a total of 32. Merging in the order 7, 5, 3, 2 writes 12 + 15 + 17 = 44.
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
- How many non-empty contiguous subarrays of [3, 5, 6, 1, 2, 3, 4] have bitwise XOR equal to 0?
- How many non-empty contiguous subarrays of [2, 2, 3, 3, 3, 2] consist of one repeated value only?
- How many non-empty contiguous subarrays of [1, 2, 4, 3, 6, 5, 7, 8] contain equally many even and odd numbers?
- What is the length of the longest contiguous subarray of [5, 3, 4, 2, 1, 9, 6] whose sum is divisible by 6?
- How many index pairs i < j satisfy a[i] - i = a[j] - j for the array [7, 3, 5, 8, 6, 9, 4, 10] (0-based indices)?
- How many inversions (pairs i < j with a[i] > a[j]) does the array [3, 1, 4, 1, 5, 9, 2, 6] contain?