class Solution{ public: long long int count=0; long long int inversionCount(long long arr[], long long N) { mergesort(arr,0,N-1); return count; } long long int* mergesort(long long arr[],long long low, long long high) { if(low==high) { long long int* ans=new long long int[1]; ans[0]=arr[low]; return ans; } long long mid=(low+high)/2; long long int* left=mergesort(arr,low,mid); long long int* right=mergesort(arr,mid+1,high); return merge(left,right,mid-low+1,high-mid); } long long int* merge(long long int* left,long long int* right,int n, int m) { int i=0; int j=0; long long int* ans=new long long int[n+m+1]; int k=0; while(i
While passing mid to mergearrays function why did you pass it as (mid-low+1) why not simply (mid+1) and also for the high too... Could you please explain that part?
return merge(left,right,mid-low+1,high-mid); long long int* merge(long long int* left,long long int* right,int n, int m),lets see, actually n and m size of left and right array
Hi Alisha, such an amazing explanation!! Btw there is another similar problem in leetcode called the reverse Pairs, I applied the same logic but i'm not able to get the output! class Solution { int count=0; public int reversePairs(int[] nums) {
int N=nums.length; int a[]=mergesort(nums,0,N-1); return count; } public int[] mergesort(int arr[],int low,int high) { if(low==high) { int[] ans=new int[1]; ans[0]=arr[low]; return ans; } int mid=(low+high)/2; int[] left=mergesort(arr,low,mid); int[] right=mergesort(arr,mid+1,high); return merge(left,right,mid-low+1,high-mid); } public int[] merge(int[] left,int[] right,int n, int m) {
int i=0; int j=0; int[] ans=new int[n+m+1]; int k=0; while(i
OMG 😱 never thought this would be that easy. Can't stop u appreciating for solving Gfg questions 👏. Keep posting, Thanks dude
Dhanyawad MataJi. 🌸🙌🙏
class Solution{
public:
long long int count=0;
long long int inversionCount(long long arr[], long long N)
{
mergesort(arr,0,N-1);
return count;
}
long long int* mergesort(long long arr[],long long low, long long high)
{
if(low==high)
{
long long int* ans=new long long int[1];
ans[0]=arr[low];
return ans;
}
long long mid=(low+high)/2;
long long int* left=mergesort(arr,low,mid);
long long int* right=mergesort(arr,mid+1,high);
return merge(left,right,mid-low+1,high-mid);
}
long long int* merge(long long int* left,long long int* right,int n, int m)
{
int i=0;
int j=0;
long long int* ans=new long long int[n+m+1];
int k=0;
while(i
your explanation is so easy, carry on
Superb explination ! I always understand your explination in one go ,, thankyou
Thanku you so much Alisha ...Amazing explanation!!!😎
Thank you for the great explanation, you gain a new subscriber.
kitne dino se pata nai chal raha tha aaj cancept clear huaa thank you so much mem.
how count+=n-i;,how did u come up with this ??
Best Explanation.
Great Explaination!!!
😍🥰
In python .....it is giving correct answer when count+=n-i+1.....in 52th line.........can you please tell me why
Great Explaination
Such a nice explanation 🔥
Is this Q over Leetcode??
It is on hackerrank interview preparation kit , and geeksforgeeks, not sure about leetcode
775. Global and Local Inversions(leetcode)
While passing mid to mergearrays function why did you pass it as (mid-low+1) why not simply (mid+1) and also for the high too... Could you please explain that part?
return merge(left,right,mid-low+1,high-mid);
long long int* merge(long long int* left,long long int* right,int n, int m),lets see, actually n and m size of left and right array
because its a length of a array
Thank you Mam ,nicely explained
Hi Alisha, such an amazing explanation!!
Btw there is another similar problem in leetcode called the reverse Pairs,
I applied the same logic but i'm not able to get the output!
class Solution {
int count=0;
public int reversePairs(int[] nums) {
int N=nums.length;
int a[]=mergesort(nums,0,N-1);
return count;
}
public int[] mergesort(int arr[],int low,int high)
{
if(low==high)
{
int[] ans=new int[1];
ans[0]=arr[low];
return ans;
}
int mid=(low+high)/2;
int[] left=mergesort(arr,low,mid);
int[] right=mergesort(arr,mid+1,high);
return merge(left,right,mid-low+1,high-mid);
}
public int[] merge(int[] left,int[] right,int n, int m)
{
int i=0;
int j=0;
int[] ans=new int[n+m+1];
int k=0;
while(i
if(left[i]>(2*right[j])) remove this
Explain why count + =n-i is ans
the thing you and i have same is number of tabs in chrome😂
nice
Its not working buddy.
Incorrect Output.