Reverse Pairs in C++


Suppose we have an array, in this array we will say one pair (A[i] and A[j]) as important reverse pairs if this satisfies the following condition −

  • if i < j and A[i] > 2* nums[j]

We have to find the number of important reverse pairs. So if the input is like [2,8,7,7,2], then the result will be 3.

To solve this, we will follow these steps −

  • ans := 0
  • Define a function merge(), this will take an array a, low, mid, high,
  • k := high - low + 1
  • Define an array temp of size k
  • i := low, j = mid + 1, k := 0
  • first := mid + 1
  • while i <= mid, do −
    • while first <= high and a[first] * 2 < a, do −
      • (increase first by 1)
    • while (j <= high and a[j] <= a[i]), do −
      • temp[k] := a[j]
      • (increase j by 1)
      • (increase k by 1)
    • ans := ans + first - (mid + 1)
    • temp[k] := a[i]
    • (increase i by 1)
    • (increase k by 1)
  • while j <= high, do −
    • temp[k] := a[j]
    • (increase k by 1)
    • (increase j by 1)
  • k := 0
  • for initialize i := low, when i <= high, update (increase i by 1), do −
    • a[i] := temp[k]
    • (increase k by 1)
  • Define a function calc(), this will take an array a, low, high,
  • if low >= high, then −
    • return
  • mid := low + (high - low)/2
  • call the function calc(a, low, mid)
  • call the function calc(a, mid + 1, high)
  • call the function merge(a, low, mid, high)
  • Define a function solve(), this will take an array A,
  • ans := 0
  • n := size of A
  • call the function calc(A, 0, n - 1)
  • return ans
  • From the main method, do the following
  • return call the function solve(nums)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
class Solution {
public:
   int ans = 0;
   void merge(vector <int> &a, lli low, lli mid, lli high){
      lli k = high - low + 1;
      vector <lli> temp(k);
      lli i = low, j = mid + 1;
      k = 0;
      lli first = mid + 1;
      while(i <= mid){
         while(first <= high && (lli)a[first] * 2 < (lli)a[i]) {
            first++;
         }
         while(j <= high && a[j] <= a[i])
         {
            temp[k] = a[j];
            j++;
            k++;
         }
         ans += first - (mid + 1);
         temp[k] = a[i];
         i++;
         k++;
      }
      while(j <= high){
         temp[k] = a[j];
         k++;
         j++;
      }
      k = 0;
      for(lli i = low; i <= high; i++){
         a[i] = temp[k];
         k++;
      }
   }
   void calc(vector <int> &a, lli low, lli high){
      if(low >= high)return;
      lli mid = low + (high - low)/2;
      calc(a, low, mid);
      calc(a, mid + 1, high);
      merge(a, low, mid, high);
   }
   lli solve(vector<int> &A) {
      ans = 0;
      lli n = A.size();
      calc(A, 0, n - 1);
      return ans;
   }
   int reversePairs(vector<int>& nums) {
      return solve(nums);
   }
};
main(){
   Solution ob;
   vector<int> v = {2,8,7,7,2};
   cout << (ob.reversePairs(v));
}

Input

{2,8,7,7,2}

Output

3

Updated on: 01-Jun-2020

647 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements