- Dynamic Programming, Memoization, Recursion, Sorting

Find expected swaps to sort given Array where probability of swapping any inversion pair is equal

Given an array arr[] consisting of permutation of the first N natural numbers, the task is to find the expected number of swaps to sort the given array where the probability of swapping any inversion pair is equal and the probability of swapping any non-inversion pair is 0.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: arr[] = {3, 2, 1}Output: 2.3333Explanation: The given array can be sorted in non-decreasing order order using sequences of swaps as shown in the image below. The expected numbers of swaps of each node can be calculated by 1 + (average of expected number of swaps of all the child nodes). Since the nodes 3, 9, 10, 11, and 12 are already sorted, the number of expected swaps to sort them are 0.The expected number of swaps of nodes 5, 6, 7, and 8 will be 1.Similarly, the expected number of swaps of nodes 2 and 4 will be 2.Hence, the expected number of swaps of node 1 is 1 + (2 + 2 + 0)/3 = 1 + 1.3333 = 2.3333Input: arr[] = {1, 3, 2}Output: 1.0Explanation: Since there is only 1 inversion pair (arr[1], arr[2]), the expected number of swaps is 1.Approach: The given problem can be solved with the help of Recursion and Backtracking using Memoization which is based on the following observations:An inversion in the array is defined as two indices (i, j) such that i < j and arr[i] > arr[j]. The expected number of swaps can be calculated recursively after swapping the integers of each valid inversion one at a time and recursively calling for the permutation after the swap until the whole permutation is sorted.Suppose there are K inversions in the current permutation and after swapping the ith inversion, the expected number of swaps to sort the permutation is denoted by Pi. Therefore, the expected number of swaps after swapping all possible inversions will be (P1 + P2 + … + PK)/K.Using the above observations, the given problem can be solved by the following steps:Create a map, say memo that stores the expected number of swaps for a given permutation.Create a Recursive Function expectedSwaps(), which takes a permutation as an argument and returns the expected number of swaps.In the expectedSwaps() function, if the expected swaps of the current permutation are already calculated, return the answer. Otherwise, iterate over each valid inversion and swap the index of the current inversion and recursively call for the permutation after swapping.Find the sum of the expected swaps after each valid swap in a variable, say res, and the count of inversions in a variable, say K.After completing the above steps, print the value of res / K as the result.Below is the implementation of the above approach:C++  #include using namespace std;  map memo;  double expectedSwaps(vector a){            if (memo.count(a)) {        return memo[a];    }          double res = 0;          int K = 0;              for (int i = 0; i < a.size(); i++) {        for (int j = i + 1; j < a.size(); j++) {                                      if (a[i] > a[j]) {                swap(a[i], a[j]);                                  res += 1 + expectedSwaps(a);                                  swap(a[i], a[j]);                K++;            }        }    }          if (K == 0)        res = 0;              else        res /= K;          return memo[a] = res;}  int main(){    int N = 3;    vector arr = { 3, 2, 1 };      cout