Count Inversions in an Array | Brute and Optimal

  Рет қаралды 187,005

take U forward

take U forward

Күн бұрын

Problem Link: bit.ly/3GJcuYj
Notes/C++/Java/Python codes: takeuforward.org/data-structu...
We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
0:00 Introduction of Course
00:40 Problem Statement
02:11 Brute force approach
02:52 Pseudocode
03:35 Complexity
04:05 Optimmal solution
04:22 Intuition
10:33 Approach + Dry-run
18:02 Code
22:37 Complexity

Пікірлер: 198
@takeUforward
@takeUforward 11 ай бұрын
Please watch our new video on the same topic: kzbin.info/www/bejne/d6TIhqCti7OUpbs
@namannema3349
@namannema3349 8 ай бұрын
this link take u to the same video we are watching
@yogeshkaushik8316
@yogeshkaushik8316 4 ай бұрын
@@namannema3349 thats what recursion means
@satvrii
@satvrii Жыл бұрын
Nowadaya strivers voice has become so calm and soft 😅❤
@anuradha3868
@anuradha3868 11 ай бұрын
True...as compared to his previous vdos 😂
@newsbuster2194
@newsbuster2194 9 ай бұрын
😂
@shubhamkumar-hx1fb
@shubhamkumar-hx1fb 7 ай бұрын
​@@anuradha3868 same thought 😂
@saswatrath4646
@saswatrath4646 3 ай бұрын
abe mic kharap tha pehle😂
@anuplohar23
@anuplohar23 7 ай бұрын
Best count inversion video on KZbin, your method of teaching is very best that it gets me understand very easily 🌟🌟🌟
@_AASHISHSHAH
@_AASHISHSHAH Жыл бұрын
this is the best playlist in world. thank you stiver for your effort.
@bmishra98
@bmishra98 25 күн бұрын
Completed the playlist. This was the best recursion playlist I ever went through. Thanks a lot Striver.
@gajendrakumar8271
@gajendrakumar8271 3 ай бұрын
Finally completed this recursion playlist and Thanks a lot striver for great explanation throughout and patiently drawing recursion tree. patience is the key to solve and teach anything . And you have it man and you are teaching that too. Thanks a lot again
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial Жыл бұрын
The best count inversion video on KZbin.. Thanks a lot Raj.. stay blessed❤
@Manishgupta200
@Manishgupta200 Жыл бұрын
I'm trying this problem and solved it by myself by taking count as global variable.. But you taught us in a vary optimal way without taking count as a global variable. Really best optimal approach. Thankyou ❤
@bishakhdutta8427
@bishakhdutta8427 Жыл бұрын
whats the complexity of your solution? can you share it?
@Manishgupta200
@Manishgupta200 Жыл бұрын
@@bishakhdutta8427 same as merger sort merge algorithm
@cinime
@cinime Жыл бұрын
Understood! Super amazing explanation as always, thank you very very very much for your effort!!
@senseiAree
@senseiAree 10 ай бұрын
Striver your voice is very soothing and calm bro ❤.... I use it to sleep at night AND study... and I don't feel sleepy
@akritisneh2849
@akritisneh2849 Жыл бұрын
So much crystal clear!!!!Thank youu❤
@vikasbagri1225
@vikasbagri1225 Жыл бұрын
Understood it very well Thanks for this amazing series
@user-jm6bc3rt5e
@user-jm6bc3rt5e 2 ай бұрын
Understood THE BEST EXPLANATION Excellent playlist 👌 👏 ❤
@prateekgoyal7009
@prateekgoyal7009 Жыл бұрын
This is the best explanation I've ever got.
@cartidise
@cartidise Жыл бұрын
I usually don't comment but wanted to say that just subbed your channel. This is the best explanation I've ever got. THANKS A LOT:>)
@selvaarumugam370
@selvaarumugam370 Жыл бұрын
As usual your teaching jus made coding much easier than it is bruh!! Waiting for Binary Search series bruh!!!!
@aditijagtap7853
@aditijagtap7853 20 күн бұрын
Best teaching approach so far!!!!
@pallavi22222
@pallavi22222 Жыл бұрын
your problem solving approach explanation is superb
@mehulthuletiya497
@mehulthuletiya497 Жыл бұрын
Timestamps: --------------------- 00:40 Problem Statement 02:11 Brute force approach 02:52 Pseudocode 03:35 Complexity 04:05 Optimmal solution 04:22 Intuition 10:33 Approach + Dry-run 18:02 Code 22:37 Complexity
@lakshaysawhney9988
@lakshaysawhney9988 27 күн бұрын
Thoroughly enjoyed the problem!!
@chiragbirla5606
@chiragbirla5606 Жыл бұрын
Best explanation ever for this problem
@mohd786ishaan
@mohd786ishaan 11 ай бұрын
UNDERSTOOD SIR ! GREAT EXPLAINATION
@prateek5208
@prateek5208 11 күн бұрын
such an awesome expalination bhaiya just approach dekhkr hi dimag ma intution agya ki merge ma kase implement krenge , you are best
@shiveshgupta1705
@shiveshgupta1705 Жыл бұрын
i just imagine if all the problems would be available on this channel in future
@SaumyaSharma007
@SaumyaSharma007 Жыл бұрын
OMG Bawal explanation Striver Bhaiya.😃
@unnatishukla8513
@unnatishukla8513 11 ай бұрын
awesome as always!!!!🤩
@pavanjegurupati
@pavanjegurupati 4 ай бұрын
Great explanation!!
@theresilientpianist7114
@theresilientpianist7114 6 ай бұрын
Really it was a great series Striver.🔥🔥
@gauristar4094
@gauristar4094 Ай бұрын
Superb logic, Understood!!!
@ishangujarathi10
@ishangujarathi10 9 ай бұрын
loved the optimal solution, intuition op!
@abhaypatel379
@abhaypatel379 Ай бұрын
thanks striver for making a complex question into very easy question 🤗
@8bit_hero850
@8bit_hero850 4 ай бұрын
Can we realistically solve this if it comes in an interview given we haven't solved it before? I mean how do can you get the intuition of merge sort from this problem? I really don't get it.
@unknown47896
@unknown47896 16 күн бұрын
no way bro
@Sumeet_100
@Sumeet_100 7 ай бұрын
Thank you for this video !!
@GuruPrasadShukla
@GuruPrasadShukla Жыл бұрын
understood the approach sir thanks alot
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
Understood amazing explanation
@sivasaimanchem4399
@sivasaimanchem4399 Жыл бұрын
All i can say is Thankyou so much ❤🙌
@xtzyrox2764
@xtzyrox2764 Жыл бұрын
I am happy with the brute force now I will see optimal 1 week before interview bcz University exams are in this month
@b39_navkaransingh73
@b39_navkaransingh73 Жыл бұрын
Did anyone ask you?
@ishasinghal3457
@ishasinghal3457 8 ай бұрын
Best explanation ever
@NazeerBashaShaik
@NazeerBashaShaik 4 ай бұрын
Understood, thank you.
@the_haryannvi_coder
@the_haryannvi_coder 2 ай бұрын
If u don't wanna use cnt in mergeSort function, you can do this:- int mergeSort(vector &arr, int low, int high) { if (low >= high) return 0; int mid = (low + high) / 2 ; int left = mergeSort(arr, low, mid); // left half int right = mergeSort(arr, mid + 1, high); // right half int m = merge(arr, low, mid, high); // merging sorted halves return left + right + m; }
@HR-pz7ts
@HR-pz7ts 3 ай бұрын
Amazing I solved two questions using the same logic.
@saitehith6647
@saitehith6647 Жыл бұрын
Superb Explanation
@vaibhav56
@vaibhav56 26 күн бұрын
I was struggling with the solution but as soon as you mentioned merge sort it clicked in my mind
@steverogers7050
@steverogers7050 4 ай бұрын
very nice explanation bhaiya
@yashisrivastava6196
@yashisrivastava6196 11 ай бұрын
just wow explanation
@rohitkumarpilania94
@rohitkumarpilania94 Жыл бұрын
Badhiya kaam kar rahe ho, see you soon
@sarangkumarsingh7901
@sarangkumarsingh7901 4 ай бұрын
Nice lecture................
@hashcodez757
@hashcodez757 4 ай бұрын
understood Bhaiya!!
@navdeepkumar9160
@navdeepkumar9160 Жыл бұрын
Great video
@user-ck6kn1ty8x
@user-ck6kn1ty8x 6 ай бұрын
Thank You Bhaiya
@nishant4595
@nishant4595 Ай бұрын
understood!!!
@NoniSabharwal
@NoniSabharwal 3 ай бұрын
The way he said wow! Uff in love with the voice
@grm642
@grm642 6 ай бұрын
Thank you😊
@YourCodeVerse
@YourCodeVerse 9 ай бұрын
Understood✅🔥🔥
@venup2813
@venup2813 Жыл бұрын
Understood brother❤️
@anshasati1920
@anshasati1920 Ай бұрын
Understood Sir🥳
@GhostVaibhav
@GhostVaibhav 5 ай бұрын
Understood🔥
@her_soulmate
@her_soulmate 8 ай бұрын
Understood 🎉
@nikhilprakashahir5730
@nikhilprakashahir5730 5 ай бұрын
so aweesome
@ShubhamKumar-uf3gc
@ShubhamKumar-uf3gc Ай бұрын
loved that bhaiya
@ravalikatalks5285
@ravalikatalks5285 6 ай бұрын
thanks alot bhaiya
@satyamgupta4808
@satyamgupta4808 11 ай бұрын
very nice video
@RituSingh-ne1mk
@RituSingh-ne1mk 7 ай бұрын
Understood!
@abhishekverma4693
@abhishekverma4693 5 ай бұрын
thank you so much for watching
@atharva3571
@atharva3571 7 ай бұрын
I understood the problem
@divyaanandhan6021
@divyaanandhan6021 Жыл бұрын
Thanks 😊👍
@HarshChoudhary-vm6eh
@HarshChoudhary-vm6eh Жыл бұрын
Understood thats great...
@muqeemuddin8057
@muqeemuddin8057 5 ай бұрын
Hey, can you make a video on binary insertion sort and compare it's time complexities with insertion sort. Thanks for your videos on DSA.
@user-nk1mb5fy7j
@user-nk1mb5fy7j Жыл бұрын
UNDERSTOOD
@anushkajain8480
@anushkajain8480 5 күн бұрын
Understood.
@abhishekahirvar7783
@abhishekahirvar7783 2 ай бұрын
thanks mate
@AbjSir
@AbjSir 9 ай бұрын
// Everytime while sorting you move an element to the left (assume nobody moves to right agar chote walo ko aana hoga to left me aa jayenge) // if an element crosses another element while moving to the left for the purpose of sorting then it should increase the count of inversion
@vikasgupta1828
@vikasgupta1828 Жыл бұрын
Thanks
@vivekbhore5722
@vivekbhore5722 4 ай бұрын
thank u striver
@varunpalsingh3822
@varunpalsingh3822 Жыл бұрын
understood 👍👍
@nayankhuman1043
@nayankhuman1043 27 күн бұрын
Understood :)
@user-lw9dj8we7k
@user-lw9dj8we7k 7 ай бұрын
Understood sir
@hetchaudhari
@hetchaudhari Ай бұрын
Is it fine to pass variables by reference?Or should we avoid that too in the interviews?
@TheNullDeveloper
@TheNullDeveloper Ай бұрын
understood the solution . But how do i get the intution that it will solve in this way i.e using this approach 😕 in most of the cases i don't have idea about the optimal one how would I approach it
@culeforever5408
@culeforever5408 9 ай бұрын
understood :)
@VaibhavSethF
@VaibhavSethF 2 ай бұрын
Hi Striver , is creating static variables in interview is a bad practice?
@hemantpatel1413
@hemantpatel1413 Ай бұрын
understood.
@pritipatil4021
@pritipatil4021 Жыл бұрын
Understood...!!!!
@mount2020
@mount2020 Жыл бұрын
Thank u bhai
@RIyaGupta-iz9iw
@RIyaGupta-iz9iw 11 күн бұрын
Understand bhaiya
@user-sm7zo5zd9t
@user-sm7zo5zd9t 5 ай бұрын
Understood
@soumiyamuthuraj3516
@soumiyamuthuraj3516 Ай бұрын
awesome
@ChhaviAayushi-qh4zx
@ChhaviAayushi-qh4zx Жыл бұрын
Good Afternoon sir sir in my college it's hard to get on campus INTERNSHIP can you please advice so that I can get an internship in my third year . Thanks
@addy1268
@addy1268 6 ай бұрын
can anybody pls explain why we cant/ should'nt declare the 'cnt' variable globally ?
@MasterChief-jy2zg
@MasterChief-jy2zg 2 ай бұрын
I tried it in a new approach. 2 pointer method. time complexity is near about O(n). but don't know why it is showing O(n^2) striver can u help me? int numberOfInversions(vector&a, int n) { int cnt=0,left=0,right=left+1; while(left < (n-1)){ if(right == n){ left++;right=left+1; } else if(a[left] > a[right]){ cnt++;right++; } else right++; } return cnt; }
@user-je7tz6le4k
@user-je7tz6le4k Жыл бұрын
These types of questions always demotivate me... like how you can think of such kind of solution in an interview
@infomania_by_ayush7901
@infomania_by_ayush7901 Ай бұрын
Understood, but I am gonna need some time to implement this.
@infomania_by_ayush7901
@infomania_by_ayush7901 Ай бұрын
Implemented.
@AnjuGupta-sz1uk
@AnjuGupta-sz1uk 11 ай бұрын
understood
@pushankarmakar1783
@pushankarmakar1783 6 ай бұрын
understood! def merge(arr,l,mid,h): temp=[] i=l j=mid+1 cnt=0 while i
@MJBZG
@MJBZG Ай бұрын
didn't understand much but will try again
@vk-mc5tq
@vk-mc5tq 4 ай бұрын
Global vs Local In Global variables values updated dynamically but in local variables we need to pass updated values (manually) to subsequent functions
@error-my9ut
@error-my9ut Жыл бұрын
understood this but sir i am not really sure that if it would hit in my head at the time of interview how can i make that sure sir
@vaibhavverma6120
@vaibhavverma6120 10 ай бұрын
you cansolve these type of questions if only you have done it or similar type of question earlier otherwise no one can get this intuition on the spot.
@shaurya2608
@shaurya2608 Жыл бұрын
Is there any chance we can solve this problem using unordered map?
@bishakhdutta8427
@bishakhdutta8427 Жыл бұрын
i was thinking the same thing, but use ordered map instead
@priyotoshsahaThePowerOf23
@priyotoshsahaThePowerOf23 Жыл бұрын
yes u can use map because it is a paring problem but this will not for interview ..In Cp it's ok,
@bishakhdutta8427
@bishakhdutta8427 Жыл бұрын
@@priyotoshsahaThePowerOf23 can you explain your solution, everyone uses only this merge sort method
@priyotoshsahaThePowerOf23
@priyotoshsahaThePowerOf23 Жыл бұрын
@@bishakhdutta8427 bro nothing just declare a map ..mapmap , and store the sorted elements of both array simultaneously then just print the keys...very much easy But know the problem is whenever you using inbuilt tools like map it used auxiliary space so space complexity is = O(N) Second thing is the time complexity also O( nlogn +mlogm)
@sourabhmittal6862
@sourabhmittal6862 10 ай бұрын
can someone tell me what is wrong with my code it is written in js class Solution { // Function to count inversions in the array. count = 0; inversionCount(arr, N) { this.count = 0; const sorted = this.isArraySorted(arr, N); if (sorted === "asc") { return 0; } else if (sorted === "desc") { return ((N - 1) * N) / 2; } this.mergeSort(0, N - 1, arr, N); return this.count; } mergeSort(low, high, arr, N) { if (low >= high) return; const mid = low + Math.floor((high - low) / 2); this.mergeSort(low, mid, arr, N); this.mergeSort(mid + 1, high, arr, N); this.mergeArr(low, mid, high, arr, N); } mergeArr(low, mid, high, arr, N) { const arr1 = arr.slice(low, mid + 1); const arr2 = arr.slice(mid + 1, high); let i1 = 0; let i2 = 0; let m = low; while (i1 < arr1.length && i2 < arr2.length) { if (arr[i1] > arr[i2]) { this.count = this.count + arr1.length - i1; arr[m++] = arr1[i1++]; } else { arr[m++] = arr2[i2++]; } } while (i1 < arr1.length) { arr[m++] = arr1[i1++]; } while (i2 < arr2.length) { arr[m++] = arr2[i2++]; } } isArraySorted(arr, N) { let ascSorted = true; let descSorted = true; for (let i = 0; i < N - 1; i++) { if (arr[i] > arr[i + 1]) { ascSorted = false; } if (arr[i] < arr[i + 1]) { descSorted = false; } } if (ascSorted) { return "asc"; } else if (descSorted) { return "desc"; } else { return ""; // Not sorted } } }
@hardikg6858
@hardikg6858 5 ай бұрын
can i solve this question using pbds
Reverse Pairs | Hard Interview Question
32:26
take U forward
Рет қаралды 120 М.
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 10 МЛН
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 98 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:40
CRAZY GREAPA
Рет қаралды 32 МЛН
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 109 МЛН
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,8 МЛН
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 180 М.
3 Sum | Brute -  Better - Optimal with Codes
38:25
take U forward
Рет қаралды 269 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
Find the Missing and Repeating Number | 4 Approaches 🔥
42:24
take U forward
Рет қаралды 136 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 112 М.
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 255 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 156 М.
Merge Overlapping Intervals | Brute, Optimal with Precise TC analysis
22:35
Slow motion boy #shorts by Tsuriki Show
00:14
Tsuriki Show
Рет қаралды 10 МЛН