Leaders in an Array | Brute - Optimal | Strivers A2Z DSA Course

  Рет қаралды 166,915

take U forward

take U forward

Күн бұрын

Пікірлер: 315
@takeUforward
@takeUforward Жыл бұрын
You don't need to sort the answer, as it will store the answer in the sorted order only when taken from back. Let's march ahead, and create an unmatchable DSA course! ❤ Use the problem links in the description.
@kshitijkumre9895
@kshitijkumre9895 Жыл бұрын
When will this course possibly complete? Placement season gonna start from august!
@vishal_s583
@vishal_s583 Жыл бұрын
Great explanation ❤️
@chandreshgourd3273
@chandreshgourd3273 Жыл бұрын
We can also complete this in O(1) space complexity by updating the current element with the maximum on the right.
@akshatranakoti3224
@akshatranakoti3224 Жыл бұрын
STRIVER BHAIYA RULAOGE KYA ITNI MEHNAT TO BACHHE BHI NAHI KR RAHE . PHLE GRAPH AUR TREE SERIES NE DIL JEETA PHIR DP SERIES NE ABHI YE BHI , EK HI DIL HAI BHAIYA KITNI BAR JEETOGE❤️
@vardhamanbhandari5644
@vardhamanbhandari5644 Жыл бұрын
@@akshatranakoti3224 he is striving to get out better placement,Hands off to his efforts🙌🙌
@yhbarve
@yhbarve Жыл бұрын
I've been learning DSA for almost 1 month now. When I saw this video pop-up, I read the question, worked on it myself, coded my solution on GFG, it worked in the 1st attempt, came back to this video to see the optimal approach, and it was the same as mine! I know this is an easy question, but I'm still proud of myself for getting the optimal approach in the 1st attempt! Thanks Striver!
@sathvikreddy4807
@sathvikreddy4807 Жыл бұрын
same, its been 3 months since you commented this, how's your dsa prep going>?
@yhbarve
@yhbarve Жыл бұрын
@@sathvikreddy4807 Going strong. Thanks for asking
@sathvikreddy4807
@sathvikreddy4807 Жыл бұрын
@@yhbarve how's your motivation tho? are you doing it daily? cause after 20 days, I am burned out tbh don't feel like doing it idk, became lazy ig
@yhbarve
@yhbarve Жыл бұрын
@@sathvikreddy4807 yeah that indeed happens to me sometime. I just take a 7-10 days gap and then come back to it. Works always!
@ThattukadaCooks-um4yn
@ThattukadaCooks-um4yn Жыл бұрын
thats nice and a relief to hear..! : )@@yhbarve
@ayushdhiman9378
@ayushdhiman9378 Жыл бұрын
PLEASE PLEASE PLEASE !! Do not stop this "Brute Better Optimal Approach technique" till the end of the series this is helpting a lott !! Lots of love
@harshalborle
@harshalborle Жыл бұрын
Few enhancements: 1. I think ans is already sorted we don't need to sort that. Because we are placing new value only if it is bigger than older. 2. We can directly assign the "maxi = a[I]" in "if (a[I] > maxi)".
@vadlacharan5442
@vadlacharan5442 Жыл бұрын
yeah that's right! and even if they ask to return the result in prior to their occurence we need reverse the result
@padmaja577
@padmaja577 10 ай бұрын
Yep no need for the thing he did abt maxi......
@Sankalp-sd6fm
@Sankalp-sd6fm 9 ай бұрын
how do i return ans as array a
@ANKAMJANAKIRAM
@ANKAMJANAKIRAM 6 күн бұрын
Please refer chatgpt for these doubts bro... I know your pain​@@Sankalp-sd6fm
@itzmartin20
@itzmartin20 Жыл бұрын
Understood! 12 minutes of concise and crisp clear explaination! Thanks a lot for your great work and dedication!
@kimpatel1832
@kimpatel1832 Жыл бұрын
Best best best youtube channel to learn DSA. Striver is a blessing!!!💎💖
@riteshbisht94
@riteshbisht94 Жыл бұрын
After watching your previous videos Now I am able to think about optimal solution for this problem Thanks a lot 🌟
@mehulthuletiya497
@mehulthuletiya497 Жыл бұрын
03:21 AM 😮 00:41- Problem Statement 00:46- What is leader and explain through example 02:24- Brute force solution 05:10- Optimal solution 08:59- Code 10:36- Time & Space Complexity
@KazHachiOreki
@KazHachiOreki Жыл бұрын
if he is not in India time could be different
@takeUforward
@takeUforward Жыл бұрын
The video was recorded yesterday at 01:00 AM, and uploaded today at 11:00 PM, the time mentioned are in my time zone Could not upload in the morning as I had office, and did not get time to edit, so came back from the office, edited, rendered, and uploaded :)
@takeUforward
@takeUforward Жыл бұрын
Thank you for the time stamp ❤️
@faizanahmed9304
@faizanahmed9304 Жыл бұрын
​@@takeUforward thank you bhaiya! Immense hardwork from u
@pranayreddy5824
@pranayreddy5824 Жыл бұрын
@takeUforward where do you live, bro?
@sksabir8063
@sksabir8063 Жыл бұрын
Seriously bro i am speechless 😶..... really u r doing great 👍 for tier 3 college.....
@senpaidawwwggg
@senpaidawwwggg Жыл бұрын
For every tier colleges
@Manishgupta200
@Manishgupta200 Жыл бұрын
That's amazing 🔥. I successfully able to solved optimal approach by myself before watching your tutorial.. it's my improvement that now i can able to think optimal approach. (I stuck on > and >= but now clear).
@san_7123
@san_7123 Жыл бұрын
yep
@akashajith3120
@akashajith3120 Жыл бұрын
i have been learning from you for the past 2-3 weeks and I solved this one on my own on a similar level of optimization that gives in the order of the array without reversing, thank you for helping me build my logical thinking . here is my solution : vector leaders(int a[], int n){ // Code here vector ans; int size=0,cur; for(int i=0;i0 && cur>ans[size-1]){ ans.pop_back(); size--; } ans.push_back(a[i]); size++; } return ans; }
@GuruPrasadShukla
@GuruPrasadShukla Жыл бұрын
probably easiest question in the series! thanks man!
@charlesbabbage6786
@charlesbabbage6786 11 ай бұрын
Understood every part of the video!! Beautifully explained!!
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
done by myself + understood!
@DevilJim-p1s
@DevilJim-p1s 4 ай бұрын
Strivers My G I can't believe I'm able to solve questions by optimal approach directly now thanks bro I'm broke rn but as soon as I'll get the job I'll buy your premium course 😅
@shubhamagarwal1434
@shubhamagarwal1434 5 ай бұрын
#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.
@sane_aalam
@sane_aalam Жыл бұрын
Allah Tumhe Humesha Salamat rakhe! Tum Bahut Nek Kam kr rahe ho Tumhre Channel Se Graphs + Tree + backtracking sikhne ke baad, leetcode platforms ko aache se use kr raha hu😊
@sumitkanth5349
@sumitkanth5349 Жыл бұрын
Hello bhaiyaaa, at 10:20 we don't need to check maxi = max(maxi, a[i]), we can change maxi only if, if condition will true.
@snehashisratna9074
@snehashisratna9074 Жыл бұрын
luv you bhai , thank you for making our lives so easy 😭😭😭😭😭😭😭 lots of love to you
@soumyadeepdas6441
@soumyadeepdas6441 5 ай бұрын
Great Explanation understood in first try 🎉🎉
@gopibhoyar3
@gopibhoyar3 5 ай бұрын
This question is basically, reverse of "Buy and sell Stocks". In Buy and Sell stocks, we use to keep minimum of the value on the left side of ith position. Here we keep maximum of the value on the right side of the ith position.
@hritikbhatnagar9616
@hritikbhatnagar9616 Жыл бұрын
Your optimization technique is too good❤❤
@cinime
@cinime Жыл бұрын
Understood! Awesome explanation as always, thank you very much for your effort!!
@sarangkumarsingh7901
@sarangkumarsingh7901 10 ай бұрын
Another Awesome lecture Sir......................
@himadriroy7980
@himadriroy7980 Жыл бұрын
Crystal Clear Striver Da
@many987
@many987 Жыл бұрын
hey Stiver bhaiya, first of all thanks for such content and I feel we don't need to sort the answer in the end because it will definitely store the answer in a sorted manner. thanks
@takeUforward
@takeUforward Жыл бұрын
Yes, you are correct, good observation.
@shwetachoudhary9003
@shwetachoudhary9003 5 ай бұрын
you are such a gem❤
@himanshupatel1053
@himanshupatel1053 18 күн бұрын
vector leaders(vector& arr) { // Code here vectorans; int maxElement = INT_MIN; for(int i=arr.size()-1;i>=0;i--){ maxElement = max(maxElement, arr[i]); if(arr[i]>=maxElement) ans.emplace_back(arr[i]); } reverse(ans.begin(),ans.end()); return ans; }
@RohitRaj-kr7ti
@RohitRaj-kr7ti Жыл бұрын
Hey, There will be a slight update in the if statement, it should be a[i] >= maxi, and the ans has to be reversed. Some test case will break if `=` is not put in the if condition(Tried on GFG, hence saying). @takeUforward. This may need a correction in the leaders explanation page on your website too.
@RohitRaj-kr7ti
@RohitRaj-kr7ti Жыл бұрын
Btw, thanks for a nice explanation.
@yash1525
@yash1525 Жыл бұрын
Yes you are right, thanks for telling the "=" correction.
@ravindrav1450
@ravindrav1450 Жыл бұрын
Clear explanation and Great energy 🙏
@torishi82
@torishi82 6 ай бұрын
Awesome!! Great!! Excellent!!
@Raj-yr3vz
@Raj-yr3vz Жыл бұрын
kitna pyaar se samjhate ho bhaiya🥺
@jagratgupta8392
@jagratgupta8392 Жыл бұрын
understood striver sir ....really nice concepts and questions
@culeforever5408
@culeforever5408 Жыл бұрын
understood 😇
@samiranroyy1700
@samiranroyy1700 5 ай бұрын
best teacher😇😇😇
@GhostVaibhav
@GhostVaibhav Жыл бұрын
Understood🔥
@hareshnayak7302
@hareshnayak7302 10 ай бұрын
understood, Thanks striver for this amazing video.
@DevashishJose
@DevashishJose Жыл бұрын
Understood. Thank you so much striver.
@MCA_HimanshuSeth
@MCA_HimanshuSeth Жыл бұрын
Thanku Bhaiya for your support , I am super happy to see your success as well as your efforts for students making free content. You are my idol , so many wishesh for u Bhaiya 😊😊
@striverdaaadi
@striverdaaadi Жыл бұрын
awesome bro, love from harry
@iazizhussain
@iazizhussain 11 күн бұрын
Fully understood
@rajatrathore5455
@rajatrathore5455 Жыл бұрын
Understood , great explanation
@ganeshjaggineni4097
@ganeshjaggineni4097 10 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@knoxtsgamer186
@knoxtsgamer186 Жыл бұрын
Thank you for your Hardwork...🙏🙏🙏
@nice_random_guy5347
@nice_random_guy5347 2 күн бұрын
understood! thanks
@RaviKumar-sn6tu
@RaviKumar-sn6tu 10 ай бұрын
understood very well...thanks sir😍
@kingbadshah452
@kingbadshah452 Жыл бұрын
understood everything thanks striver
@ashishpradhan6250
@ashishpradhan6250 Жыл бұрын
understood
@gauravsrivastava17
@gauravsrivastava17 Жыл бұрын
Great explanation 🔥
@shaikimran5441
@shaikimran5441 27 күн бұрын
Love You Brooooooooooooooooooo........
@konankikeerthi
@konankikeerthi 7 ай бұрын
Understood. Thank you bro
@YourCodeVerse
@YourCodeVerse Жыл бұрын
Understood✅🔥🔥
@rajaryaman6495
@rajaryaman6495 Жыл бұрын
It's the Second question, I solved myself right (optimal). Thanks! Striver for your teachings. Here's my code :- vector superiorElements(vector&a) { // Write your code here. vector ans; int n = a.size(); int max = a[n - 1]; ans.push_back(a[n - 1]); for(int i = n - 1; i >= 0; i--) { if(a[i] > max) { max = a[i]; ans.push_back(a[i]); } } return ans; }
@niteshkhanna690
@niteshkhanna690 Жыл бұрын
It's east
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 4 ай бұрын
Imagine you are at a marathon race, standing at the finish line, watching the runners as they approach. The runners arrive one by one, and your goal is to identify the "leaders" of the race. A leader in this context is someone who is faster than all the runners who arrive after them. Start with the last runner (125 minutes): Since they are the last to arrive, there's no one after them. Therefore, this runner is a leader. Move to the previous runner (130 minutes): Compare 130 minutes with 125 minutes. Since 130 > 125, this runner is also a leader. Move to the next runner (115 minutes): Compare 115 minutes with 130 minutes. Since 115 < 130, this runner is not a leader. Move to the next runner (110 minutes): Compare 110 minutes with 130 minutes. Since 110 < 130, this runner is not a leader. Move to the next runner (105 minutes): Compare 105 minutes with 130 minutes. Since 105 < 130, this runner is not a leader. Move to the first runner (120 minutes): Compare 120 minutes with 130 minutes. Since 120 < 130, this runner is not a leader In this context, the leaders (130 and 125 minutes) are the runners who are faster than everyone who finishes after them. This mirrors the concept of "leaders" in an array, where each element must be greater than or equal to all the elements that come after it.
@Mythri333
@Mythri333 3 ай бұрын
Thank you so much ❤❤❤❤❤
@ArvindSingh-wj7vy
@ArvindSingh-wj7vy 10 ай бұрын
understood boss 😎
@Gaurav_Tripathi_
@Gaurav_Tripathi_ Жыл бұрын
Understood Bhaiya.!!
@faizanahmed9304
@faizanahmed9304 Жыл бұрын
Understood bhaiya
@HARSHA_27
@HARSHA_27 Жыл бұрын
Understood!!🙇‍♂
@sumitworld918
@sumitworld918 Жыл бұрын
Understood 💯❤
@samuelfrank1369
@samuelfrank1369 11 ай бұрын
Understood. Thanks a lot
@curs3m4rk
@curs3m4rk 10 ай бұрын
This was very easy
@tahaansari5621
@tahaansari5621 Жыл бұрын
Woke up for 2nd Ramzan Sehri and saw ur video and clicked. The question was relatively easy so I understood in the first go.
@md_seraj786_
@md_seraj786_ 10 ай бұрын
Understood : 💜
@AbhinavPundhir
@AbhinavPundhir Жыл бұрын
class Solution{ //Function to find the leaders in the array. public: vector leaders(int a[], int n){ vector vec; vec.push_back(a[n-1]); for(int i=n-2; i>=0; i--){ if(a[i]>=vec.back()){ vec.push_back(a[i]); } } reverse(vec.begin(), vec.end()); return vec; } };
@rumiNITPatna
@rumiNITPatna 2 ай бұрын
thank u striver!
@lifehustlers164
@lifehustlers164 Жыл бұрын
Completed 12/28!!!
@banothutharun2743
@banothutharun2743 10 ай бұрын
great sir
@BG-lj6fw
@BG-lj6fw Жыл бұрын
thanks a ton. understood!!
@swapnilhajare5557
@swapnilhajare5557 Жыл бұрын
Understood 👍🏻
@srushtibhagchandani7255
@srushtibhagchandani7255 7 ай бұрын
Understoood!!
@harshalparanjiya5850
@harshalparanjiya5850 23 күн бұрын
let's go --> 1 january 🔥🔥
@nikhilgautam007
@nikhilgautam007 Жыл бұрын
Thanks buddy 🙏
@RaunitJaiswal-s9v
@RaunitJaiswal-s9v 4 ай бұрын
int max_from_right = arr[n - 1]; leaders.push_back(max_from_right); // The rightmost element is always a leader // Traverse the array from right to left for (int i = n - 2; i >= 0; i--) { if (arr[i] >= max_from_right) { max_from_right = arr[i]; leaders.push_back(max_from_right); } } This approach has a time complexity of O(n) since we traverse the array only once.
@tamilmukbang3789
@tamilmukbang3789 9 ай бұрын
understood thank you bro
@utsavseth6573
@utsavseth6573 Жыл бұрын
Thanks Raj.
@RajGupta-i3w
@RajGupta-i3w 6 ай бұрын
Just a small thing that I want to share what I observed in this question is that, even if question is asking to return the answer array in sorted way, we dont need to apply sorting because since we are taking elements from right to left and according to question a leader must be greater than all elements occuring at right, so the answer array will automatically will be in a sorted order. so Overall Time complexity will only be just O(N). I hope I am able to explain my observation.
@AbjSir
@AbjSir Жыл бұрын
u can do easily in O(n) if you use list and then store elements as list.push_front()
@atozhungama2321
@atozhungama2321 8 ай бұрын
understood👍
@ankushladani496
@ankushladani496 Жыл бұрын
Thank you Bhaiya
@ali_dev7415
@ali_dev7415 Жыл бұрын
GFG solution in C++:- class Solution{ //Function to find the leaders in the array. public: vector leaders(int a[], int n){ // Code here vector ans; int maxx=a[n-1]; ans.push_back(a[n-1]); for(int i=n-2;i>=0;i--){ if(a[i]>=maxx){ ans.push_back(a[i]); maxx=a[i]; } } reverse(ans.begin(), ans.end()); return ans; } };
@per.seus._
@per.seus._ Жыл бұрын
understood❤
@gautamsaxena4647
@gautamsaxena4647 4 ай бұрын
understood bhaiya
@stranger1967
@stranger1967 Жыл бұрын
understood sir ❤❤
@asheshkaran5843
@asheshkaran5843 Жыл бұрын
Understiood!!
@viralmoments571
@viralmoments571 4 ай бұрын
if not work write if statement as follow if(arr[i]>=maxi)
@rajatpatra2593
@rajatpatra2593 Жыл бұрын
Understood ❤️
@ultimategamer5136
@ultimategamer5136 Жыл бұрын
UNDERSTOOD
@sayakghosh5104
@sayakghosh5104 Жыл бұрын
Understood!
@rahil1575
@rahil1575 Жыл бұрын
in the if loop we can directly assign maxi=a[i]
@shaheenparween4326
@shaheenparween4326 Жыл бұрын
understood✔
@SiddheshKukade
@SiddheshKukade Жыл бұрын
Thank you so much
@oyeshxrme
@oyeshxrme 4 ай бұрын
thanks bhaiya
@amitpatil7659
@amitpatil7659 Жыл бұрын
Understood :)
@anurag_vish
@anurag_vish Жыл бұрын
Understood....
@samarthpai5359
@samarthpai5359 4 ай бұрын
understood!
@ayushgupta0
@ayushgupta0 Жыл бұрын
Understood.
@mrsttechno5974
@mrsttechno5974 Жыл бұрын
Understand sir
@rohanraj2604
@rohanraj2604 Жыл бұрын
Leetcode Question No. 1299 is just a variation of this question try it out for those who do not find the leetcode question link in the A2Z sheet.
@naveensaicremsiyadlapalli3769
@naveensaicremsiyadlapalli3769 Жыл бұрын
for better approach can we use next greater element concept using stack ???
@takeUforward
@takeUforward Жыл бұрын
Not needed unnecessary space
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 517 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Quando eu quero Sushi (sem desperdiçar) 🍣
00:26
Los Wagners
Рет қаралды 15 МЛН
Set Matrix Zeroes | O(1) Space Approach | Brute - Better - Optimal
30:07
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 761 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 326 М.
8 Design Patterns EVERY Developer Should Know
9:47
NeetCode
Рет қаралды 1,1 МЛН
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 261 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 555 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 808 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Count Inversions in an Array | Brute and Optimal
24:17
take U forward
Рет қаралды 270 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН