i have been asked this question in my intuit online assesement for an intern role... and i know how inversion count principal works..so i did it comfortably.. but that too only bcoz of old video of striver.... thanks bhya ..love u
@sanjayav6461 Жыл бұрын
Did you clear the interview?
@shubhamagarwal14343 ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India.
@junaid617475 ай бұрын
The amount of clarity you have over concepts is truly remarkable! Hope to be as good as you one day.
@nirupamsuraj1634 Жыл бұрын
Those people wondering why cant we just multiply a 2 in the count inversion logic. Because if we compare with twice the values, the array doesnot end up being sorted in each iteration, which will contradict our assumption. Try yourselves with different testcase, you will get it.
@HarshKumar-ip5nr Жыл бұрын
Where I am missing, can you pin point with example class Solution { private: int count=0; void merge( vector &nums, int start, int mid, int end){ vector temp; int i=start; int j=mid+1; while( i
@nirupamsuraj1634 Жыл бұрын
@@HarshKumar-ip5nr bro try not doing temp.pushback inside nums[i] >2*nums[j]. It is not the logic for sorting. nums[i] >2*nums[j] should be handled inside another while loop seperately, and just increase your count there and nothing else. Keep the while loops for sorting as it is in merge sort.
But I have not changed the original logic of merge sort but I write this new logic separately inside that else in if-else. So can any one tell me why it is still wrong because now it is not disturbing sorting. okay I got it
@ravishkumar6060 Жыл бұрын
The energy with you explain the problem and it's solution is really amazing Sir!!! Your teaching methodology is so simple and convincing that even if you make 1-2hr video for a problem, I can watch it with 100% attention...
@AdityaSharma-er3gs4 ай бұрын
25:09 Here you can also use private static int countPairs(int[] nums, int s, int m, int e){ int i = s; int j = m+1; int count = 0; while(i
@12anikeshguhaxa53 күн бұрын
i was also using this logic but no this doesnt work ...maybe i am doing something wrong
@sohilpathan9006 Жыл бұрын
In Leetcode it will give buffer overflow because int can't store 2x value of INT_MAX So u can use nums[i] > (long long)2*nums[right] OR 0.5*nums[i] > nums[right] any one of them in while loop condition in countPairs.
@shashanktripathi6277 Жыл бұрын
use typecasting
@wttc411 ай бұрын
or you can do like this also: nums[i] > 2LL * nums[right]. (we're comparing int to long long) To be more safe, do this: 1LL * nums[i] > 2LL * nums[right]
@shivanshpatel18989 ай бұрын
@@wttc4 this worked and code is submitted but can you please explain how it works
@wttc49 ай бұрын
@@shivanshpatel1898 simply 2 means 2 is int, whereas 2LL means 2 is of type long long. 1LL * nums[i] means we are multiplying long long and int, so 1LL * nums[i] is of type long long. Similarly, in some cases, you can also do 0LL + some_int to make the type of result to long long.
@lakshsinghania6 ай бұрын
@@wttc4 so basically the whole multiplication is taking place in long long i assume right ?
@namanchandak1532 Жыл бұрын
here is more optimized code you also should apply binary search in the counting pair function instead of increasing right because right portion of the array is sorted int countP(vector& nums, int l, int m, int h) { long long int right=m+1,cnt=0; for(int i=l;i
@sunny_235615 ай бұрын
yup... ease this using upper bound
@yashwanthyerra282014 күн бұрын
But it is taking more time to execute than this class Solution { private int countAndMerge(int[] nums, int left, int mid, int right) { int count = 0; int j = mid + 1; for (int i = left; i
i did this question during my placement prep and yes i saw with my naked eyes how we can make use of merge sort and in general count inversion in nlogn time :) Good work striver!
@cinime Жыл бұрын
Understood! Another super amazing explanation as always, thank you so so SO much for your effort!!
@mdzaidhassan4263 ай бұрын
Your effort in making us understand the whole approach is commendable! Thanks a lot :)
@neerajkumar-ts6om3 ай бұрын
Thanks bhai maja aya question solve krke, pata nhi kaise batau kitna maza aarha hai code karke abb.
@shyampawar88933 ай бұрын
Perfectly Explained and Best series
@aafilburner Жыл бұрын
a[i] > 2* a[right] condition may cause overflow... rewrite as 0.5 * a[i] > a[right]
@saisri5953 Жыл бұрын
thanks
@harshjha1616 Жыл бұрын
it worked thanks
@SairamSudiReddy Жыл бұрын
or cast 2*a[right] to long
@YN__YN Жыл бұрын
thanks a lot
@wttc411 ай бұрын
or you can do like this also: nums[i] > 2LL * nums[right]. (we're comparing int to long long) To be more safe, do this: 1LL * nums[i] > 2LL * nums[right]
@juhichoudhary928111 ай бұрын
Understood, You are The Best Striver
@ksankethkumar7223 Жыл бұрын
Toughest problem understood as a piece of cake..
@jeeveshkumar96825 ай бұрын
i thought of sorting the whole array and then using two pointers to compare the values and increase the count accordingly. But the problem with this solution is that the constraint of keeping i
@shikher45593 ай бұрын
are you talking about thIs solution ? public int merge(int[] arr, int low, int mid, int high){ int left = low; int right = mid+1; int pairs = 0; ArrayList temp = new ArrayList(); while(left
@btechstudent16203 ай бұрын
@@shikher4559 yes is this working ?
@shikher45593 ай бұрын
@@btechstudent1620 its working with a bit modification 1) run a loop for calculating pairs separately 2) do the same as this code
@078_garlapatilikhitreddy83 ай бұрын
@@shikher4559counting the pairs seperately is fine i thought while merging we can modify the count as with counting inversion. After doing a dry run i understood that I have count the pairs seperately and then do the merging
@dayashankarlakhotia4943 Жыл бұрын
Understood your previous explanation and current explanation both are good
@cricketobserver178 Жыл бұрын
Medium questions are a combination of two easy questions And hard questions are combination of two medium problems That's my observation till now
@055_meetmakwana97 ай бұрын
Understood, You are The Best Man!
@ShahNawaz-cx3pi4 ай бұрын
Count Pair function can also be implemented as:- int CountPairs(vector&arr,int low,int mid, int high){ int i = low; int j = mid+1; int cnt = 0; while(i
@abhinavraj65073 ай бұрын
Did your code get accepted ?
@shaikhmoin8492 ай бұрын
@@abhinavraj6507 yes this will work.
@242deepak Жыл бұрын
Easier way: class Solution { public: int merge(int l,int mid,int r,vector &nums) { vector temp; int i=l,j=mid+1,count=0; while(i
@abhijeettripathi7615 Жыл бұрын
actually that should be the way it will have analogy between count inverison and this
@Manishgupta200 Жыл бұрын
According to me, it's a medium level question... because I understood clear concept of mergeSort by your tutorial. Thankyou Striver
@vikashH-h1vАй бұрын
int countPairs(vector &arr, int low, int mid, int high) { int right = mid + 1; int cnt = 0; for (int i = low; i
@narasimhann2814 Жыл бұрын
Can someone tell why the below fails. It clears 33 TC but not all. Its a modification to no of inversion pair where for each element in right I find the left array range to satisfy the condition int merge(vector&arr, int start,int mid,int end){ int n1 = mid-start+1; int n2 = end-mid; int i,j,x; long long count=0; vector a1(n1); vector a2(n2); for(x=0,i=start; i
@kaichang81862 ай бұрын
understood, thanks for the great explanation
@RohitKumar-dy2gc5 ай бұрын
thankyou for such a wonderful explanation striver✨✨
@iamxpossible2 ай бұрын
use long long to avoid integer overflow!
@kavyahegade64775 ай бұрын
whle comparing, if one element from 2nd array does not form a pair with an element in 1st array, you said you will miss out the next element. but in reality we do not. because we increment the 1st array i des and check again, if it works then all other works, so count inversion algorithm works
@mohdnabeel7024 ай бұрын
In count inversion we are trying to find the smaller number suppose we are at 6 and 3, we see 3 is smaller, we add 3 to our answer list, and increment the right pointer to compare next pointer with 6, missing out on comparing 3 with the next elements in left array. (this is how I understood).
@vikasbagri1225 Жыл бұрын
Understood it very well Thanks for this amazing video
@paneercheeseparatha3 ай бұрын
Amazing guy!
@AdityaSingh-uy8ms2 ай бұрын
UNDERSTOOD !!
@gyanendrasaurav877 Жыл бұрын
(mid+1)-i will also work just fine but keep that inside the while loop
@nikhilchobhiyal3 ай бұрын
Maamaamiyaa!!!
@studynewthings1727 Жыл бұрын
Awesome explanation.
@inspiringzone12 Жыл бұрын
Great explanation
@akilesh63792 ай бұрын
understood sir
@praveensoni11199 ай бұрын
Can't thank you enough Man!!
@torishi824 ай бұрын
Amazing. Thank you.
@akshaygill4692 Жыл бұрын
can someone explain why the if condition i put in else block would not work here while(left
@shivasai7707 Жыл бұрын
yes i have the same doubt it should work i felt i was the only one
@kishore4262 Жыл бұрын
Can someone explain this.Thanks in advance:)
@mohdnabeel702 Жыл бұрын
Same thing we just have to modify the if condition rather than doing arr[i]>arr[j] Iam using arr[i] > 2*arr[j]; Both are getting accepted.
@mohdnabeel702 Жыл бұрын
@@shivasai7707 It works import java.util.ArrayList; class Solution { public int reversePairs(int[] nums) { return mergeSort(nums,0,nums.length-1); } public static int mergeSort(int[] arr,int lo, int hi){ if(lo >= hi) return 0; int mid = (lo + hi)/ 2; int counter=mergeSort(arr,lo,mid); counter+=mergeSort(arr,mid+1,hi); counter+=countPairs(arr,lo,mid,hi); merge(arr,lo,mid,hi); return counter; } public static void merge(int []arr,int lo,int mid,int hi){ List tempList = new ArrayList(); int i = lo; int j = mid+1; while(i
@kishore4262 Жыл бұрын
Hey @@mohdnabeel702 ; can you explain me whats wrong with this: class Solution { public: void merge(vector &nums,int low,int mid,int high,int &c){ int i = low; int j = mid+1; vector temp; while(i
@shashvatsinghal25744 ай бұрын
I did this using a multiset, got a time complexity of O(3nlogn) but still i got tle "class Solution { public: int reversePairs(vector& nums) { multiset ms; for(auto i:nums) ms.insert(i); long long final=0; for(int i=nums.size()-1;i>=0;i--){ auto it=ms.find(nums[i]); ms.erase(it); auto lb = ms.lower_bound((2 * (long long)nums[i])+1); final += distance(lb, ms.end()); } return final; } };"
@DeadPoolx17123 ай бұрын
UNDERSTOOD;
@harshilpatel32053 ай бұрын
Understood 🙏🏻
@googleit2490 Жыл бұрын
Understood :) Aug'9, 2023 09:11 pm
@YourCodeVerse11 ай бұрын
Understood✅🔥🔥
@venup28139 ай бұрын
understood striver💥
@umangsharma73643 ай бұрын
Wondering if we can solve this through TreeSet
@aniketkhorwal72898 ай бұрын
thanks striver💙
@shubha_jagadeeshАй бұрын
understood 💗
@AjaySaini-hc9ho9 ай бұрын
Thanks brother!❤
@akashsingh930110 ай бұрын
THANKU BHAI
@hemantpatel14135 ай бұрын
Understood.
@AppaniMadhavi9 ай бұрын
Superb
@mohan-xx8ih Жыл бұрын
Leetcode solution class Solution { void merge(vector &nums, int low, int mid, int high){ int *temp = new int[high - low + 1]; int i = low; int j = mid + 1; int k = 0; while((i
@anshbathla38503 ай бұрын
Am I the only one who understood everthing as crystal clear till dry run but during code the mind stopped working
@anithusiast.2 ай бұрын
happened initiallty will take time ..learn basics of coding first
@harshitpatil61389 ай бұрын
Ubderstood ❤
@aayush5474Ай бұрын
am i tripping or he keeps saying merge short?
@sarangkumarsingh79018 ай бұрын
Understood.............
@suheabkhan2546 Жыл бұрын
Great explanation.. understood.... Why is the notes for the previous videos , are linked to old articles.. will the new articles replace them or new articles will not be written for them?
@takeUforward Жыл бұрын
The articles are new, if you go though them, they are updated... We did not create a new article, instead updated the older ones.
@AmanSingh-yc4cw8 ай бұрын
Understood 👍
@HoneySingh-yp7zc6 ай бұрын
superb
@ashnakohli67634 ай бұрын
class Solution { public int reversePairs(int[] nums) { return countInversionsRec(nums); } int countInversionsRec(int[] arr){ int l=arr.length; if(l
@anshulrai6058Ай бұрын
understood👍
@Appcgubn6 ай бұрын
why cant we use treemap and traverse through the array and store the occurences of the number and checking them upto the condition verifies in the tree map
@dewanandkumar858911 ай бұрын
Understood
@AmanKumarSinhaOfficial Жыл бұрын
Please make an {Android App Development} course
@takeUforward Жыл бұрын
I am a full stack dev, I have no experience in it 😅
@tapansonak1431 Жыл бұрын
@@takeUforward Please make a full stack web dev course
@lakshsinghania6 ай бұрын
@@tapansonak1431 There are diff courses on yt watch out that for instance hitesh choudhary, and some foreign yt channels/ udemy courses which are good
@avijeetswain7101 Жыл бұрын
25:00 can somebody help me please? Here we are using a zero index array so when we are doing (right -(mid+1)) can't we write it as (right-(mid+2)) we also did in count inversion (mid -(left+1)). Or (right-(mid+1)) means how far right is from its initial position mid+1 ??
@S.s-tz9fo Жыл бұрын
the right pointer stops at one place greater , we are adding total number of elements before the right pointer(excluding right)
@avijeetswain7101 Жыл бұрын
Thanx brother
@shivasai7707 Жыл бұрын
im totally confused if 6 is greater than twice of 2 then 13 21 25 will be greater than twice of 2 as they are greater count of inversions method should work striver please help thanks:)
@nirupamsuraj1634 Жыл бұрын
Because if we compare with twice the values, the array doesnot end up being sorted in each iteration, which will contradict our assumption.
@abhijeettripathi7615 Жыл бұрын
your code will work correct thought process
@nirupamsuraj1634 Жыл бұрын
@@abhijeettripathi7615 the code will work as long as you seperate the counting logic from sorting logic. In sorting logic dont compare with twice the values.
@bhagyashri863 Жыл бұрын
understoood🤩
@reassume4826 Жыл бұрын
I dont get why do we need to put this function before merge method? When I am putting in merge method, it gives wrong answer. Can anyone point out why? I put this logic before actual merge operation inside merge function, still giving wrong answer.
@PriyaSharma-v8u Жыл бұрын
Understood!
@jeetoppers31506 ай бұрын
understood 😍😍😍😍😍
@sarvansingh6736 Жыл бұрын
Got it
@AbjSir Жыл бұрын
understood
@culeforever5408 Жыл бұрын
understood :)
@gauravkabdwal309 Жыл бұрын
Understood
@akshatverma8086 Жыл бұрын
Hey @striver , recently I started doing ur a2z DSA sheet. After learning array from ur playlist I am doing string but the problem I am able to solve the questions even medium level. What approach should I apply. I give 20 mins to understanding n more and then code. It gives results 60%but not 💯. How should I start?
@vishwasgupta6149 Жыл бұрын
start by learning english
@____priyanka_b_ Жыл бұрын
Can you please explain same codes in java
@priyotoshsahaThePowerOf237 ай бұрын
In leet code its taking 232 MB ..means temp arr taking extra spaces right striver??
@anuragroy9045 Жыл бұрын
#Understood
@saillaanil6948 Жыл бұрын
can anyone please help me, why this code is not working int merge(vector &arr, int low, int mid, int high) { vector temp; // temporary array int left = low; // starting index of left half of arr int right = mid + 1; // starting index of right half of arr int count=0; //storing elements in the temporary array in a sorted manner// while (left
@abhijeettripathi7615 Жыл бұрын
see do the sorting and counting separately
@saillaanil6948 Жыл бұрын
@@abhijeettripathi7615 why can't we do it together
@edu_77218 Жыл бұрын
@@saillaanil6948This is because even if arr[left] > 2*arr[right] is not satisfied, right will be incremented as arr[left] > arr[right]. To calculate cnt, right has to be incremented only when arr[left] > 2*arr[right] is true. So, this requires an extra while loop.
@vikasgupta1828 Жыл бұрын
Thanks
@GeneralistDev Жыл бұрын
Why below code doesn't work ? class Solution { int cnt = 0; void merge(vector &nums, int s, int mid, int e) { vector temp; int i = s, j = mid + 1; while (i
@aayushichouhan775 Жыл бұрын
understood
@akshayvakharia75527 ай бұрын
Hi @takeUForward UNDERSTOOD. But can we align with the previous solution by tricking a bit like else { //For this question condition change int tempPointer = leftPointer; while (tempPointer (2 * input[rightPointer]))) { tempPointer++; } if (input[tempPointer] > (2 * input[rightPointer])) { count += (mid - tempPointer + 1); } // Existing Merge code list.add(input[rightPointer]); rightPointer++; }
@pc8803 Жыл бұрын
Hi Striver, I am Btech CSE 4th sem student. I have been following your KZbin channel for a 2-3 weeks now. I had a doubt about your SDE Sheet and A-Z DSA sheet .That which one should I complete first I already know basics of Cpp, Java and DSA ( I had started doing 375Qs sde sheet of apna college ) I was able to solve questions of problem but my solutions were not optimal approach . Please help.
@113_manshi4 Жыл бұрын
Follow his A2Z sheet.
@kamleshverma9724 Жыл бұрын
will this code work for already sorted array?? because for sorted array, every left element will be smaller than element in right/right array???
@OrderEmperor Жыл бұрын
😂😂there will be no pair in that case so obviously count will be 0
@vibhutijoshi0722 күн бұрын
cant we do this using map?
@kurma.49329 күн бұрын
how?
@abhimanyuthapliyal58232 ай бұрын
US
@luckshaeey Жыл бұрын
understood :)
@GeetainSaar4 ай бұрын
😊😊
@m.rnaganathan7492 Жыл бұрын
how to show all the pairs can you help me out or anyone pls
@alphaCont11 ай бұрын
ok
@karthikarthik1795 Жыл бұрын
Sir it is also showing time limit exeeded error
@satvrii Жыл бұрын
❤❤❤
@apoorva5961Ай бұрын
😇
@ananthalakshmi8 ай бұрын
int cnt = 0; class Solution { public: void merge(vector & nums, int left, int mid, int right){ int i = left; int j = mid + 1; int k = 0; int ans_arr[right - left + 1]; while(i
@banothutharun27438 ай бұрын
wow
@prajwalshaw9217 Жыл бұрын
void merge(vector&arr,int low,int mid,int high) { vectortemp; int left=low,right=mid+1; while(left
@prajwalshaw9217 Жыл бұрын
Can anybody say what is wrong with my code...?...it's giving wrong answer.