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
@vaibhavsethia704 жыл бұрын
Understood
@sahilchoudhary14734 жыл бұрын
Understood
@ashishdwivedi51904 жыл бұрын
Hey u forgot to upload link to this video on sde sheet....
@mafiaco12574 жыл бұрын
No 🥺
@pleasesirmorevideos46844 жыл бұрын
Understoood
@shardulkhadye58752 жыл бұрын
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.
@codding32world502 жыл бұрын
Yes I was stuck for whole day literally then came here😢
@aakashmehta94742 жыл бұрын
Not at all possible to figure out this solution unless you are genius 😃
@ritikashishodia6752 жыл бұрын
Maine to phele kiya tha ye to jb dubara kiya after months nahi hua.😔😔
@garvgoel1743 Жыл бұрын
@@ritikashishodia675 This is very normal 😂
@mdhaidarparwez968 Жыл бұрын
For this type of problem, you should make a note and revise weekly
@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!
@yashisrivastava54824 жыл бұрын
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
@takeUforward4 жыл бұрын
Yashi Srivastava glat that you wait, this things keeps me motivated!!
@abhijitroy19582 жыл бұрын
The alpha rule : never lose the concentration while watching striver's video . Best explanation
@rohitanjane16682 жыл бұрын
Highly agree with you 👍🏻🙌🏻
@neerajsinghjr2 жыл бұрын
Seriously, this guy is Awesome. but its also true... you can't doze off for a single bit. If you do then rewind ...
@baljotsinghchoudhary74343 жыл бұрын
this question is very interesting one should try to solve with all approches merge sort, fenwick and trie
@tanayshah2753 жыл бұрын
Great Explanation! One more thing if anyone is implementing it then the invertion count will increase by mid - i + 1
@tarungopal30652 жыл бұрын
@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)
@tusharvaish80962 жыл бұрын
Why.? Can you explain.
@tusharvaish80962 жыл бұрын
@Anand Pandey why we need to add 1 ? Can you explain
@mohitsingh77932 жыл бұрын
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); } }
@shreeramgoyal843 жыл бұрын
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?
@prashantbisht22193 жыл бұрын
Yeah but wouldn't it be O(n^2).For every element you traversing the entire queue in worst case.
@pranav288 Жыл бұрын
@@prashantbisht2219 yup
@krisnerdscave3942 жыл бұрын
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?
@krisnerdscave3942 жыл бұрын
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 :)
@vershamishra87632 жыл бұрын
@@krisnerdscave394 yes, Thank you for the first statement, I got confused then this helped me to clarify.
@shivalikagupta34332 жыл бұрын
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!!!
@devanshmesson27774 жыл бұрын
You Speak So Clearly! Great Video Quality and Awesome Explaination!!
@dhanubhardwaj69353 жыл бұрын
why not inv_count=mid-i+1 bcz both i and mid are inclusive
@BattlefieldAWWWClips2 жыл бұрын
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 .
@mohitsingh77932 жыл бұрын
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?
@mdazeen88202 жыл бұрын
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}
@adityasai5502 жыл бұрын
Bro can u tell me from where you learned Fenwick tree. There are not many good videos about Fenwick tree in KZbin
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..
@takeUforward4 жыл бұрын
Thanks! Cheers!!
@aasthajain42104 жыл бұрын
You are so hardworking man!! I just look up to this✨
@rudranilacharya52932 жыл бұрын
Can we use ordered set in interviews, this ques is much easier with ordered set
@SurajVerma-lx6ik4 жыл бұрын
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 🤔
@jayeshgupta86184 жыл бұрын
Bro when you solve these questions on leetcode then leetcode suggest similar questions , just do one or two from the suggestions maybe .
@SurajVerma-lx6ik4 жыл бұрын
@@jayeshgupta8618 uske liye leetcode premium lena padega kya ????
@sasiraj15944 жыл бұрын
@@SurajVerma-lx6ik no
@SurajVerma-lx6ik4 жыл бұрын
@@sasiraj1594 ok
@shivendrsrivastava73154 жыл бұрын
So can we say that the crux is doing inversion is basically sorting ? and merge sort is the most appropriate one
@himanshusinghal52994 жыл бұрын
I think we can use binary search(lower_bound stl) which would take O(nlogn)
@takeUforward4 жыл бұрын
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
@himanshusinghal52994 жыл бұрын
@@takeUforward yeah got it thanx
@KathanShah032 жыл бұрын
@@takeUforward is there any other method to solve this problem using hash/set/maps?
@shanayaarora1983 жыл бұрын
Happy Teachers Day :) Thanks for such a great content.
@venuvenu27192 жыл бұрын
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.
@sak35792 жыл бұрын
can't sort the array
@brahmkaransingh88042 жыл бұрын
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
@acxd4 жыл бұрын
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.
@takeUforward4 жыл бұрын
I did not want to modify because a bunch kf students follow gfg 😅
@smartwaytolearn2425 Жыл бұрын
why can't we solve this with gap method??? please reply.......
@ranasauravsingh2 жыл бұрын
UNDERSTOOD... !!! Thanks striver for the video... :)
@amitsamratmaurya31703 жыл бұрын
Can you explain why you are using -> (mid-i) isnt it supposed to be (mid-i+1), but why your code works?
@VinayKumar-oi4yq3 жыл бұрын
Yeah, my code worked with mid-i+1 as well
@atmanirbharladka44673 жыл бұрын
I really appreciate you explaining the prerequisite too for the question.
@demonslayer4607 Жыл бұрын
tum dhanush fan ko kya ?
@souravchakraborty97772 жыл бұрын
Would you kindly explain the Fenwick tree method?
@stith_pragya2 жыл бұрын
Thank You very much for providing this video.........👍👍👍👍👍👍
@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; }
@amrithapatil45952 жыл бұрын
I think line number 28 should be inv_count = inv_count + mid - i+1;
@brahmkaransingh88042 жыл бұрын
Yes that was the doubt which i was searching for in the comment section
@brahmkaransingh88042 жыл бұрын
@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 Жыл бұрын
yep, i had the same doubt
@SatyaMantha Жыл бұрын
Striver, i make sure i like and comment on every video that i watch. I am learning a lot from your videos.
@darshantank5542 жыл бұрын
What do you think about a "binary indexed tree" with O(N) ?!?!?!?!?!
@greedycat98453 жыл бұрын
your diagram had made me reailze the core part of the solution.very nice explanatio
@peter9759 Жыл бұрын
Nice video totally understood optimal approach
@Ayush-ir3wi2 жыл бұрын
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??
@pulkitahuja47503 жыл бұрын
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 Жыл бұрын
whats the name of this problem on leetcode?
@saishree88863 жыл бұрын
@takeuforward using global variables gives wrong o/p y?But doing the same by adding subtrees gives the crct o/p
@bhaveshkumar68422 жыл бұрын
Thank you so much bro!!!!!
@goodpeople76154 жыл бұрын
Awesome content shows how good preparation it needs and you nail every time . Happy teachers day ʕ´•ᴥ•`ʔ
Nice explanation, I would love to see more videos on critical questions
@maulikdev50402 жыл бұрын
Amazingly taught the concept. Thank you so much!!
@joyalmeida65653 жыл бұрын
why do we need temp array? I'm not getting in what way will the existing array will be modified. Please Explain
@yatinarora12523 жыл бұрын
U can watch merge sort it that u need a array so that you can basically store your elements and find comparison
@ankitdubey93103 жыл бұрын
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?
@nuclearnadal693 жыл бұрын
Bro this approach sounds more brute than the brute force itself
@ankitdubey93103 жыл бұрын
@@nuclearnadal69 my programming 4 months back was "brutal" lol 😂😂
@MallistParasite3 жыл бұрын
The Struggle Between Sort and Shot.Very Evident😛. Great Explaination.Understood.
@farazahmed72 жыл бұрын
bolo aunty tau kya
@nayanbhuyan81563 жыл бұрын
Your sde sheet link for this video is not working, directing the spoj
@SHASHANKRUSTAGII3 жыл бұрын
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?
@farazahmed72 жыл бұрын
no one can come up with the optimal soln until and unless you have already solved it before.
@SHASHANKRUSTAGII2 жыл бұрын
@@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
@aniketdubey73632 жыл бұрын
Can this be solved using policy based data structure ?
@gurharpartapdhaliwal30874 жыл бұрын
We can do this in 4-5 lines using PBDS bt obv. they will not allow us to use PBDS.
@arshadmanxoori07 Жыл бұрын
How can i think that, it will be solve using merge sort ??
@shashankbhattacharya58612 жыл бұрын
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?
@ankitkushwaha62692 жыл бұрын
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]
@mohitratanpal272 жыл бұрын
thx bro actually i did merge sort like this,but the video showed a different method,which after 45 min of debugging I understood😅
@sjaishree22752 жыл бұрын
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)
@ishaanverma19693 жыл бұрын
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?
@takeUforward3 жыл бұрын
Areh areh numbers can be huge, its not only 0-9
@ishaanverma19693 жыл бұрын
Oh right!
@breakthecode83232 жыл бұрын
Thanks For Your Efforts
@kaashplays13932 жыл бұрын
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…?
@SreyesSrinivasan2 жыл бұрын
Fabulous solution!
@hunnysunaria24503 жыл бұрын
can we do this without using temp array??
@gautamarora65163 жыл бұрын
Yes, you can. Use the concept of shell sort.
@roushanraj85304 жыл бұрын
Great work bro, please never stop these explanation videos 💯💯
@karthikpaladugula54243 жыл бұрын
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
@zahidcoc46464 жыл бұрын
Following from bangladesh..😇😇
@vartikasinghania97233 жыл бұрын
amazing explanation, thank you for your efforts:)
@ravishankar92472 жыл бұрын
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.
@saunaknandi18142 жыл бұрын
What is the thought process behind the Merge sort?
The way you teach, how can anyone one not understand 🔥🙏
@harshkumargupta85383 жыл бұрын
just the way i did.
@xd9050 Жыл бұрын
A small tip for those who are watching this for the first time -understand merge sort properly before jumping into this
@mukulbindal23034 жыл бұрын
Bro, can you tell me whether waqar ahmed is Ses or PP .. plz I can't contact him anywhere
@aadityaupadhyay62644 жыл бұрын
PP
@voldemort5763 жыл бұрын
what is sess or pp
@willturner34403 жыл бұрын
First video I used to watch at ×0.75
@rajatagrawal70162 жыл бұрын
thanks for the explanation
@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]
@nimishgupta83574 жыл бұрын
Cant we use set for the same? It will be quite easy to implement.
@takeUforward4 жыл бұрын
how do you count numbers on left ? Subtracting iterators take O(n)
@nimishgupta83574 жыл бұрын
@@takeUforward It means with set the complexity will become O(n2) right. Thanks bhai
@takeUforward4 жыл бұрын
Nimish Gupta yes bro.
@ardhendureja37854 жыл бұрын
No bhaiya set ke lower bound nikal lunga and deklunga usse bara elements kitne hai jo usse pahle aya hai
@ardhendureja37854 жыл бұрын
Time complexity is nlogn
@rhythmbhatia89062 жыл бұрын
Awesome explanation!
@ashwinvarma93494 жыл бұрын
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?
@remmargorpp4 жыл бұрын
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.
@aditikhandelwal1354 жыл бұрын
@takeUforward In interview thwy always ask us to code it or only explanation is enough???
@takeUforward4 жыл бұрын
Explanation, then the code
@code.tle_2 жыл бұрын
understood clearly..
@harshdasila66802 жыл бұрын
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
@varshasingh66272 жыл бұрын
I didn't understand why we have counted the inversions so many times instead of one time in merge sort?
@priyabratroutray81842 жыл бұрын
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
@rocklee32543 жыл бұрын
I thought this one and minimum no. of swaps required to sort an array are same
@yashjain14923 жыл бұрын
Thank you very much👍
@ashwinvarma93493 жыл бұрын
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 Жыл бұрын
Thank you sir
@anshumaan1024 Жыл бұрын
better than gfg's explaination
@bhushanbhosale23942 жыл бұрын
bro link is wrong on sheet for this video...please look into it
@sheikhaman62184 жыл бұрын
Legends are watching this during online class🤣🤣 cos your way of solving a problem is so addictive
@takeUforward4 жыл бұрын
Haha, what if your prof sees this comment 🤣
@sheikhaman62184 жыл бұрын
@@takeUforward then he will reward me with great internal marks, you know what i mean🤪🤣🤣
@funenjoynilaypatel45532 жыл бұрын
understood nicely 1-Aug-2022
@hemanthvarmas4 жыл бұрын
Great Explanation as always! Thanks anna.😅
@abhishekh_p2721 Жыл бұрын
This is awesome ❤
@sanjeevchandranm28182 жыл бұрын
I used this algo but one of the case failed while in coded in codeninja
@goswami464574 жыл бұрын
Great explanation sir
@ManojKumar-bk1kp4 жыл бұрын
Please, continue with this series closely following your videos.