Rotate Array by K places | Union, Intersection of Sorted Arrays | Move Zeros to End | Arrays Part-2
HTML-код
- Опубликовано: 10 июл 2024
- Notes/C++/Java/Python codes in Step 3.1 of bit.ly/tufA2ZYt
Left Rotate array by 1 place: takeuforward.org/data-structu...
Left Rotate array by D places: takeuforward.org/arrays/left-...
Right Rotate array by d places: takeuforward.org/arrays/right...
Move zeros to End: takeuforward.org/data-structu...
Linear Search: takeuforward.org/arrays/linea...
Union of two sorted Arrays: takeuforward.org/data-structu...
The intersection of two sorted arrays: takeuforward.org/data-structu...
Problem links
Left Rotate array by 1 place: bit.ly/3ZeXGrO
Left Rotate array by K places: bit.ly/3IQQdrt
Move zeros to End: bit.ly/3XbsF6k
Linear Search: bit.ly/3GmcVGJ
Union of two sorted Arrays: bit.ly/3IpS7Rd
Intersection of two sorted arrays: bit.ly/3KSSx3Z
We have solved the above problems, and we have gone from brute force and ended with the most optimal solution.
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
01:01 Left rotate an Array by 1 Place
07:17 Code-compiler (Left rotate an Array by 1 Place)
08:05 Left rotate an Array by D Places
20:18 Code-compiler (Left rotate an Array by D Places )
27:13 Move zeroes to end
40:30 Code-compiler (Move zeroes to end )
41:05 Linear search
42:43 Code-compiler (Linear search )
43:04 Union of two sorted array
53:33 Code-compiler (Union of two sorted array)
59:05 Intersection of two sorted array
1:09:30 Code-compiler (Intersection of two sorted array)
Let's march ahead, and create an unmatchable DSA course! ❤
Timestamps someone please :)
Use the problem links in the description.
At 23:47 my mind just got blown away. Me be like: - itna easy kese Benstocks 😂🙏. Bhai iPad ka notes ka bhi pdf dalo na . Tumaaher writing sei sab samajh mai ata hai. Boh tuf sight par itna bada article dekhne sei dar lagta hai 🙂. But you are doing amazing job bhai thank you 🙏😌.
appreciable !!
Please upload the next video.
@takeUforward
class Solution {
public:
void rotate(vector& nums, int k) {
int n = nums.size();
k =k%n;
int temp[k];
for (int i = n - k; i < n; i++)
{
temp[i - n + k] = nums[i];
}
for(int i=n-k-1;i>=0;i--)
{
nums[i+k]=nums[i];
}
for(int i=0;i
for moving zeroes to the end we cen write it in 1 loop instead of two though time complexity is same , complexity for understanding decreases : v oid moveZeroes(vector& nums) {
int j=0;
for(int i=0;i
Timestamps:
0:00:00 - Intro
0:00:55 - Q1. Left rotate an array by 1 place
0:07:33 - Q2. Left rotate an array by D places
0:26:36 - Follow up Right rotate an array by D places
0:27:11 - Q3. Moving Zeroes to end
0:40:54 - Q4. Linear Search
0:43:01 - Q5. Union of Two Sorted Arrays
0:59:04 - Q6. Intersection of Two Sorted Arrays
1:12:22 - Outro
Thanks a lot sir for taking out time from your busy schedule and making videos and uploading those videos in the span of 2-3 days. It takes a lot ot effort. Thanks a lot 😊😊
For Right Rotation of Array
void rotate(vector arr, int d)
{
if(d>size)
d = d % size;
reverse(arr.begin(),arr.end());
reverse(arr.begin()+d,arr.end());
reverse(arr.begin(),arr.begin()+d);
}
27:00 rotating an array left by 1 place is equal to rotating it right by n-1 places. We can use this information for rotating an array to right by d places.
Perfect thought!
great
Bhai leetcode pa left rotation question reverse .end() and begin() use karna pad Raha hai but gfg ma nahi why
@@himanshukaushik9223 in case of vector we use stl function like begin() and end() but in case of array we don't use it for example in gfg
@@bhargavanand7325 thanks bro
me first getting the optimal solution and then finding the brute 🤣🤣
mee too😂
#me2 moment
how to remember logic ,i always forget nd cant able to code
@@user-oi1oe5he5r I am not the right person but still always go back to the code after a week or so and try solving it again. Then review it after a month. That is it, it will get hard coded in your brain after that.
Bro me too 😢
Isi wajah se easy problem solve krne me v time lgta h 😂😂 dimag me bs chlta h ki optimal hi krna h
Thank you for all the tutorials,
Please keep supporting from starting to get good placement by the help of your tutorials!
not only your dsa knowledge is good also your communication skill, voice tone , body movement is superb 🥰🥰
Who is grinding their head with DSA in their summer vacation?? Heartly thanks to striver for making us understand dsa in such depth :)
What a wonderful video, it was vey good. Your way of teaching concepts through questions is best.
I did not only enjoy the lecture but loved it too. Thanks Striver Bhaiya for explaining so lucidly. ❤
for the first time i guessed which concept can be used it might sound like a small thing but as a beginner who is just learning to code actually implementing codes without any reference and knowing which loop is used is really HUGE for me. THANK YOU SOO MUCH
same for me. feels like a big leap
Understood and what an explanation
Love your explanation bro, just continue doing what you are doing for the coding community
Amazing person
Understood! Fantastic explanation as always, I've enjoyed the video. Thank you very much!!
Munnawar Faaroqi explaining dsa questions...😂😂😂😂😂
Understood 😄.
Logic used for right rotate the array k places is that instead of rotating array right by k places I have rotated it n-k places to left .
so code for this is -
class Solution {
public:
void rotate(vector& nums, int k) {
int n = nums.size();
k = k % n;
reverse(nums.begin(),nums.begin()+(n-k));
reverse(nums.begin()+(n-k),nums.end());
reverse(nums.begin(),nums.end());
}
};
hey, me also applied same approach
same here
Exactly right
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
u can do this also.
I always thought that the optimal solution to this code was the hashset one that you did in the middle part. It blew my mind when I saw the optimal one... And that too so easy man... Wow .. just wow to your observation ❤
Thank you so much for sharing the content free of cost!! very nicely explained too😘
for rotating to right : (i have used k instead of d):
void optimal(vector& nums , int k){
int n = nums.size();
k = k%n;
reverse(nums.begin()+(n-k),nums.end());
reverse(nums.begin() , nums.begin()+(n-k));
reverse(nums.begin() , nums.end());
}
Your are best amongst all I found on internet hatsoff to you sir.
bhai ki pehli video lgayi h ye mene. bohut maja aya, dil garden garden hogya bilkul. thanku bhai.😁
Love your explanation sir!You always make things simpler
Thanks for providng such amazing course for completely free. We are intended to you.
Right Shift Solution(Leetcode 189):
k = k % arr.length;
reverse(arr, 0, arr.length-1-k);
reverse(arr, arr.length-1-k+1, arr.length-1);
reverse(arr, 0, arr.length-1);
Just do k=k%n, k=n-k!! as simple as that
Actually, there is another optimal solution for moving zeroes problem. It is similar to the function we use in quicksort algorithm (quickselect part for arranging elements around pivot).
void Optimal2(̀vector& nums) {
int j=-1, n=nums.size();
for(int i=0; i
Exceptionally concise👏
Thank you for making such great and useful videos by taking out time from your busy schedule.
Striver please bring string series. Much needed
Learning a lot outta this guy..
Striver is the lion of this community
Thank you for all the tutorials till now and much more waiting for the videos of those questions, which are in the A2Z DSA course sheet.
Thank You Striver for such an effort.
Your explanations and teaching is really amazing.
Its really Valuable /priceless content :)
Love u striver, U are adding great value to my life!! This is amazing..
Concise solution for move zeroes to end:
int p = 0;
for (int i = 0; i < n; i++)
if (arr[i])
swap(arr[p++], arr[i]);
Clear explanation and amazing teaching.
Brother, Actually only because of you i came to know what is coding and how to perform our own logic while writing code.
Your teaching was nice and I really love it. once before viewing your videos i may code but i don't know how it works internally, but after seeing ur videos it is very easy to understand the working ❣.
my first own logic:
vector temp;
int first = arr[0];
for(int i=1; i
Salute to this consistency.
😆😆
@@rohandhalpe9084 😡😡😡
@@saurabhtiwari9614 kya hua bhai 😔
@@rohandhalpe9084 Bhai aaisa face kyo bnaya h.
I recommend everyone to solve the linked leetcode problems attached as well, as the code needs to be tweaked little bit . So u can understand and do by yourself
Great approaches Striver. thanks
This much consistency is all we needed bhaiya🤞🤞🤞kudos to you
Thanks a lot sir for taking out time from your busy schedule and making videos. It takes a lot of effort. Thanks a lot.
Hey striver wonderful video. I have a query. The code will be applicable only if the vector or array is sorted in the intersection part. So do we need to sort it again or is there any other optimal approach ?
done the assignment and understood each and every concept . Sir appreciate your hardwork
Your videos are awesome. I am finally learning to think about a problem and how to approach it. Thank you so much ❤️😬
thank you so much for the series sir , it is helping a lot
One thing striver.....
In the sheet for the same question as we know there is a link attached for the gfg version and for the leetcode version but in some cases leetcode version is harder and sometimes gfg version.
So please pick the hard problem for that
Rotate right ---->>>>
void rotate(vector& nums, int k) {
vector temp(nums.size());
for(int i=0; i< nums.size(); i++)
{
temp[(i+k)%nums.size()] = nums[i];
}
nums=temp;
}
class Solution {
public void rotate(int[] nums, int k) {
int n=nums.length;
k=k%n;
reverse(nums,n-k,n-1);
reverse(nums,0,n-k-1);
reverse(nums,0,n-1);
}
void reverse(int arr[],int start,int end)
{
while(start
Guys i just wanna ask something....r u guys doing all these problems on your own or like just watching the lectures
First Understanding the problem then finding approach to solve it then if I make the correct solution then codding it in vs code and moving to nxt question and if I didn't get the solution then watching striver
Move zeroes question another approach with O(1) space
int j=0;
// Firstly taking all non- zeroes number
for(int i=0;i O(1)
Raj, Thanks a lot for This Amazing Video about C++ Arrays
Video - 2 Completed ✅
Thanks for this amazing course
your explanation is awesome and it is easy to understand
Your course is awesome wish I could have found your course a year before currently I am in 3rd year of my clg 😊
From which college u r
@@anshu59kumari Techno International New Town West Bengal
thank u so much sir for providing best content for us.❤
Such a great lecture i wouldn't never forget these conceptss❤❤
code for right-rotation:
void rotate(vector& nums, int k) {
int n = nums.size();
if (k > n) {
k = k % n;
}
reverse(nums.begin(), nums.begin()+n-k);
reverse(nums.begin()+(n-k),nums.begin()+n );
reverse(nums.begin(), nums.begin()+n);
}
Hii brother can you pls explain me why time complexity of intersection is O(1) and not O(n), where n is the size of larger sized array. Time stamp- 1:12:01
Understood. Thank you so much for this video.
I have reached here , sir . By following your A to Z DSA course . Thanks a lot sir..
Can u tel me is that a fee course or free course?
Love your effort anna ❤
Understood,Thanks striver for this amazing video.
Thank you so much for taking out time of your busy schedule. Can you please make such videos on Trees as well ? That will be a great help. Thanks a lot😊😊
wonderfull video...i cannot express my words striver...thank you so so much
I'm really lucky to have you as a teacher
understood....and thank you for making such an amazing and interactive dsa playlist
समझ मे आ रहा है?
haa bhai acha padhata hai@@shivvratraghuvanshi522
Understood 😉 maja aa gaya bhaiya
Really it is world biggest best dsa course. 😊😊
I wish I could get to know about your course in my first year, I'm in second year now, not late, btw thanks a lot (UNDERSTOOD)
I'm grateful to you sir🙌🙌. You are the best🙏
Understood Striver , well done 🙌👍
Kudos to your efforts!
For the instersection question,why don't you consider temp array/vector space in addition to visted array for space complexity(brute force) ,because that too is used?.And for the optimal solution space complexity must be O(min(n1,n2)) if all elements are common??...
Great! btw in the worst case of Intersection, the space complexity would be O(n1>n2?n2:n1) because whichever one is smaller matches completely with the other array and ends. P.S.- the space is for returning the answer and not solution.
great explanation!
Understood Thanks a lot Striver😍
For left rotate by D places Brute can be : TC: O(n^2) if d==n
Hence the rest two solutions shown in video will be better and optimal.
int n=arr.size();
while (k>0) {
Integer temp=arr.get(0);
for (int i = 1; i < n; i++) {
arr.set(i-1, arr.get(i));
}
arr.set(n-1, temp);
k--;
}
return arr;
nicely explained bhaiya ;
thank you for this amazing video;
that relationship at the last though!! 😂❤loved it bhaiya
left rotation to right rotation: simple approach is to think oppositely to left rotation.
Shift all elements right of d and add to temp and push it.
Just reverse the order of the reverse statements used in left rotation!.
For left rotate by k - In python u cannot directly use the reverse function of lists , as slicing needs to be done in that case which would require extra space , so it's better to make custom reverse function and use that . @takeUforward
you can also solve the move zeroes to end with this approach in a single loop :
int i=0;
int j=-1;
while(i
void rotateright(vector& nums, int k) {
k = k % nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin()+k);
reverse(nums.begin()+k, nums.end());
}
Big fan and student sir🙏🙏
Just a small request: kindly put system design videos before this placement season sir🙏🙏
Why bro Why, Brute force->better-> optimal, Its really a big effort. thanks for such wonderful video series. Love the way u explain.
Bhaiya you are gem 💎
two pointer approach is great ...
In the brute force approach of union of arrays, you could have directly returned the set instead of taking a temp vector and copying it all over, because the set would only take the single copies of the elements are the time of insertion
bro you are right but most of the time they i.e., dsa practicing websites expect you to return the result in the form of array(for c++ vector and for Java ArrayList).
// right rotation by k places
int k;// no of rotation
cin>>k;
int d = k%n;
reverse(arr,arr+(n-d));
reverse(arr+(n-d),arr+n);
reverse(arr,arr+n);
for(int i: arr){
cout
great content!
In move zeroes to end problem we can use this also...,
int y=0;
for(int i=0;i
wonderfulll explanation ❣❣
Understood 😄. Thank you ☺.
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
class Solution:
def rotate(self, arr: List[int], d: int) -> None:
n = len(arr)
d%=n
def reverse1(i,j,arr):
while i
great explanation sir🔥🔥
thankyou striver for this amazing series
In MAANG interview generally the coding round is of 45 min and they ask 2-3 LC medium questions.So, approx. 15 min(discuss + solve) for 1 question if 2 medium LC question they asked. So, if we know optimal solution then discussing on brute force approach will cost us. So, any suggestion here ?
In the three reversals, we are doing iterations just for half the time i.e O(d/2) + O((n-d)/2) + O (n/2) making it O(n) so TC as well as SC of optimal solution is better than the better solution.
43:50 its not i
understood striver! thanks a lot. :)
For left rotate for d places..how do i take the d values in temp in java?
Understood, Thank you😀😀
thanks for this awesome content sir ..... next lecture sir??