Javatpoint Logo
Javatpoint Logo

Reverse Pairs Problem in Data Structure

Problem Statement

Given an integer array number, return the number of reverse pairs in the array.

A reverse pair is a pair (i, j) where:

0 <= i < j < nums.length and

nums[i] > 2 * nums[j].

Example:

Input: nums = [1,3,2,3,1]

Output: 2

Explanation:

  • The output indicates that there are two reverse pairs in the given array. A reverse pair consists of two elements, nums[i] and nums[j], where i < j and nums[i] > 2 * nums[j]. In the provided array [1,3,2,3,1], the pairs [3,1] and [3,1] satisfy this condition, resulting in a total of two reverse pairs.

Example 2:

Input: nums = [2,4,3,5,1]

Output: 3

Explanation:

  • The output implies that there are three reverse pairs within the given array. A reverse pair occurs when there are two elements, nums[i] and nums[j], with indices I < j where nums[i] > 2 * nums[j]. In the array [2,4,3,5,1], the pairs [4,1], [5,1], and [3,1] satisfy this condition, resulting in three reverse pairs.

Java Approach Using Two Pointer Technique

Output:

Reverse Pairs Problem in Data Structure

Code Explanation:

  • The code efficiently counts the number of reverse pairs in a given array using the merge sort algorithm. During the merging process, it implements the two-pointer technique.
  • In the merge method, two sorted sub arrays are merged while comparing elements using two pointers (i and j). If an element in the first sub array is greater than an element in the second sub array, it indicates the existence of reverse pairs. The count of such pairs is incremented based on the number of remaining elements in the first sub array.
  • During the check method, a second pointer (right) iterates through the second sub array to find elements that form reverse pairs with elements from the first sub array.
  • Overall, the two-pointer technique efficiently identifies and counts reverse pairs while merging sorted sub arrays in the merge sort algorithm.

Time Complexity:

  • Until the final elements following the divide method are recursively divided into halves until accumulations, the time complexity is O(n log n) because of the merge sort algorithm.
  • The check method will run in linear time O(n), where 'n' stands for the array size.
  • The whole process had the algorithm with the merge sort as the dominating factor, and its time complexity is O(n log n).

Space Complexity:

  • The space complexity is O(n) because the merge operation, the recursive calls and the temporary arrays need an additional space, which is proportional to the size of the array.

Drawback:

  • Besides, one need for the above solution is its big space complexity. Besides requiring extra space for the temporary arrays, the merge sort algorithm performs the merging in all stages. As the size of the input array increases correspondingly, the space allocated to the temporary arrays will increase in the same qualitative way that produces a space-complexity increase.
  • This extra space usage can become quite significant in the case of really large dimensional input matrices, thus leading to memory-related issues or affecting the efficiency of an algorithm running on a device with limited memory resources.

Java Approach Using Merge Sort

Output:

Reverse Pairs Problem in Data Structure

Code Explanation:

The code implements the merge sort algorithm to count the number of reverse pairs in an array efficiently. Here's how it works:

  • Merge Sort: The array is recursively divided into halves until single elements are obtained. This is the "divide" step.
  • Merge: During the merging process, elements from the left and right halves are compared and merged into a single sorted array. This is where the counting of reverse pairs occurs.
  • Counting Reverse Pairs: While merging, if an element from the left half is greater than twice the element from the right half, it indicates a reverse pair. The count is incremented by the number of remaining elements in the left half that could form reverse pairs with the current element from the right half.
  • Base Case: The recursion stops when the array is reduced to a single element.

By efficiently sorting the array using merge sort and counting reverse pairs during the merge phase, the code accurately determines the total number of reverse pairs present in the array.

Time Complexity:

  • The time complexity of the suggested solution is O( n log n), where n is the length of the input array. Nevertheless, the drawback is that the merge sort process is based on the division of the array into halves recursively and the merge operation has to be executed in linear time that is proportional to the size of the array.

Space Complexity:

Space complexity is O(n), as the algorithm utilizes some extra space for the arrays used for temporary storage during the merge process.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA