Majority Element | Moore's Voting Algorithm| O(N) Time complexity and O(1) Space Complexity Solution

  Рет қаралды 14,271

Hello World

Hello World

3 жыл бұрын

This is the video under the series of DATA STRUCTURE & ALGORITHM. We are going to solve Questions from GeeksforGeeks Majority Element in O(N) Time Complexity and O(1) Space complexity. Given an array A of N elements. Find the majority element in the array. A majority element in an array A of size N is an element that appears more than N/2 times in the array.
Which is a very famous and Routine question asked in Interview. The question is from the Topics Data structure. A full easy concept in Hindi. This is Question is asked in Many companies like Google, Amazon, Oyo Rooms, Paytm, Samsung, Adobe, etc..
We also Provide courses on Competitive Programming and Data structure. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------
► Homework Question: - - - - - - - - - - - - - - - - - - - -
----------------------------------------------------------------------------------------
► Majority Element: practice.geeksforgeeks.org/pr...
► PDF of Majority Element: github.com/Prince-1501/Hello_...
► CODE of Majority Element: github.com/Prince-1501/Hello_...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
----------------------------------------------------------------------------------------
►Our Playlists on:-
►Competitive Programming : • How to start Competiti...
►C++ Full Course : • L-01 || Introduction a...
►Algorithms : • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
Our Students Contacts Form:-
Form link: docs.google.com/forms/d/e/1FA...
------------------------------------------------------------------------
#interview_preparation #geeksforgeeks #Hindi

Пікірлер: 116
@tanvisharma6879
@tanvisharma6879 3 жыл бұрын
I cannot thank this channel and the instructor enough for making my DSA concepts so clear. Thanks a lot:))
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
You made my day ❤️❤️
@alim241081
@alim241081 Жыл бұрын
You are junior than me but you are superb
@ChessKaChaskaa
@ChessKaChaskaa 3 жыл бұрын
dil se shukriya sir , batana ka tarika wakai bahut damdar hai.
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Dil se shukriya...itna dil se bolne ke liye
@pulkitmethaniya6471
@pulkitmethaniya6471 3 жыл бұрын
bhai kya sai explanation kiya yr point to point itni saari video dekhne ke baad smjh aa gaya tumse :)
@mridul1075
@mridul1075 2 жыл бұрын
bhai bhai kya explanation hai yrr, gazab...take u forward wale ne to english jhadke salle ne confuse kr dia tha, thanks bro
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
😁😁 No worry Prince is here
@codinghero2001
@codinghero2001 Жыл бұрын
Best explanation of Voting Algorithm available on You Tube ❤🙏
@DHIRAJKUMAR-je4ql
@DHIRAJKUMAR-je4ql 2 жыл бұрын
a lot of appreciation for your work from my side bcz i am in learning stage of dsa thank u so much.
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Your welcome buddy You are not alone just stick with us and please share my channel with the needy students who wants the content like this
@mokshdadave5077
@mokshdadave5077 Жыл бұрын
superb explaination sir!!!.........you made it crystal clear.
@letsgoswe
@letsgoswe 6 ай бұрын
It was good and easy explanation. But here's something I want to talk bout on the naive approach. Don't you think the if condition in inner loop is wrong? if we are checking inside the inner loop like this : if count n /2 then return arr[i]. and we will return -1 in the outside of the main loop.
@Karan-sir
@Karan-sir 2 жыл бұрын
Your videos are really very simple and helpful and the way you teach I am your fan brother thank you so much for creating these videos.
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks a lot for your support ❤️
@paridhijain2313
@paridhijain2313 3 жыл бұрын
Apka hindi explaination saare concepts samja deta he aasani se. Thank you bhaiya for such useful content. Keep making such videos. Subscribed already.
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Thanks for this comments 🙂 Sure I will
@BMEFaisal_khan
@BMEFaisal_khan 2 жыл бұрын
sir,great explanation
@deepanshugarg8411
@deepanshugarg8411 Жыл бұрын
thanks a lot sir !! , really i watched almost all the video , your explantion is really nice
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
You are most welcome
@vitaminprotein2217
@vitaminprotein2217 Жыл бұрын
2nd approach best tc->o(1) worst o(nlogn) //hashing unordered_mapumap; int maxi=-1e9; int key=-1; for(auto x:nums)umap[x]++; //return the most freq element for(auto x:umap){ if(x.second>maxi){ maxi=x.second; if(x.second>nums.size()/2)key=x.first; } } return key;
@user-sk5pi8sp3r
@user-sk5pi8sp3r 4 ай бұрын
Voting algorithm best video on youtube
@ayushijindal4898
@ayushijindal4898 Жыл бұрын
Beautifully Explained 👏 Keep Explaining😇
@ankushladani496
@ankushladani496 Жыл бұрын
Thank You Bhaiya for this....
@shyampandey1862
@shyampandey1862 Жыл бұрын
Really crestal clear explanation.
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Thanks a ton shyam keep learning yaar Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
@aishwaryasanjayjadhav2619
@aishwaryasanjayjadhav2619 2 жыл бұрын
Great explanation ✨👍🏻... thankyou so much for this moores's voting algorithm ✨✨
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
You're welcome 😊
@nunsavathnaveenkumar9219
@nunsavathnaveenkumar9219 3 жыл бұрын
sir, your explanation is too good for understanding ,thank you
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
I am glad .. I found one more new subscriber And from you I hope I got 💯 more subscribers
@Suraj.18
@Suraj.18 Жыл бұрын
thank you bhaiya🥰
@aashishanand6695
@aashishanand6695 2 жыл бұрын
Bhaiya the way u explain is🔥🔥🔥🔥🔥
@knowsbetter4113
@knowsbetter4113 Жыл бұрын
Finally got this algo I like how you split solutions into steps . thank you bhaiya
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Thanks a lot buddy please share this video so that others can also get benefits from this
@subhranilbagchi580
@subhranilbagchi580 2 жыл бұрын
your explantions are awsome man
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks a lot 😀
@vakilsingh5086
@vakilsingh5086 3 жыл бұрын
isa element frequency check k lia use kr skta hai
@startupmindset7597
@startupmindset7597 Жыл бұрын
bro your explaination is best on youtube .....
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Thanks a lot Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
@PriyankaSingh-pn8xv
@PriyankaSingh-pn8xv 2 жыл бұрын
Finally understood, this algorithm... Thanks
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
keep learning Priyanka and be consistent and share my channel with your college groups that helps me a lot
@BMEFaisal_khan
@BMEFaisal_khan 2 жыл бұрын
great ,Explanation sir
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks 😊😊
@adichaudhary1861
@adichaudhary1861 3 жыл бұрын
bhaiya mene ye question number of times kiya but kbi run nhi hua bhut video bhi dekhi aaj muje acche se smj aaya h thank u bhaiya
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Waooo nice, chalo matlab mai thik thak samjha leta hun 😅 nice keep it up
@sakshisingh3851
@sakshisingh3851 Жыл бұрын
Best explanation ever!!!!!
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
glad u liked it
@OpenDiscussion__55
@OpenDiscussion__55 3 жыл бұрын
Thanks❤, Bhaiwa
@Crazy-is3yx
@Crazy-is3yx 2 жыл бұрын
Its really helpful...😃thanks..sir
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Most welcome
@ritikraj1086
@ritikraj1086 2 жыл бұрын
Amazingly explained!!! Glad to find your channel SUBSCRIBED
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks yaar Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
@deepakgaroda975
@deepakgaroda975 2 жыл бұрын
thank you so much sir for such nice explaination
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Keep sharing with your friends and colleagues
@suhelali4547
@suhelali4547 2 жыл бұрын
tons of thanks
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
🤩🤩
@parwaagrawal
@parwaagrawal Жыл бұрын
Bhaiya Aapka code sirf first element ka count check karta hai aur return ho jata hai , Ye dekhiye isme aapka code kam nahi kar raha Array [3,1,1,1,2] ; isme apka code -1 return karega jabki sahi answer toh 1 hai...
@leepakshiyadav1643
@leepakshiyadav1643 2 жыл бұрын
Brilliant explaination 👏👏
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks Leepakshi Please , if possible then share this channel in your college, groups or linkedin becoz it's cost nothing to you 😀
@namanshah8523
@namanshah8523 2 жыл бұрын
we can also do this in O(N log N) ---we will just simply sort the array and run a for loop, which would check the element at index = i and index = i+n/2 if they both are equal, that is the majority element, and we will simply return and if there is no majority element we will simply return -1;
@seemaalam3127
@seemaalam3127 2 жыл бұрын
lovely explained😍
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks a lot Seema
@JGyanRaj
@JGyanRaj 3 жыл бұрын
I was waiting for this question
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Now, your wait is over 😅😅
@biswajitsamal2408
@biswajitsamal2408 3 жыл бұрын
Sir, as we studied counting sort there also we are actually storing the frequency can't we use counting sort to solve this problem.
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
I think on your solution we have to maintain some auxiliary space which takes O( N ) space complexity .... And we have to solve in O ( 1) space complexity
@Man_of_Culture.
@Man_of_Culture. 3 жыл бұрын
Bro through this method he is not using extra space . That's the beauty of this algorithm.
@rahulsati5819
@rahulsati5819 2 жыл бұрын
nice explanation bhaiya
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Keep learning buddy 😊
@suhelali4547
@suhelali4547 2 жыл бұрын
amazing work sir
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks 🙏
@ajaysiddartha4108
@ajaysiddartha4108 3 жыл бұрын
AMAZING BRO!!
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Thanks for your moral support ❤️
@shubhamfuloria7047
@shubhamfuloria7047 2 жыл бұрын
Thank you so much sir :)
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
keep learning buddy and be consistent and share my channel with your college groups that helps me a lot
@harshverdhanswami5850
@harshverdhanswami5850 3 жыл бұрын
your naive solution is wrong bcz if I take value {3,1,2,2,1,2,3,3} then the o/p is 3 2 2 2 3 3 but we want 2 3 .please explain the naive solution.
@therahul5304
@therahul5304 2 жыл бұрын
Hats off
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Keep learning buddy and please share this with your friends and colleagues
@ABHISHEKKUMAR-wc9ke
@ABHISHEKKUMAR-wc9ke 2 жыл бұрын
bhaiya hashing bhi toh use kr sakte hi isme
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yeah u can
@divyanshsingh490
@divyanshsingh490 2 жыл бұрын
Sir,You know your channel is Magnetic 🧲 for me ,which attract me to solve the problem of DSA and even I solve the problem I love too see your solution... Can u please guide me how to solve the problem like u...is any other way of practice...
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Consistency brother Thanks bhai Bass share kardo channel ko aap mere motivation ke liye 🥰
@divyanshsingh490
@divyanshsingh490 2 жыл бұрын
I already share with my all friends..
@divyanshsingh490
@divyanshsingh490 2 жыл бұрын
Bhaiya Recursion pr v video bna do...ki approch ko implement kase kre properly
@mukulbansal8269
@mukulbansal8269 2 жыл бұрын
👍
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
😃😃
@ankitsharma7541
@ankitsharma7541 3 жыл бұрын
thanks bhai you help us a lot.
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Thank you for your support
@Dheeraj-be9gt
@Dheeraj-be9gt Жыл бұрын
this algo is not capiable to sove this [8,6,4,6,2] testcase
@avinashyadav8598
@avinashyadav8598 3 жыл бұрын
Bhaiya,ram ram🙏,, bhaiya yar unordered Map ka bhi use kar skte hai. Please reply
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
If possible then yesss
@avinashyadav8598
@avinashyadav8598 3 жыл бұрын
@@HelloWorldbyprince bhaiya code bhej rha hu , dhekna jara... #include #include #include using namespace std; int solve(int arr[],int k){ unordered_map mp; for(int i=0;i0) { int j=mp[a]; j++; mp[a]=j; } else { mp[a]=1; } } int vote=ceil(k/2)+1; for(auto i=mp.begin();i!=mp.end();i++) { if(i->second>=vote){ return i->first; break; } } return -1; } int main() { int ar[]={2,2,2,2,3,4,1,6,2,2};// give an array to process; int sze=sizeof ar/sizeof ar[0]; int a=solve(ar,sze); cout
@avinashyadav8598
@avinashyadav8598 3 жыл бұрын
@@HelloWorldbyprince arey bhaiya aab smaj gya,, thumbnail dhyan se dheka tab pta chala,,, Space:-o(1) hai, My bad Bhaiya 😂😂😂🙏🙏
@shashikantverma60
@shashikantverma60 Ай бұрын
O(n*logn) by sorting
@arpitshrivastava672
@arpitshrivastava672 3 жыл бұрын
Sir, brute force algorithm kya hoti h ?
@AnkitSingh-wq2rk
@AnkitSingh-wq2rk 2 жыл бұрын
sabse basic bina dimag lagaye solution dimag mein aaye
@arpitshrivastava672
@arpitshrivastava672 4 ай бұрын
@@AnkitSingh-wq2rk thanks
@redhat7581
@redhat7581 2 жыл бұрын
[1,2,1,3,1,4,1,6,8,9] kya ya question is method sa solve ho sakta hai
@Csaim
@Csaim 2 жыл бұрын
-1 ans hoga
@PPP-7061
@PPP-7061 8 ай бұрын
👌👌👌👌👌👌
@HelloWorldbyprince
@HelloWorldbyprince 8 ай бұрын
❤️
@AjayPrajapati-bj4tr
@AjayPrajapati-bj4tr 2 жыл бұрын
bhau time complexity ki link ka playlist btana
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yaar sorry Ajay Iska playlist to nahi hai mere pass abhi tak
@lakshyasingh3061
@lakshyasingh3061 2 жыл бұрын
2 1 13 showing test case fail for this input . i code the program just like you
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
What output u r expecting and aa kya raha hai ... And compiler me v code karke iska expected output dekho aap
@sandeepkumarkasera1
@sandeepkumarkasera1 3 жыл бұрын
[8,8,8,6,6,4,4] for this array the majority element will be 4 ?
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
N= 7 And N/2 = 3 And none of elements has count more than 3 Hence ans = -1
@sandeepkumarkasera1
@sandeepkumarkasera1 3 жыл бұрын
@@HelloWorldbyprince Thank you Sir ,Now I got it ..I was confused
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
It happens with me also 😀😀
@tbmsahil8850
@tbmsahil8850 Жыл бұрын
bhaiya i solve the above problem but i used space
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
koi baat nhi see the solution and try again
@tbmsahil8850
@tbmsahil8850 Жыл бұрын
@@HelloWorldbyprince vo tho dekh Li best explanation ever bro hats off
@okey1317
@okey1317 3 жыл бұрын
hindi malum nahimm
@beinghappy9223
@beinghappy9223 2 жыл бұрын
Best explanation of Voting Algorithm available on You Tube ❤🙏
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Wow, thank you!
- А что в креме? - Это кАкАооо! #КондитерДети
00:24
Телеканал ПЯТНИЦА
Рет қаралды 7 МЛН
УГАДАЙ ГДЕ ПРАВИЛЬНЫЙ ЦВЕТ?😱
00:14
МЯТНАЯ ФАНТА
Рет қаралды 2,8 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 38 МЛН
Dutch National Flag Algorithm. Explained with playing cards.
12:11
PhD AI student explains how China already have won in AI..
13:28
livinlavidaluke
Рет қаралды 45 М.