Lecture21: Solving LeetCode/CodeStudio Questions [Arrays]

preview_player
Показать описание
In this Video, we are going to solve questions on Array:
- Rotate arrays
- Check if rotated and sorted array
- Add arrays

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:36 - Rotate Arrays [Question 1]
03:18 - Promotion
04:22 - Approach
08:13 - Code
15:19 - Check if Sorted & Rotated array [Question 2]
17:59 - Code
22:14 - Add 2 arrays [Question 3]
25:44 - Code

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

"Programming pe kam focus karo, engineering pe zyada focus karo" (Before bursting out to code, first break the problem into subparts or conditons). Awesome lecture bhaiya

shreyanshthakur
Автор

if you hate the subject, its because of the teacher.
if you love the subject, its because of the teacher.

tremendous thanks babbar sir.

deeppatel
Автор

My approach for Rotate Array problem:

1. Reverse the whole vector
2. Reverse the first k elements of the vector
3. Reverse the remaining elements of the vector

code:

void rotate(vector<int> &nums, int k){
k = k % nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}

Dry run:

arr = {1, 2, 3, 4}
k = 2
step 1: {4, 3, 2, 1}
step 2: Reverse first two elements -> {3, 4, 2, 1} (k = 2 -> reverse elements present at 0th and 1st index)
step 3: Reverse the remaining elements -> {3, 4, 1, 2}

saignaneswarsutrave
Автор

"why I started...?"
This line motivates me a lot...
Thnks bhaiya..❣️❣️

er.preetiyadav
Автор

Homework | Time Complexities |
1> TC = O(n)
SC = O(n)
2> TC = O(n)
SC = O(1)
3> TC = O(n+m)
SC = O (n+m)

Awesome Lecture !!

zhrrBro
Автор

Blessings of many students are with you
Keep going bhaiya 🙏🙏(respect)

crmotive
Автор

Present Bhaiyaji,

Aap sirf video dalte rahiye .. humare taraf se full support aur mehenat hum darshaate rahenge...

Aur ye course duniya ka best course hai.

sounaksaha
Автор

I found your playlist on dsa few days ago and I loved this playlist

nischalgupta
Автор

Question 3) Add 2 arrays can also be done in the following way.

vector<int> reverse(vector<int>v){
int s = 0;
int e = v.size()-1;
while(s<e){
swap(v[s], v[e]);
s++;
e--;
}
return v;
}
vector<int> findArraySum(vector<int>&a, int n, vector<int>&b, int m) {
vector<int>ans;
int mul=1, i, j, res1=0, res2=0;
for(int i=n-1;i>=0;i--){
res1 = res1 + (a[i]*mul);
mul=mul*10;// this was the changing step
}
mul=1;
for(int i=m-1;i>=0;i--){
res2 = res2 + (b[i]*mul);
mul=mul*10;
}
int total = res1+res2;
while(total > 0){
int ele = total % 10;
ans.push_back(ele);
total = total / 10;
}
return reverse(ans);
}

yatharthahuja
Автор

In two array sum
you can declare deque <int>ans;
then use ans.push_front();
and directly output ans without reversing: 👍

aryanyadav
Автор

Another solution for Array rotation : (Using Reversal Algorithm)
void rotate(vector<int>& nums, int k)
{

int pos = nums.size()-(k%nums.size());


//reversing last k elements
reverse(nums.begin()+pos, nums.end());

//reversing remaining nums.size()-k elements
reverse(nums.begin(), nums.begin()+pos);

//reversing whole vector
reverse(nums.begin(), nums.end());
}

Time Complexity : O(N)
Space Complexity: O(1)

sumitshetty
Автор

Check sorted and rotated :
{
int n = given.size();
vector<int> temp(n);
temp = given;
sort(given.begin(), given.end());
vector<int> check(n);

for (int k = 0; k < n; k++)
{

for (int i = 0; i < n; i++)
{
int pos = (i + k) % n;
check[i] = given[pos];
}
if (check == temp)
{
return 1;
}
}
return 0;
}

bcs_Jaskaran
Автор

me Saturday-sunday pura din beth ke, video dekhta hu aur practice bhi karta hu,

Thank you bhai💛🤗

akashthoriya
Автор

love your channel sir, hats of to you.
i have a small add-on to the last question, as i tried to solve the last question before hand of watching the solution, so i came up with my solution as:
I would first convert the arrays into an integer, and the sum the both converted integer to get the final answer and then mod the answer with 10 to get the remainders and push that remainder into a vector and later reverse it to get the final answer.
Code:
#include <bits/stdc++.h>
vector<int> findArraySum(vector<int>&a, int n, vector<int>&b, int m) {
int sum1=0, sum2=0;
int sig=0;
for(int i=n-1;i>=0;i--){
sum1=sum1+(a[i]*pow(10, sig));
sig++;
}
sig=0;
for(int i=m-1;i>=0;i--){
sum2=sum2+(b[i]*pow(10, sig));
sig++;
}

int sum=sum1+sum2;

vector<int> arr;
while(sum!=0){
int temp=sum%10;
arr.push_back(temp);
sum/=10;
}
reverse(arr.begin(), arr.end());
return arr;
}

And thank you so much for your incredible persistence sir.

avijeetpaul
Автор

Love bhaiyaa 🌹❤️
Watching your videos since i was in class 11th i am in college and feel.glad that am following ur videos since too

aniket_k
Автор

This placement series is awesome bhaiya and best ever series on dsa ever...

kunalkamthe
Автор

First
By the review of your course in YouTube i don't visit your channel but now from this video i recommend my friend your channel.

goldiison
Автор

Love bhaiya content bohot tagde level ka aa raha hai 🔥🔥😍 Aise hi banate raho

piyushborkar
Автор

Best way of teaching. Quick and quirky. Please keep on making such videos.

iconliving
Автор

Thank you for this placement series Bhaiya
We're learning and enjoying a lot.

awais_ansarii