Lecture21: Solving LeetCode/CodeStudio Questions [Arrays]

Hello ji. How are you all? This is Love Babbar. And in this video we are going to solve some more questions based on arrays, so that you have more practice and confidence in doing the questions. After this we would be dealing with Strings and character resistance analysis as well as multiple dimensional arrays. But for today, let's solve some more questions, quickly and to – the- point. Let's start. Alright! So let's move on to our first question, which says, Rotate array. What is this? So question number one says rotate array. So its not a dynamic question just an easy one. But, YES, sometimes we do get stuck with it, so let's understand it once! Even I got messed up with it , ONCE! So, the question says, you will be given an array in the input, for example, 1 7 9 11, you just have to rotate it by the value 'K'.

Suppose the value of K =2, then rotate the K, so I'll have to move forward every element by 2, shift the position of every element by 2. In this case my output shall be– 1 will move forward by 2 and will be in 9's place, 7 will move forward by 2 and will be in 11's place, if 11 moves forward by 2 then it will be in cyclic form. it will be in 7's place and 9 will move forward by 2 as well and will be in 1's place. So you have understand that is what we mean when we say move forward by 2. Alright!! Shifting by the place 'K', so you have understood this, alright! For whom the place is available, for example, 1 will be in 9's place so we put it there, and for the ones who are devoid of the place, for example, 11 we don't have two places in front, so we'll move it in cyclic form. It comes from back to occupy either the first place or the second one. We have understood this therefore this is how its being rotated.

Let's see one more example, so that we get more clarity. Suppose the input is given as -1, -100, 3, 99 and what is the value of K!! Its 2. Perfect! If I make my output array then it would be having one, two , three, four places. Perfect! We can see that if we push -1 by 2 places forward then it will occupy 3's place. Perfect! So now, I know the position of -1. Now if I shift -100 by 2 places , it will occupy 99's place. Its very easy, here it should be -100. Now, 3 will be shifted by 2 places, so 3 will come here. Next, Let's check 99, we'll shift it by 2 place forward and it will occupy the place of -100. Perfect! So this is the rotated array. Now you have understood , how to rotate an array, right!! You just have to understand a simple logic. That is, the coding of the phenomenon of— this cyclic form.

So this video is sponsored by, Relevel, if you want to enter into software management and grab a job of high CTC. Then Relevel is the platform, where you can see companies hiring, for example, CRED, 1mg, meesho, Urban Company, upGrad. Relevel tests are completely free as here many start-ups are hiring as well. A new feature has been added that of sample question papers so that you take a mock test before giving the real test and you will get an idea of what could be asked in real test.

Till now they have already provided 7 crore+ jobs and these are some names from the leaderboard and the benefit of maintaining your position on leaderboard is that you also get exciting prizes which are expensive as well. Now if you want to take up a job while being in the leaderboard and winning gifts as well then you just have to click on the link in the description and sign up , register for test and there are many categories in which you can register you can see here, these are all the categories and they even have a new category of 'Fullstack Development' So just register, take up a job and throw me a party. Thank you! We have understood the logic, if we want to move forward the last element then we will move in cyclic form But how will we do it? Its very easy my friend, have you heard about mod? You might have heard!! There was one pretty function that if you % any number with n, then your answer/ output will always come in the range of 0 ……

(n-1) Did you understand, let's see an example first, if you % any number with 10 , then your answer will always range from 0 ……….9, did you get it? Just try it then. 43% 10 = 3 69 % 10 = 9 70 & 10 = 0 55 % 10 =5, so you must have understood that whenever we mod with n , the answer always ranges from [0…..(n-1)] The index of the array always starts from 0 to (n-1), okay!! similarity!! We are getting a similarity. Now we know that by using mod, if i want to put 100 in the nth place, but the nth place doesn't exist. But If I mod this nth place with n, then the answer would be 0. Which means that you didn't place 100 in nth position , instead you placed it in the 0th position. We had to do this right!! We rotated the cycle. Let's understand it one more time. The example is 11 12 13 14 15 and the index is 0 1 2 3 4. The value of K = 3. You have to shift by 3 places. Okay, fine, its simple! Let's see carefully what you would do.

You want 15 to shift forward by 3 places, one two three, so now the current index is (n-1). If I want to shift it forward by 3 then it will be n, n+1, n+2, so you want to place it in (n+2), but (n+2) doesn't exist But why will you be tensed, you'll mod the (n+2) with n and the answer is 2. And what does that 2 mean, its this index, so you put your 15 here, shall we check it once? We had to shift forward 15 by 3 places, one two three So is it working by doing mod, of course, dynamically. So you will say, where did this 2 come from? Just now we understood it, right! We don't need to do anything! 15 was at (n-1)th position, we just shifted it forward by 3 places It reached n, (n+1), (n+2), then we had to place it in (n+2)th position, we mod (n+2) with n so that I am able to find the right Index as (n+2) doesn't exist.

So when I did mod with n , then I will revolve in this range, this is what I want. So in short, arr[(i+K)% n] = arr[i] , this is the array just put the value in it. We wrote arr[(i+K) %n] = array[i] and what does this depict? It says that this will shift the given number by K place in the cyclic way. It will shift the value in a cyclic way by K position. But whom, the 'i'th term. This is being depicted by the formula. Alright! So you have understood it. The values which can be shifted normally, they would be done, but this formula will also shift the values in a cyclic way who don't have space in front, in this way.

So you must have understood it, along with the formula as well, even if its a little. We'll be able to solve it, so let's move on, Alright! We don't have to do anything else, we just have to code this much! Focus on what has to be done, let's see the code once! Its very simple as I say, make a vector, vector<int>, you must have understood vector fully as we have tested as well.

You made a temp of size n, just start a loop, for(int i=0; i< nums; size(); i++) Alright! Do this much and then just put the formula here. temp[( i+k) nums. size()] and we have to put here the value of – nums(i); Alright! You have done this much. Now everything is stored inside temp and it has been told in answer that whatever you store, do it in num array.

So you just copied it. You wrote, for(…… or instead num=temp; Then you did nothing and left it. Let's check if its correct or we just jinxed the code over here. We ran it, and we got an error which says…. what.. what are you even doing? It said what is the 'n' thing here, oh, that's correct. nums.size(), shall we run it now? Let's see. It said what is 'nums' can't you even write the spelling properly, gosh!! Now tell me the outcome. All the test cases have been accepted, now let's run it. Al; the test cases are working fine! Shall we submit it? Let's submit it. Earlier when I solved it , Run time error had occurred.

Perfect! Its been solved. Right! We have understood the logic now, but let's still understand what we have done! Now tell me why did you make the 'temp' vector, why didn't you directly go for the 'nums' vector. Let's understand it. Let's do it in nums vector then you will understand it better. The nums vector looks like this { 11 12 13 14} and suppose k= 2. Then in this case, for i=0, you say that, nums[(i+k)% n} = nums[i] and what will it become? nums, what is i+k =2 so, 2% 4 , as the value of n is 4.

2 % 4 = 2, so, nums[2] = nums[0], perfect! Now your array is 11 12 and here it was 0 1 2 3, so you put the value of 0 at 2. which means this 11 14, thus your new array has been created. Now you went for i= 1, the formula is , nums and what is (i+k) that is (1+2) = 3 3% 4 =3 and we have to put nums [1] in it. This is 3 here and this is 1. So your array is 11 12 11 12, perfect! Let's go for i=2, here its 0 1 2 3 nums[(2+2)%4] which is 0 , so nums[0] = nums[ 2] , just put the index 2 in index 0. Its 11 12 11 12, so same way we have checked for i=3 and it will be, nums[1] = nums[3] , something like this will come 11 12 11 12, is it the correct answer? We had 13 and 14 but where did it go? That is the thing my friend, that is why we took the' temp' array and not the other one. If we apply the same formula in this array, it will be over write it. Alright! We have used the value of this vector but we have stored it in the temp array so nothing overwrites. Here we created the temp array which was empty, right! So in this way we had to create the temp array separately.

It will be over written in nums array and our answer is wrong. And if we take the temp array then Let's see what happens. Here the nums array is 1 2 3 4 ,so let's go for i=0, according to the formula temp[(i+k)%n] = nums[i] , perfect! What will come for i=0? For i=0, temp will be (0+2) that is 2 and 2% 4 is 2 , therefore, temp[2] = nums[0] What is nums[0]? 1 which means the value of temp array is, here the index is 0 1 2 3 1 is put inside the index 2, perfect! We have done this, the we went for i=1, temp[3] = nums[1], perfect! nums[1] is 2, this is 0 1 2 3, right!! Here it will come 3 in temp[2], so This was 1 and here its 2, good! Next i= 2, so , temp[0] = nums[2] which means at 0th position you have put the value of nums. What is at the 2nd position? its 3 so you put here 3 as well. Alright! Now last i=3, temp[1] = nums[3], alright! What is at the 3th index? 4, so you have to put 4 in the temp [1]. Here its 3 4 1 2 and the index is 0 1 2 3, has it been rotated by 2 places? Its rotated by 2 places, yes it is. Right! So you must have understood why we have taken the temp array, what is its significance here? what is its use? Why didn't we take it in the same array? In the same array it would have been over written like this.

So we created a new array and we did in it. And how did we know about the cyclic form? because we had related it with whom? with the logic of mod, as it works the same way. Here we had done it right! The mod that we do , the range lies from [0 ……(n-1)] and I have to rotate it here, the position ranges from [0 …………(n-1)] and then vice -versa. Same thing happens with mod you go back from[(n-1) ……0] so you have understood this, perfect! Now Let's move on to our second question which will be very easy.

Here you have to check if array is sorted and rotated or not. We had learnt sorted and rotated in binary search when we were finding pivot. Do you remember? We had solved it there, we just have to check if the array is sorted and rotated or not? Alright! Just read the question once. Alright! in question 2, you have been given an array which consists of some values. 3 4 5 1 2 now find and tell me the answer from the question itself. Maybe its from allotting I think so but there isn't anything in the solution. So let's start this with logic,, our mind says that its an easy question, right! So let's write all the cases once, what can be our first case? 1st case must be that the array is already sorted. 1 2 3 4 5 in this way. What can be the 2nd case? It can be the true case that its sorted as well as rotated just like the above example. 3 4 5 1 2, alright! What does the 3rd case say? That its neither sorted nor rotated, its different means its a useless question. Just like 3 5 7 1 6 Alright! So now I know that there aren't any cases apart from these three, as there can't be any.

So if I talk about the 1st case, then I can see that 1< 2, 2<3, 3<4,4<5 and this one 5>1. Then 1 pair exists where nums[i-1]> nums[i]. Perfect! In the 2nd case 3<4, 4<5 but here5>1 and then 1<2 and then 2>3, so here as well 1 pair exists So I have understood in which case I have to send true.

I have to send true in that case where 1 pair exists with above condition, the one that we have written above. Perfect! Now I know that this answer will be wrong, why? as here its 3 5 7, 3<5 , 5<7 and then 7>1 and then 1< 6 and 6> 3. we got 2 pairs, even if I write it down and initialize it, that will be 3 5 7 1 6, it will be in this form. Right! But this isn't that way, how does our rotate and sorted array looks like? Its of this type. But this one goes straight up.

Right! So we know that the third case is wrong as we are getting 2 pairs. So I made an answer that if your finding only one pair that is In your array if there exists a pair, which is in cyclic form, As we are comparing the last element to the first one, so there exists a pair where the condition is being fulfilled then your answer is TRUE. And if there are more than 2 pairs in the array, then its FALSE. Alright! perfect! So let's code it once.

So we'll maintain a count. int count = 0; Perfect! now start a loop. for (int i=0;i<n; i++), alright! Now just check all the elements in it. OK, fine! Now I have to compare the last element with the first one therefore if(nums[i-1]> nums[i]) if this is greater than that then just do count ++; Now there is a mistake, suppose if I am doing here [i-1] and starting with 0 then I will come as -1 in the starting but -1 doesn't even exist therefore exception will arise in the index sorted bound so we have to start it from 1, right! This is being done. Now with this loop you have done all these comparisons, these all four of them, but this comparison is pending, its a single comparison so there is no harm in doing separately. We can even use mod, we have done separately therefore if( nums[]…. but first let's make 'n' separately.

Int n= nums. size, perfect! therefore if( nums[n-1] which is the last element, if its greater than your first element that is nums[0]) then in this case do the count++ as well, perfect! Then you can return it but first check if its been count is 1 or not, so count==1; If its 1 then return true otherwise false. Are we able to understand the algorithm, let's understand one more time I maintained a count which will tell me about the pairs that exist in which nums[i-1] > nums[i] right! We traversed the whole array, then you see how many pairs exist and then you do count ++, right! then you had the last cyclic comparison remaining, like this one, so for that last one you applied this condition let's compare the last element with the first one and the if its greater then i'll do count ++.

For the last one if the count is 1 then return true otherwise return false, simple and now let's run it once. Its been accepted so let's run all the test cases. We ran all the test cases and the answer is wrong. wow! perfect! Now tell me what is the mistake here? The test case of 1 1 1 was wrong, what is wrong here? All the answers are equal, let's understand what happens in this type of case. So here if nums[i-1] is this, then do count++. Our answer is coming in this case as false and false depicts that the order isn't sorted. Alright! OK, so let's understand this one as we had skipped it. What does this test case say? that if…. we had already seen three cases. We missed the fourth case, which says all the elements will be equal, now what shall we do? when all the elements are equal then in this case, if I do it this way, count <=1, then my answer would come.

Let's run it. It ran right! Let's do all the test cases. Successful right! perfect! So if I try to find answer in this case then there isn't any pair for which nums[i-1] > nums[i]. Which means the count of pair is 0, that means , if count =0/1 then return true else false. That is what we have written here. If count<= 1 then return true else false, we have done this much. We had missed a case that we have handled. So let's submit it. Perfect! Its 100% faster solution than c++ solution and I don't think you will get this. But its ok, I guess you might get this but I don't think so that they'll tell you. So you have understood this which is important. You have done all the comparisons including a cyclic comparison. And checked hoe much is the count, if count is 0/1, In the case of 0 everything will be equal, and for the case of 1 when we get a pair in which nums[i-1]> nums[i], that is , one element which is lesser than the upcoming one.

Right! So you applied this logic. Now Let's move on to our next problem which is Sum of Two arrays. This is pretty question however the implementation takes time but its fine. Its a very pretty question, alright! So it says you have been given two arrays. arr 1 and arr 2, they consists of some elements. The element in the first array is 1 2 3 4. In the second array it contains 6, and we have to sum it up that is we have to add them. So here 1 2 3 4 and 6 answer is 0 4 2 1 backwards so its 1 2 4 0 This is our answer, Let's check it once! So the answer is 1 2 4 0. Second test case says 1 2 3 and 9 9, so if I write here, 1 2 3 and here 9 9 and add it so the answer would be 2 2 2 , alright! This is your answer and you must have understand what is happening logic wise, we have done simple adding. But let's carefully understand its code. Alright! So we add from the latter part in the array, you can't add from the first element, its wrong. We always start to add from the back, so in both the arrays we'll run the loop from its end.

Perfect! We have 3 cases, I'll explain all three. 1st case, in which the upper array is longer than the lower array that is 1 2 3 4 and 6, right! So you must have handled this during the loop. right, then this remains, 1st case. In the 2nd case, when your upper array is shorter and lower array is longer, then during the loop you must have handled this. But this part remains. Right! Now the 3rd case, suppose the length of both the arrays are equal. You handled all three during the loop but if you add this part then you would have a carry, right! Now that carry is remaining, you will have to add it right! Three cases, 1st case where left array is longer, 2nd case where right array is longer and the 3rd case you have a carry remaining, right! So we have to keep these things in mind and then we can solve our problems easily.

Alright! You will understand when we'll solve a question. For example, this question its 1 1 and 2 3, this is in the form of the array. Her its i and in the second one its j, now, I added it. I found a value which is SUM where I added 1+3= 4 Quickly you found the carry, how? carry= sum/10, now if I take the example To add 19 and 9, then the answer here is 8 and the carry, here it was 18 which we'll divide by 10 so it'll be 1.

Oh! So I got 1 by dividing and I got the carry and the value that we have to put here, how will we find that? Simple , sum= sum%10, if I do 18% 10 then it will be 8 and that is what I have got here. This is the value we got and you put it in the answer array, arr[i] = sum, in this way. If you have understood this the you have understood the entire question. You divided to get the carry and the value that you have to put in that particular question, you found it by doing mod. If you have understood this then you'll understand the entire question during code. So let's do it so that its clear to you. And yeah! Let's finish it in 10 minutes. Perfect! So first I'll make a variable, int i= n; and int j= m; Perfect! Oh! here its should be n-1 and m-1; Both of them are indicating the last element, that we are present at the last Index.

Now I ran a loop, Let's make a carry first. int carry = 0, perfect! We assumed the starting carry as 0. Now I ran a loop, while(i>=0; that go for the starting and not out of the range. And j >=0, in both the cases/ array. So in this case, find the first value that is, int value 1= a[i] We don't need to wrote it, I am doing it so that you understand, then the second value which is, b[j], both the values are found, now let's find everything, we'll find sum.

Int sum = value 1 + value 2 + (it can also be carried right!) carry Alright! so you wrote this expression, in this way it'll become sum. Sum has been found. Now you found the carry, carry= sum/10, perfect! sum = sum% 10, by doing like this. And you put it in the answer array, what is it? Let's make one now. vector<int> , right! So I put it in answer and my answer was 'ans'. AND then ans.push_back(sum); perfect! So here my art ends, which part, where both the lengths had been overlapped, like this part, now I am left with the part of 1 2 3 and the carry, so we have three things remaining. If I go for the first part/ 1st case, right! There is one more mistake, we have to do i– and j– as well.

You will have to come back to this right! Let's move on! Now the1st part, when Your first array is longer and remaining as well, then, while( i>=0) So in this case, you'll just find the sum. int sum = a[i]+ carry And what will be sum, carry, you have copied everything here. You removed the j from here. Perfect! Now let's find the 2nd case. Second case , we'll find the sum and how, by b[j], and then we did b[j] + carry. We found the carry and sum, pushed the answer and did , j–, perfect! Now let's move on to the 3rd case. It says the carry is remaining. Then, while(carry!= 0), till then I assumed the sum as carry. And carry= sum/ 10 as we need it, but not j– so we'll remove it. Perfect! We can optimize it which you'll do , so now your vector is ready in which you have to give the answer, but there'll be a mistake But we have found the answer, there is only one mistake, but first you printed it. You printed the vector or whatever your answer is and we returned it.

It will show in the answer what we have to print, when we'll code and run it. let's see what happens. See we have done a mistake here, we did I>=0 , but it should be with j. Let's run once again. Alright! It says error expected in line 9. We have missed a semicolon, here it is, let's run once again. Its wrong answer in all of it, alright, we knew it. We ran all of at once, we know that our answer is coming in reverse.

Let's check it once. See here, our answer is 6 9 7 but the real answer is 7 9 6, our answer is 3 2 and here its 2 3 .Let's understand why is it happening? Whatever answer we found, like in this case our answer is 0 by adding 6+4. then here its 4 2 1 backwards, so what did we do, firstly we had a vector like this, First we put 0and then 4 2 1, so our answer is 0 4 2 1, so our answer is opposite. Right! We just have to reverse the vector and nothing else and our work is done. If we reverse here, 'reverse', if I make a function and put a vector in it write our answer, then our work will be done! How do we reverse it? No problem, its simple, I'll show you now, you made a function 'void' OR reverse, leave the void, you might not understand it therefore vector<int> reverse(vector<int> v), I often take v, you just did , int s =0 and int e = v.size() -1; You said, while(s< e), just swap and nothing else, v[s++] and v[e– 1]; you have to swap them.

And you returned v. Perfect! Here you answered return, remove this one. Now let's run the code once again. Perfect! Can I submit it? Is there anything left, or you have to correct something, think carefully. We checked it if there's anything wrong, let's run a custom test case once. Never mind let's just submit it.

Perfect! Its the correct answer so you have understood it, first we made the cases I remember I had an interview for microsoft and they asked me to focus less on programming and coding and focus more on engineering. What you do, first make the cases, this ill be my first case, second case, third case and then start to code it, not that, you have to do it immediately and quickly. We had the 1st case here, that the length of the first array might be long. And second array might be shorter. What will be the second case? The first array might be shorter and the second array might be longer. What will be the third case? That the arrays are equal but you might have a carry left, even if its 0, you will be able to do the work. But if its greater than 0 , then we have to handle it , right! So we have handled it as well, we handled all the three cases, when One of the array is longer in size than the other one and the third one which has equal arrays and a carry is left.

This is what happens in math, it has simple problems. You just found the sum and then added and found the answer, then you found the carry and the value that you have to insert, don't confuse this one, that why are both same, we are meaning to find only a value by mod. Which you will pick up and insert here. Suppose you want to remove it, if you can't understand it, then assume it as value. int value = sum% 10, assume it the value as well, now did you understand! That I found a value and mod the sum with 10 and found the value and put it here, its this easy, right! DON'T confuse it, then you'll be able to do it. We have got the answer. So this is the game thing that we solved recently, we had so much fun! I'll quickly place all three cases in my code base. and you'll get the link in the description. So that is it for this video and we'll meet in the next one and do tell me if you liked it.

I have made this video in a hurry, you should have understood it, which is important here. In homework, you have to tell me the complexities of all three solutions in the comment. Question 1 we have time and space complexity, question 2 we do have time and space complexity and in question 3 as well. This is for your home work. Perfect! And you have to solve all these questions as well by yourself, Done! OK! bye, bye!.