Lecture 20: Solving LeetCode/CodeStudio Questions [Arrays]

preview_player
Показать описание
In this Video, we are going to solve questions on Array:
- Reverse an Array after m position
- Merge 2 sorted arrays
- Move zeroes to end

There is a lot to learn, Keep in mind “ Mnn boot karega k chor yrr apne se nahi yoga ya maza nahi para, Just ask 1 question “ Why I started ? “

Homework: Added in Video already

Question Links:

Do provide you feedback in the comments, we are going to make it best collectively.

Telegram Group Link: Love Babbar CODE HELP

Connect with me here:

Intro Sequence: We have bought all the required Licenses of the Audio, Video & Animation used.

Timestamps:

00:00 - Introduction
00:32 - Reverse an Array [Question 1]
04:24 - Promotion
05:27 - Code
12:48 - Merge 2 sorted arrays [Question 2]
15:00 - Code
25:44 - Homework
26:49 - Move Zeroes [Question 3]
31:35 - Code

#DSABusted #LoveBabbar
Рекомендации по теме
Комментарии
Автор

Due to your help and guidance I have cleared Amazon SDE-1's coding round . Wish me luck for the interview .

Lucrativesummon
Автор

Feedback: Again I'm saying problem solving is this course's speciality which no other yt channel or resource has ..lage raho Bhaiya ji...sath me hain bilkul aapke 💯❤️

rahulrastogi
Автор

Present bhaiya . What you said about this course before start is Turning out to be 100 percent true . Keep going bhaiya. You are like our professor from money heist.

aayushkhurana
Автор

Personally a JS Learner, Learned C++ amd loved it, but there were less problem solving focused lectures in YouTube. This playlist of DSA series fullfills the issue. Thnx vaiya

nacxxj
Автор

32:50 for(int j=i; j<nums.size(); j++), we can reduce the number of iterations by having j=i instead of j=0

durgashreenv
Автор

this playlist is destined to change the life of upcoming undergrads. Really thank you for what you have done for the cs
community.

yashsonune
Автор

solved 2 questions of arrays first time by myself...thankx alot to u man this playlist is really a gem!

prathamsharma
Автор

25:44 Homework code:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=0, j=0, k=0;
vector<int> nums3;
while(i<m && j<n){
if(nums1[i]<=nums2[j]){
nums3.push_back(nums1[i]);
i++;
}
else{
nums3.push_back(nums2[j]);
j++;
}
k++;
}

while(i<m){
nums3.push_back(nums1[i]);
i++;
}

while(j<n){
nums3.push_back(nums2[j]);
j++;
}
// cout << nums1.size();

nums1 = nums3;
}
};

KunalKumar-tpcc
Автор

25:44 Homework, it is self explanatory

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=0;
int j=0;
vector<int>ans;

while(i<m and j<n)
{
if(nums1[i]<nums2[j])
{
ans.push_back(nums1[i]);
i++;
}else
{
ans.push_back(nums2[j]);
j++;
}
}
while(i<m) // push remaining elements if any
{
ans.push_back(nums1[i]);
i++;
}
while(j<n) // push remaining elements if any
{
ans.push_back(nums2[j]);
j++;
}
nums1=ans; //assign ans to nums1 as we don't need to return ans (its a void function)
// return ans;
}
};

musqitonebeats
Автор

Bhaiya ab pata nahi but jyada maja ata hai approach ka intuition thora thora ane lagta hai . Thank you ❤️

atulkumar
Автор

Bhai Yrr you totally nailed the questions asked in my Nagarro and TCS interview is from your Bhai
:)

aryanshah
Автор

Time Stamp 25:44 Re: Homework

The way to think about the solution is that we will have to do a reverse sorting.
We initialize k=m+n-1 as that will be the last location of nums1.
We will keep checking for the greater element of the two arrays(i=m-1, j=n-1) and insert the values.
nums1 = [1, 2, 3, 0, 0, 0], m = 3
nums2 = [2, 5, 6], n = 3

nums1 = [1, 2, 3, 0, 0, 0]
| |
i k

nums2 = [2, 5, 6]
|
j
nums2[j]>nums1[i] thus nums1[k]=6
k and j are decremented.

nums1 = [1, 2, 3, 0, 0, 6]
| |
i k

nums2 = [2, 5, 6]
|
j
nums2[j]>nums1[i] thus nums1[k]=5
k and j are decremented.

nums1 = [1, 2, 3, 0, 5, 6]
| |
i k

nums2 = [2, 5, 6]
|
j
We keep following up this procedure and we get the desired reult.


void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i=m-1, j=n-1, k=m+n-1;
while(i>=0&&j>=0)
{
if(nums1[i]>nums2[j])
{
nums1[k]=nums1[i];
i--;
k--;
}
else
{
nums1[k]=nums2[j];
j--;
k--;
}
}
while(i>=0)
nums1[k--]=nums1[i--];
while(j>=0)
nums1[k--]=nums2[j--];
}

virajdeshpande
Автор

What a consistency bhiyaa
Aakho me neend ye phir bhi video bana rahe ho 🔥🔥🔥🔥

Explanation is on top

raj
Автор

In the second question merge two sorted arrays - 12:48 I used this approach
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
// removing zeroes
for(int i = 0; i<nums2.size() ; i++){
nums1.pop_back();
}


for(int i = 0; i<nums2.size() ; i++){
nums1.push_back(nums2[i]);
}

sort(nums1.begin(), nums1.end());

}
};

artifice_abhi
Автор

Present sir, array toh sab padhate h but bhaiya array ka postmortem karte h. Thank you bhaiya.Roz bas ese hi video ane dijiye.

srajittanwar
Автор

Maja aa gaya dekh ke bhaiya♥️ U r video improving my logic building …….. in this video i am able to think brute force approch ……thank you bhaiya ♥️

vijaymaurya
Автор

Small correction bhaiya
23:37 At line no - 32 it should be arr3[k++] = arr2[j++]; instead of arr2[k++] = arr2[j++];

rahulgoswami
Автор

Q MERGE TWO SORTED ARRAYS
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {

for(int i=0;i<n;i++){
nums1[i+m]=nums2[i];
}
sort(nums1.begin(), nums1.end());
}
};

we can also use this approach because here what we are doing is that we are just increasing the size of the vector by changing the value of I and pushing the num2 element one by one to the vector . After pushing it we will get an unsorted vector with value and we can just easily sort it and this will return correct ans.
Thank you sir for making this video

laveshkhairajani
Автор

Question1:
void reverseArray(vector<int> &arr, int m)
{
// Write your code here.
int i = m+1;
int j = arr.size()-1;
int temp = -1;
while(i <= j ){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}

AmanSharma-tndy
Автор

wah bhaiya samajh aa gya bahut asan ho gya aapke padhane ke baad isse pahle aisa lagta tha ki coding mere liye bani hi nhi hai lekin ab mja aa rha hai.Thanks bhaiya ❤

atulkumarpandey