📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Consider the following Python-like pseudo-code for a

    modified Merge Sort algorithm that sorts an array `arr` and also counts the number of "reverse pairs" (i.e., pairs `(i, j)` such that `i < j` and `arr[i] > 2  arr[j]`).     ```python     def merge_sort_and_count_reverse_pairs(arr):         n = len(arr)         if n 2  right_half[j]:                 cross_pairs += (len(left_half) - i) # All remaining elements in left_half form a reverse pair with right_half[j]                 j += 1             else:                 i += 1         # Standard merge step (omitted for brevity, assume it correctly merges left_half and right_half)         merged_arr = merge(left_half, right_half)         return merged_arr, left_pairs + right_pairs + cross_pairs     # Assume a standard merge function exists:     # def merge(left, right):     #     ... returns sorted merged array ...     input_array = [2, 4, 3, 5, 1]     ```     If `merge_sort_and_count_reverse_pairs(input_array)` is called, what will be the total number of reverse pairs returned?
    A 2 Correct Answer Incorrect Answer
    B 3 Correct Answer Incorrect Answer
    C 4 Correct Answer Incorrect Answer
    D 5 Correct Answer Incorrect Answer

    Solution

    The pairs are: (2,1), (4,1), (3,1), (5,1).

    Practice Next
    More IT DBMS Questions
    ask-question