#dsa help
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @silent wing! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 720 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
can you send the full image
here you go 🙂
Approach it like this: for every place in the array check the following places for the condition
You will need 2 for loops, nested into each other
i got da answer guys
"""class Solution {
static int count = 0;
public int reversePairs(int[] nums) {
count = 0;
mergeSort(nums);
return count;
}
public void mergeSort(int[] arr){
int n = arr.length;
if(n <= 1){
return;
}
int[] a = new int[n/2];
int[] b = new int[n-n/2];
int k = 0;
for(int i = 0; i < a.length; i++) a[i] = arr[k++];
for(int j = 0; j < b.length; j++) b[j] = arr[k++];
mergeSort(a);
mergeSort(b);
merge(arr,a,b);
}
public void merge(int[] target, int[] a, int[] b){
int i = 0, j = 0;
while(i < a.length){
while(j < b.length && a[i] > 2L * b[j]){
j++;
}
count += j;
i++;
}
i = 0;
j = 0;
int k = 0;
while(i < a.length && j < b.length){
if(a[i] < b[j]){
target[k++] = a[i++];
}
else{
target[k++] = b [j++];
}
}
while(i < a.length) target[k++] = a[i++];
while(j < b.length) target[k++] = b[j++];
}
}""" ```
This message has been formatted automatically. You can disable this using /preferences.