Question: For this problem I have a java code: import java.util.Arrays; import java.util.Scanner; public class MergeSortWeightedSumInversion { public static long merge(int arr[], int l, int mid,

For this problem I have a java code:
import java.util.Arrays; import java.util.Scanner; public class MergeSortWeightedSumInversion { public static long merge(int arr[], int l, int mid, int r) { long weight = 0; int n1 = mid-l+1; int n2 = r-mid; int[] a = new int[n1]; int[] b = new int[n2]; for(int i = 0; i This is wrong because I tried testing it on the array: [7,3,8,1,5] and I got a weight of 26, its only adding up half of the inversions. How would I modify it so that it works? Code in java and time complexity must be O(n log(n)) please. In the problem of counting inversions, you are given a permutation a1,a2,an of numbers 1,2,,n, and the goal is to count the number of pairs i,j, where i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
