COUNT INVERSIONS in an ARRAY | Leetcode | C++ | Java | Brute-Optimal

  Рет қаралды 235,699

take U forward

take U forward

Күн бұрын

Пікірлер: 276
@takeUforward
@takeUforward 4 жыл бұрын
Please watch the new video which covers it in more depth, and also prints it: kzbin.info/www/bejne/d6TIhqCti7OUpbs Understood or nott? am waiting in the comment section ;) . Check me out at Instagram: instagram.com/striver_79/ . If you appreciate the channel's work, you can join the family: bit.ly/joinFamily
@vaibhavsethia70
@vaibhavsethia70 4 жыл бұрын
Understood
@sahilchoudhary1473
@sahilchoudhary1473 4 жыл бұрын
Understood
@ashishdwivedi5190
@ashishdwivedi5190 4 жыл бұрын
Hey u forgot to upload link to this video on sde sheet....
@mafiaco1257
@mafiaco1257 4 жыл бұрын
No 🥺
@pleasesirmorevideos4684
@pleasesirmorevideos4684 4 жыл бұрын
Understoood
@shardulkhadye5875
@shardulkhadye5875 2 жыл бұрын
i sincerely feel that this problem should not be marked as medium level as its extremely difficult to figure out this answer in an interview if not solved previously.
@codding32world50
@codding32world50 2 жыл бұрын
Yes I was stuck for whole day literally then came here😢
@aakashmehta9474
@aakashmehta9474 2 жыл бұрын
Not at all possible to figure out this solution unless you are genius 😃
@ritikashishodia675
@ritikashishodia675 2 жыл бұрын
Maine to phele kiya tha ye to jb dubara kiya after months nahi hua.😔😔
@garvgoel1743
@garvgoel1743 Жыл бұрын
@@ritikashishodia675 This is very normal 😂
@mdhaidarparwez968
@mdhaidarparwez968 Жыл бұрын
For this type of problem, you should make a note and revise weekly
@muditkhanna8164
@muditkhanna8164 Жыл бұрын
180 questions form sde sheet with striver's solution>>> 1000 self practice questions , you get to learn so many tricks and concepts with in each question. the key is to not fast forward the question. give each question its deserving time. that's all then striver's sheet is enough to you ! thank you dude!
@yashisrivastava5482
@yashisrivastava5482 4 жыл бұрын
Sir you are really a motivation , after so much of busy schedule your are still selflessly helping us in every way (hats off to you) . I wait the whole day just for your video and only after watching and learning the concept I sleep. Thank you so much sir
@takeUforward
@takeUforward 4 жыл бұрын
Yashi Srivastava glat that you wait, this things keeps me motivated!!
@abhijitroy1958
@abhijitroy1958 2 жыл бұрын
The alpha rule : never lose the concentration while watching striver's video . Best explanation
@rohitanjane1668
@rohitanjane1668 2 жыл бұрын
Highly agree with you 👍🏻🙌🏻
@neerajsinghjr
@neerajsinghjr 2 жыл бұрын
Seriously, this guy is Awesome. but its also true... you can't doze off for a single bit. If you do then rewind ...
@baljotsinghchoudhary7434
@baljotsinghchoudhary7434 3 жыл бұрын
this question is very interesting one should try to solve with all approches merge sort, fenwick and trie
@tanayshah275
@tanayshah275 3 жыл бұрын
Great Explanation! One more thing if anyone is implementing it then the invertion count will increase by mid - i + 1
@tarungopal3065
@tarungopal3065 2 жыл бұрын
@Anand Pandey As you are calling merge() function with 'mid' as as the argument..you have to write " mid- i + 1" But the In video... He is calling as merge(left, mid+1, right)
@tusharvaish8096
@tusharvaish8096 2 жыл бұрын
Why.? Can you explain.
@tusharvaish8096
@tusharvaish8096 2 жыл бұрын
@Anand Pandey why we need to add 1 ? Can you explain
@mohitsingh7793
@mohitsingh7793 2 жыл бұрын
Merge-Sort with small modification : void merge(int l1, int h1, int l2, int h2, vector&nums, int &count) { int n1 = h1 - l1 + 1; int n2 = h2 - l2 + 1; vectora(n1), b(n2), c(n1 + n2); for (int i = 0; i < n1; i++) a[i] = nums[l1 + i]; for (int i = 0; i < n2; i++) b[i] = nums[l2 + i]; int i = 0, j = 0, k = 0; while (i < n1 and j < n2) { if (a[i] l) { int mid = l + (h - l) / 2; merge_sort(l, mid, nums, count); merge_sort(mid + 1, h, nums, count); merge(l, mid, mid + 1, h, nums, count); } }
@shreeramgoyal84
@shreeramgoyal84 3 жыл бұрын
Hey Striver! was wondering if we could do it by Priority Queue. I mean we can but then we would need an extra storage for storing the popped values and an additional complexity to push back to pq. What do you suggest?
@prashantbisht2219
@prashantbisht2219 3 жыл бұрын
Yeah but wouldn't it be O(n^2).For every element you traversing the entire queue in worst case.
@pranav288
@pranav288 Жыл бұрын
@@prashantbisht2219 yup
@krisnerdscave394
@krisnerdscave394 2 жыл бұрын
12:22 I guess there should by said number of elements to the right of the 'i' not 'mid' so the number of elements from left that the right element needed to jump over it. And this is what Striver said here 12:27 basically :) 11:56 K should not be set to zero, as we won't to have indexes in temp as in original arr. 13:01 Do we rally need that copying for anything?
@krisnerdscave394
@krisnerdscave394 2 жыл бұрын
Love to discuss with myself :) but yeah we really need that copying and by saying that K should not be zero I've automatically confirmed that it's needed :)
@vershamishra8763
@vershamishra8763 2 жыл бұрын
@@krisnerdscave394 yes, Thank you for the first statement, I got confused then this helped me to clarify.
@shivalikagupta3433
@shivalikagupta3433 2 жыл бұрын
Truly each and every word you speak is in align with the question's essence and explanation 1 minute apart and I need to watch the video again. Its a thorough explanation. Wonderful!!!
@devanshmesson2777
@devanshmesson2777 4 жыл бұрын
You Speak So Clearly! Great Video Quality and Awesome Explaination!!
@dhanubhardwaj6935
@dhanubhardwaj6935 3 жыл бұрын
why not inv_count=mid-i+1 bcz both i and mid are inclusive
@BattlefieldAWWWClips
@BattlefieldAWWWClips 2 жыл бұрын
Hi striver meet gandhi , here again revising few tough code , i think there is a mistake wen u are doing inv_count =inv_count +(mid-1) ; U are telling this for right side of array but ideally we are counting from mid to left (i.e. i) . I think it is left right do correct asap plz if wrong .
@mohitsingh7793
@mohitsingh7793 2 жыл бұрын
Hey Stiver,I can easily solve that question using fenwick tree data structure.But what about if maximum element in array can be upto 10^9.How to solve that using fenwick tree ? Or we can't do incase of large elements of array?
@mdazeen8820
@mdazeen8820 2 жыл бұрын
this can be done by compressing the array because size of array is 10^5 .For example {10^8,10^9} can be compressed as {1,2}
@adityasai550
@adityasai550 2 жыл бұрын
Bro can u tell me from where you learned Fenwick tree. There are not many good videos about Fenwick tree in KZbin
@mohitsingh7793
@mohitsingh7793 2 жыл бұрын
@@adityasai550 kzbin.info/www/bejne/b6bEiXpsZ75ri9k
@Sahil-nm7di
@Sahil-nm7di 4 жыл бұрын
I am already done this question but after that i am loving to see your solution video its helps us to how we should explain our solution in front of interviewer thanx..
@takeUforward
@takeUforward 4 жыл бұрын
Thanks! Cheers!!
@aasthajain4210
@aasthajain4210 4 жыл бұрын
You are so hardworking man!! I just look up to this✨
@rudranilacharya5293
@rudranilacharya5293 2 жыл бұрын
Can we use ordered set in interviews, this ques is much easier with ordered set
@SurajVerma-lx6ik
@SurajVerma-lx6ik 4 жыл бұрын
Hey striver I have one suggestion for placement series U could add 3 similar ques for practice in each vdo of placement series starting from Day -1 ... I am not sure i am right or wrong plzz tell striver is it correct or not bas man me kya isliye suggest kar diye . Plzz reply #striver 🤔
@jayeshgupta8618
@jayeshgupta8618 4 жыл бұрын
Bro when you solve these questions on leetcode then leetcode suggest similar questions , just do one or two from the suggestions maybe .
@SurajVerma-lx6ik
@SurajVerma-lx6ik 4 жыл бұрын
@@jayeshgupta8618 uske liye leetcode premium lena padega kya ????
@sasiraj1594
@sasiraj1594 4 жыл бұрын
@@SurajVerma-lx6ik no
@SurajVerma-lx6ik
@SurajVerma-lx6ik 4 жыл бұрын
@@sasiraj1594 ok
@shivendrsrivastava7315
@shivendrsrivastava7315 4 жыл бұрын
So can we say that the crux is doing inversion is basically sorting ? and merge sort is the most appropriate one
@himanshusinghal5299
@himanshusinghal5299 4 жыл бұрын
I think we can use binary search(lower_bound stl) which would take O(nlogn)
@takeUforward
@takeUforward 4 жыл бұрын
No pro it will not.. you will have to use set, so after lower bound to find number of elements you need to subtract the iterator which will take n time making it total of n^2
@himanshusinghal5299
@himanshusinghal5299 4 жыл бұрын
@@takeUforward yeah got it thanx
@KathanShah03
@KathanShah03 2 жыл бұрын
@@takeUforward is there any other method to solve this problem using hash/set/maps?
@shanayaarora198
@shanayaarora198 3 жыл бұрын
Happy Teachers Day :) Thanks for such a great content.
@venuvenu2719
@venuvenu2719 2 жыл бұрын
I have been thinking of an alternate easy approach. Let's say I sort the array in nlogn, then count every element inversion count using O(N) approach where we just count the number of elements on the right side of the array. total TC= nlogn + n = nlogn Wouldnt this be an easier solution? Please correct me if this is wrong.
@sak3579
@sak3579 2 жыл бұрын
can't sort the array
@brahmkaransingh8804
@brahmkaransingh8804 2 жыл бұрын
No its wrong bcoz it will count that inversions also which was not a inversion in real array . It will work if array is sorted in descending order, then only your approach will follow good. Hope you understand. If have any doubt let me know
@acxd
@acxd 4 жыл бұрын
It would have been much much clearer if you had modified the code of merge sort from gfg because using temp as parameter is confusing and the gfg article from where you have taken the code is also not easy to understand.
@takeUforward
@takeUforward 4 жыл бұрын
I did not want to modify because a bunch kf students follow gfg 😅
@smartwaytolearn2425
@smartwaytolearn2425 Жыл бұрын
why can't we solve this with gap method??? please reply.......
@ranasauravsingh
@ranasauravsingh 2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@amitsamratmaurya3170
@amitsamratmaurya3170 3 жыл бұрын
Can you explain why you are using -> (mid-i) isnt it supposed to be (mid-i+1), but why your code works?
@VinayKumar-oi4yq
@VinayKumar-oi4yq 3 жыл бұрын
Yeah, my code worked with mid-i+1 as well
@atmanirbharladka4467
@atmanirbharladka4467 3 жыл бұрын
I really appreciate you explaining the prerequisite too for the question.
@demonslayer4607
@demonslayer4607 Жыл бұрын
tum dhanush fan ko kya ?
@souravchakraborty9777
@souravchakraborty9777 2 жыл бұрын
Would you kindly explain the Fenwick tree method?
@stith_pragya
@stith_pragya 2 жыл бұрын
Thank You very much for providing this video.........👍👍👍👍👍👍
@MrJoyRana
@MrJoyRana Жыл бұрын
Solution in javascript function merge(left, right, obj) { let result = []; let l = 0; let r = 0; while (l < left.length && r < right.length) { if (left[l] < right[r]) { result.push(left[l++]); } else { obj.count++; // this is one pair as a[i]>a[j] obj.count += left.slice(l + 1).length; // if left[l]>left[r], and left array is sorted in ascending order, then all elements to to the right also can be pair result.push(right[r++]); } } return [...result, ...left.slice(l), ...right.slice(r)]; } function mergeSort(arr, obj) { const length = arr.length; if (length < 2) return arr; let midPoint = Math.floor(length / 2); const left = arr.slice(0, midPoint); const right = arr.slice(midPoint); return merge(mergeSort(left, obj), mergeSort(right, obj), obj); } function countInversion(arr) { const obj = { count: 0 }; const mergedArr = mergeSort(arr, obj); return obj.count; }
@amrithapatil4595
@amrithapatil4595 2 жыл бұрын
I think line number 28 should be inv_count = inv_count + mid - i+1;
@brahmkaransingh8804
@brahmkaransingh8804 2 жыл бұрын
Yes that was the doubt which i was searching for in the comment section
@brahmkaransingh8804
@brahmkaransingh8804 2 жыл бұрын
@Anand Pandey bro can you tell me whats wrong with the below code. Just ignore as i am not maintaining the temp array and just looking for inv_count. Thanks in advance Code -> long long merge(long long arr[], long long l, long long mid, long long r){ long long i = l; long long j = mid + 1; long long inv_count = 0; while(i
@imtiyazuddin4827
@imtiyazuddin4827 Жыл бұрын
yep, i had the same doubt
@SatyaMantha
@SatyaMantha Жыл бұрын
Striver, i make sure i like and comment on every video that i watch. I am learning a lot from your videos.
@darshantank554
@darshantank554 2 жыл бұрын
What do you think about a "binary indexed tree" with O(N) ?!?!?!?!?!
@greedycat9845
@greedycat9845 3 жыл бұрын
your diagram had made me reailze the core part of the solution.very nice explanatio
@peter9759
@peter9759 Жыл бұрын
Nice video totally understood optimal approach
@Ayush-ir3wi
@Ayush-ir3wi 2 жыл бұрын
inv_count += merge(arr, temp, left, mid + 1, right); why are we passing mid+1 here when the starting index of the 2nd array is mid??
@pulkitahuja4750
@pulkitahuja4750 3 жыл бұрын
Can you please the solution using BIT, I saw it on leetcode but I am not so familiar to using BIT except for the range sums. It would be very helpful if we could learn some applications of BIT.
@amitguptapc
@amitguptapc Жыл бұрын
whats the name of this problem on leetcode?
@saishree8886
@saishree8886 3 жыл бұрын
@takeuforward using global variables gives wrong o/p y?But doing the same by adding subtrees gives the crct o/p
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Thank you so much bro!!!!!
@goodpeople7615
@goodpeople7615 4 жыл бұрын
Awesome content shows how good preparation it needs and you nail every time . Happy teachers day ʕ´•ᴥ•`ʔ
@mannthakkar
@mannthakkar 2 жыл бұрын
Very interesting question once you practice it
@AdityaKumar-rs5jn
@AdityaKumar-rs5jn 4 жыл бұрын
Bhai bahut bdiya explain krte.. Fully understood..
@takeUforward
@takeUforward 4 жыл бұрын
Thanks! Cheers :)
@nayanbhuyan8156
@nayanbhuyan8156 4 жыл бұрын
Nice explanation, I would love to see more videos on critical questions
@maulikdev5040
@maulikdev5040 2 жыл бұрын
Amazingly taught the concept. Thank you so much!!
@joyalmeida6565
@joyalmeida6565 3 жыл бұрын
why do we need temp array? I'm not getting in what way will the existing array will be modified. Please Explain
@yatinarora1252
@yatinarora1252 3 жыл бұрын
U can watch merge sort it that u need a array so that you can basically store your elements and find comparison
@ankitdubey9310
@ankitdubey9310 3 жыл бұрын
can we just move in the array and use stl sort to sort the right half and then compare each element to the one on the right and if its greater we are good to go, however after every sort i want to my array back to normal, is there a way around this?
@nuclearnadal69
@nuclearnadal69 3 жыл бұрын
Bro this approach sounds more brute than the brute force itself
@ankitdubey9310
@ankitdubey9310 3 жыл бұрын
@@nuclearnadal69 my programming 4 months back was "brutal" lol 😂😂
@MallistParasite
@MallistParasite 3 жыл бұрын
The Struggle Between Sort and Shot.Very Evident😛. Great Explaination.Understood.
@farazahmed7
@farazahmed7 2 жыл бұрын
bolo aunty tau kya
@nayanbhuyan8156
@nayanbhuyan8156 3 жыл бұрын
Your sde sheet link for this video is not working, directing the spoj
@SHASHANKRUSTAGII
@SHASHANKRUSTAGII 3 жыл бұрын
I tried to think, when i saw nlogn, heapsort, merge sort came to my mind. my mind tried to think, but could not develop intuition for O(nlogn) :( Although, brute force is easy for me, but how do i optimise?
@farazahmed7
@farazahmed7 2 жыл бұрын
no one can come up with the optimal soln until and unless you have already solved it before.
@SHASHANKRUSTAGII
@SHASHANKRUSTAGII 2 жыл бұрын
@@farazahmed7 no after 1 ye if practice now i can see why merge sort is the key to solve this problem. We can make use of merge sort in order to find the number of inversions optimally
@aniketdubey7363
@aniketdubey7363 2 жыл бұрын
Can this be solved using policy based data structure ?
@gurharpartapdhaliwal3087
@gurharpartapdhaliwal3087 4 жыл бұрын
We can do this in 4-5 lines using PBDS bt obv. they will not allow us to use PBDS.
@arshadmanxoori07
@arshadmanxoori07 Жыл бұрын
How can i think that, it will be solve using merge sort ??
@shashankbhattacharya5861
@shashankbhattacharya5861 2 жыл бұрын
I am facing one issue with the code. would you care to have a look at my code? if yes, should i post it here or in a file i can attach?
@ankitkushwaha6269
@ankitkushwaha6269 2 жыл бұрын
Simple code in java public class Solution { static long count = 0; public static long getInversions(long arr[], int n) { mergeSort(arr, 0, arr.length - 1); return count; } public static long[] mergeSort(long[] arr, int lo, int hi){ if(lo == hi){ long[] base = new long[1]; base[0] = arr[lo]; return base; } int mid = (lo + hi) / 2; long[] left = mergeSort(arr, lo, mid); long[] right = mergeSort(arr, mid + 1, hi); long[] merge = merge2Sort(left, right); return merge; } public static long[] merge2Sort(long[] left, long[] right){ int i = 0; int j = 0; int k = 0; long[] merge = new long[left.length + right.length]; while(i < left.length && j < right.length){ if(left[i]
@mohitratanpal27
@mohitratanpal27 2 жыл бұрын
thx bro actually i did merge sort like this,but the video showed a different method,which after 45 min of debugging I understood😅
@sjaishree2275
@sjaishree2275 2 жыл бұрын
guys one question ,how long did it take for you to solve one striver solution(some say it depends on question ,in that case tell avg time or time for this sol)
@ishaanverma1969
@ishaanverma1969 3 жыл бұрын
Can't we start from the right and hash the digits which occur, and then for each digit we encounter, check the dictionary to find out how many digits to the right are smaller than the current digit, and increment count by that number? This would take O(10) i.e. O(1) space and O(10n) i.e. O(n) time?
@takeUforward
@takeUforward 3 жыл бұрын
Areh areh numbers can be huge, its not only 0-9
@ishaanverma1969
@ishaanverma1969 3 жыл бұрын
Oh right!
@breakthecode8323
@breakthecode8323 2 жыл бұрын
Thanks For Your Efforts
@kaashplays1393
@kaashplays1393 2 жыл бұрын
I didn’t understand why copying the temp array to the original array was necessary, since we already have the count of inversion and we can return it without modifying the original array. Anyone can help…?
@SreyesSrinivasan
@SreyesSrinivasan 2 жыл бұрын
Fabulous solution!
@hunnysunaria2450
@hunnysunaria2450 3 жыл бұрын
can we do this without using temp array??
@gautamarora6516
@gautamarora6516 3 жыл бұрын
Yes, you can. Use the concept of shell sort.
@roushanraj8530
@roushanraj8530 4 жыл бұрын
Great work bro, please never stop these explanation videos 💯💯
@karthikpaladugula5424
@karthikpaladugula5424 3 жыл бұрын
Segmentation fault: (Help !!!) long long int inversionCount(long long arr[], long long N) { if(N==1) return 0; long long mid = (N-1)/2; long long s1=mid+1; long long left[s1]; for(int i=0;i
@zahidcoc4646
@zahidcoc4646 4 жыл бұрын
Following from bangladesh..😇😇
@vartikasinghania9723
@vartikasinghania9723 3 жыл бұрын
amazing explanation, thank you for your efforts:)
@ravishankar9247
@ravishankar9247 2 жыл бұрын
Note: Please start solving this SDE sheet if and only if you have studied DSA atleast twice or else you won't be able to understand even through the video solution of the problem.
@saunaknandi1814
@saunaknandi1814 2 жыл бұрын
What is the thought process behind the Merge sort?
@satvrii
@satvrii Жыл бұрын
Kitna accha samjhaate ho 🥲🥲💋
@codingaid2753
@codingaid2753 2 жыл бұрын
Such a easy way of explanation ThanKs @ striver
@shivampokhriyal_
@shivampokhriyal_ 3 жыл бұрын
MergeSort approach : 3:04
@pradeepsaravanau
@pradeepsaravanau 2 жыл бұрын
javascript solution function mergeSortAndCount(arr) { let count = 0; helper(arr); return count; function helper(nums) { if (nums.length right[rightIndex]) { count += left.length - leftIndex; rightIndex++; } else { leftIndex++; } } leftIndex = 0; rightIndex = 0; let sortedArray = []; while (leftIndex < left.length || rightIndex < right.length) { if (leftIndex >= left.length || left[leftIndex] > right[rightIndex]) { sortedArray.push(right[rightIndex]); rightIndex++; } else { sortedArray.push(left[leftIndex]); leftIndex++; } } return sortedArray; } }
@sahilchoudhary1473
@sahilchoudhary1473 2 жыл бұрын
Great explanation
@ritikdubey3338
@ritikdubey3338 3 жыл бұрын
The way you teach, how can anyone one not understand 🔥🙏
@harshkumargupta8538
@harshkumargupta8538 3 жыл бұрын
just the way i did.
@xd9050
@xd9050 Жыл бұрын
A small tip for those who are watching this for the first time -understand merge sort properly before jumping into this
@mukulbindal2303
@mukulbindal2303 4 жыл бұрын
Bro, can you tell me whether waqar ahmed is Ses or PP .. plz I can't contact him anywhere
@aadityaupadhyay6264
@aadityaupadhyay6264 4 жыл бұрын
PP
@voldemort576
@voldemort576 3 жыл бұрын
what is sess or pp
@willturner3440
@willturner3440 3 жыл бұрын
First video I used to watch at ×0.75
@rajatagrawal7016
@rajatagrawal7016 2 жыл бұрын
thanks for the explanation
@atharvameher5880
@atharvameher5880 Жыл бұрын
Yo CAME UP WITH MY OWN SOLUTION Checks from back of array and maintains a stack of Smaller elements, if larger count is incremented Its like the naive approach but optimised class Solution { // arr[]: Input Array // N : Size of the Array arr[] //Function to count inversions in the array. static long inversionCount(long arr[], long N) { long count=0; int flag=0; ArrayListal=new ArrayList(); al.add(arr[(int)N-1]); for(int i=(int)N-1;i>=0;i--){ if(arr[i]
@nimishgupta8357
@nimishgupta8357 4 жыл бұрын
Cant we use set for the same? It will be quite easy to implement.
@takeUforward
@takeUforward 4 жыл бұрын
how do you count numbers on left ? Subtracting iterators take O(n)
@nimishgupta8357
@nimishgupta8357 4 жыл бұрын
@@takeUforward It means with set the complexity will become O(n2) right. Thanks bhai
@takeUforward
@takeUforward 4 жыл бұрын
Nimish Gupta yes bro.
@ardhendureja3785
@ardhendureja3785 4 жыл бұрын
No bhaiya set ke lower bound nikal lunga and deklunga usse bara elements kitne hai jo usse pahle aya hai
@ardhendureja3785
@ardhendureja3785 4 жыл бұрын
Time complexity is nlogn
@rhythmbhatia8906
@rhythmbhatia8906 2 жыл бұрын
Awesome explanation!
@ashwinvarma9349
@ashwinvarma9349 4 жыл бұрын
bro, at line 52 and 53 why do we need to increment the count? only the 'merge' function is incrementing and returning the count not the '_mergeSort' func, right?
@remmargorpp
@remmargorpp 4 жыл бұрын
line 52 and 53 keep track of total count while the merge function counts the inv_count for the two current merging array and return it to line 52 and 53 adding up to total count.
@aditikhandelwal135
@aditikhandelwal135 4 жыл бұрын
@takeUforward In interview thwy always ask us to code it or only explanation is enough???
@takeUforward
@takeUforward 4 жыл бұрын
Explanation, then the code
@code.tle_
@code.tle_ 2 жыл бұрын
understood clearly..
@harshdasila6680
@harshdasila6680 2 жыл бұрын
GFG SOLUTION: C++ Public: long long int mergeIt(long long arr[], long long temp[], long long left, long long mid, long long right) { long long inv_count=0; long long i=left; long long j =mid; long long k =left; while((i
@varshasingh6627
@varshasingh6627 2 жыл бұрын
I didn't understand why we have counted the inversions so many times instead of one time in merge sort?
@priyabratroutray8184
@priyabratroutray8184 2 жыл бұрын
you can pass a reference variable which only updates at the merge function void merge(long long *arr,long long *temp,int s,int mid,int e,long long int& x) { int i=s; int j=mid+1; int k=s; while(i
@rocklee3254
@rocklee3254 3 жыл бұрын
I thought this one and minimum no. of swaps required to sort an array are same
@yashjain1492
@yashjain1492 3 жыл бұрын
Thank you very much👍
@ashwinvarma9349
@ashwinvarma9349 3 жыл бұрын
whats wrong in the code below? #include using namespace std; int merge(vector& nums, int start, int mid, int end){ int count = 0; int n1 = mid - start + 1; int n2 = end - mid; vector leftArray(n1); vector rightArray(n2); for(int i = 0; i < n1; i++){ leftArray[i] = nums[start + i]; } for(int i = 0; i < n2; i++){ rightArray[i] = nums[mid + 1 + i]; } int i = 0, j = 0, k = start; while(i < n1 && j < n2){ if(leftArray[i] = end) return 0; int count = 0; int mid = (start + end) / 2; count += mergeSort(nums, start, mid); count += mergeSort(nums, mid + 1, end); count += merge(nums, start, mid, end); return count; } int countInversions(vector& nums) { return mergeSort(nums, 0, nums.size() - 1); //return nums; }
@yasaswinibingimalla4602
@yasaswinibingimalla4602 Жыл бұрын
Thank you sir
@anshumaan1024
@anshumaan1024 Жыл бұрын
better than gfg's explaination
@bhushanbhosale2394
@bhushanbhosale2394 2 жыл бұрын
bro link is wrong on sheet for this video...please look into it
@sheikhaman6218
@sheikhaman6218 4 жыл бұрын
Legends are watching this during online class🤣🤣 cos your way of solving a problem is so addictive
@takeUforward
@takeUforward 4 жыл бұрын
Haha, what if your prof sees this comment 🤣
@sheikhaman6218
@sheikhaman6218 4 жыл бұрын
@@takeUforward then he will reward me with great internal marks, you know what i mean🤪🤣🤣
@funenjoynilaypatel4553
@funenjoynilaypatel4553 2 жыл бұрын
understood nicely 1-Aug-2022
@hemanthvarmas
@hemanthvarmas 4 жыл бұрын
Great Explanation as always! Thanks anna.😅
@abhishekh_p2721
@abhishekh_p2721 Жыл бұрын
This is awesome ❤
@sanjeevchandranm2818
@sanjeevchandranm2818 2 жыл бұрын
I used this algo but one of the case failed while in coded in codeninja
@goswami46457
@goswami46457 4 жыл бұрын
Great explanation sir
@ManojKumar-bk1kp
@ManojKumar-bk1kp 4 жыл бұрын
Please, continue with this series closely following your videos.
@gouravkumarshaw5467
@gouravkumarshaw5467 2 жыл бұрын
great explanation : D
@rohitjaiswal2563
@rohitjaiswal2563 4 жыл бұрын
You help us a lot!!!
Count Inversions in an Array | Brute and Optimal
24:17
take U forward
Рет қаралды 229 М.
Count Subarrays with Xor as K | This problem clears a lot of concepts
16:50
小蚂蚁会选到什么呢!#火影忍者 #佐助 #家庭
00:47
火影忍者一家
Рет қаралды 120 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 262 #shorts
00:20
LeetCode: The Worst Thing to Happen to Software Engineering
8:03
Coding with Dee
Рет қаралды 135 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 622 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 893 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 270 М.
Counting inversions in an array
19:03
Techdose
Рет қаралды 92 М.
What is Competitive Programming?
6:26
William Lin (tmwilliamlin168)
Рет қаралды 2,5 МЛН
WHY IS THE HEAP SO SLOW?
17:53
Core Dumped
Рет қаралды 252 М.
小蚂蚁会选到什么呢!#火影忍者 #佐助 #家庭
00:47
火影忍者一家
Рет қаралды 120 МЛН