Minimum Number of Swaps Required to Sort the Array | Love Babbar DSA Sheet | GFG | FAANG🔥| Placement
Вставка
- Опубліковано 9 лют 2025
- #sorting and #searching #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Minimum Number of Swaps Required to Sort the Array'.
Array question Playlist = • Love Babbar DSA 450 Qu...
String question Playlist = • Love Babbar DSA 450 Qu...
Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
Roadmap for Dynamic Programming = • Complete Roadmap for D...
Great Strategy to solve DSA = • Great Strategy to solv...
My Journey to 5 star at codechef = • My Journey to 5 Star a...
Love Babbar DSA Sheet : drive.google.c...
Follow us on Instagram:
Subham Singh : / shubham__ig
Shailesh Yogendra : / shaileshyogendra
Yogesh Yogendra : / i_am_yogesh_here
Follow us on LinkedIn:
Subham Singh : / subham-singh-cc
Shailesh Yogendra : / shailesh-yogendra-8b13...
Yogesh Yogendra : / yogesh-yogendra-26bbb518a
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
Shukriya brother, you're way younger than me, but have so much more knowledge. Nice to learn from you bro
I know im asking randomly but does someone know a method to log back into an Instagram account??
I stupidly lost the password. I love any tricks you can offer me
void swap(pair& a,pair& b)
{
pair temp=a;
a=b;
b=temp;
}
//Function to find the minimum number of swaps required to sort the array.
int minSwaps(vector&nums)
{
// Code here
int n=nums.size();
vector v(n);
for(int i=0;i
You are using push_back function don't use that use v[I]={nums(I) , I} it works
bro you cant do both declaring size and then using push back, either use index based or simply remove size from declaration
Best explanation 👍
best explanation. keep it up bro
Best explanation
Bhai hindi wali feeling hi alag hoti hai 👍👍👍
Thanks for very easy explanation....
Nice Approach!!
What a approach!👏
this was a great intution
Op explanation .
bahut mast bhai
Bhaiya mera interview h kuch dino me microsoft ka..Maine aapke channel se tyari ki h,agar selection ho jata h to me aapko thanks donation dungi
Describe it in graph approach if possible.
Will this approach work when duplicate elements are there?
Bhai please sort ko short mt bola kr....
nice explanation bro
How is it guranteed that you will be doing min no swaps this way
but how can this solution assure that this solution is giving min sawps?????
Exactly
Bro the idea behind this algorithm is to place the element exactly at its correct index..
🙌
bhai esa ni kar sakte ki ek alag array m saare elements store kara len aur fir usko sort karden
aur uske baad loop chala kar jitne elements match nhi honge original array se uske half no of swaps honge
Nhi.
Bhai to jo bata rha hai na usse minimum number of swaps kabhi nhi aa payega
nice explanation thanks .
Great work
Bhai mast explanation h ..
Code sabhi jagah mil jayega but the good explanation isn't.
all good but 18 min explanation was not needed at all.
Space complexity kitna hai isss code ka
Good explanation 👍, but what if given array contains duplicates??
Use some stable sort technique
but i think in question it is already stated that "strickly increasing order" so that kind of input wont come in this question.
but if you want to do for the duplicate then you should use stable sort.
Legend 🔥🔥🔥
Nice explanation
corner cases is not covered,
op explanation!!!
splendid!!!
Bahiya aapne median of two sorted array ka solution nahi bataya
Vo bhi bata dijiye
Best 🙏🙏🙏
AWESOME VIDEO
Thanks!
can u explain why i--?
Hats off dude
thnx so much
will it work on duplicate element?
no distinct is already mentioned in question
why do half of the people call sorting as "shorting" ?? 😆
Time limit error
sort an array not short an array (Pronuncation)
Good explanation but the way you talk is irritating !
Learning from a younger person is what makes you irritating , nothing bad about his accent.
@@GdLongerHandle I'm same as his age😂
Just because you don't like the accent, doesn't make the content bad
bhai tumhe solution ka explaination chahiye free mein aur phir bhi ungli karni hai gazab ho yar
@@AnkitSingh-wq2rk es trh ki ungaliyan apko mauka deti hai khud ko improve krne ka. Rather than being quiet and see him grow slowly, tell him his weaknesses to let him grow fast
bhaiya , parameters ka jhol ho jaataa hai kabhi kabhi
aapne arr[] aur N size ke basis pe sol diya hai but gfg par vector leke &nums diya hai bas😢😢
To find n , use n= mums.size() . Hope this helps :)
Bhaiya beginner ke liye btaye please
This is youtube man just speak english. I wish I could subscribe
your accent is execellent 👍
bhot ganda pdhate ho yr
why this code isn't giving desired o/p?
int minSwaps(vector&nums)
{
/*vectorans=nums;
int cnt=0;
sort(ans.begin(), ans.end());
for(int i=0; i
make this video on java pls
bhai sorted hota hai shorted nhi ,bolna sikh pehle
bro TLE aara h isse
Python code for this
btw it's SORT and not SHORT 🤣🤣🤣 Can't stop myself from mentioning that..
improve your accent bhaiiii
can anyone tell why this code is not working ??
int minSwaps(vector&nums){
// Code here
vector help = nums;
int ans = 0;
sort(help.begin(),help.end());
for(int i = 0 ; i < help.size() ; i++){
if(help[i]!=nums[i])
ans++;
}
if(ans%2==0)
return ans/2;
return ans/2+1;
}
in the question, it is asking min number of swaps. so u need to take its previous index in the algo and sort that vector of pairs and convert that sorted one into the vector given as input.
please explain in English
chod do bhai pdhna ,
student ka time waste nhi hoga
Short nhi sort
Tu iska vakeel h kya
Bhaiya why you used v(N) ?
Pta nhi ji kon sa nasha karte hai
@@amandeepnokhwal2977 kon?
Vo doubt clear hogaya stack overflow se
@@anshulvairagade1604 link bhejiyo soln ka 🙂
Solution ya vo v(N) bracket me N w use kiye vo doubt ?? Doubt ki link
stackoverflow.com/questions/52358322/when-i-try-to-define-this-vector-pair-int-pairint-int-vpn
@@anshulvairagade1604 thnx bro 🦄
how to implement this in java ?
```
class Solution
{
static class Pair{
int element;
int index;
Pair(Integer element,Integer index){
this.element = element;
this.index = index;
}
}
//Function to find the minimum number of swaps required to sort the array.
public int minSwaps(int nums[])
{
if(nums == null || nums.length == 0 ){
return 0;
}
ArrayList al = new ArrayList();
for(int i=0;ia.element-b.element);
int count = 0;
for(int i=0;i
Bhai short nahi hota h Sort hota h
Sort not short
sale "Shorted" nai "sorted" hota hai
short kardo
bilkul bakwass 7 min ka explanation 17 minmein samjha rakha