Lecture21: Solving LeetCode/CodeStudio Questions [Arrays]
Vložit
- čas přidán 27. 07. 2024
- 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 ? “
Visit Relevel: relvl.co/2smk
Discord Server Link: / discord
Course Flow: whimsical.com/dsa-4-placement...
Homework: Added in Video already
Notes Link: drive.google.com/file/d/11ACm...
Code Links: github.com/loveBabbar/CodeHel...
Question Links:
- Rotate arrays: leetcode.com/problems/rotate-...
- Check if rotated and sorted array: leetcode.com/problems/check-i...
- Add arrays: bit.ly/3DXfsDZ
Do provide you feedback in the comments, we are going to make it best collectively.
Telegram Group Link: Love Babbar CODE HELP
telegram.me/lovebabbercodehelp
Connect with me here:
Instagram: / lovebabbar1
Twitter: / lovebabbar3
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
Do Visit Relevel: relvl.co/2smk
Bhaiyaa notes ki link galt dal gyi
reach++
@@ShouryaPant corrected
@@CodeHelp thanx Bhaiyya You are Doing An Amazing Job. 👏 This Course Is Fantastic And awesome. KEEP it Up The Good Work🙂🙂
@@CodeHelp Sir, I can create an interactive blog that will help students to get topic wise notes along with code for this DSA course.
"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
if you hate the subject, its because of the teacher.
if you love the subject, its because of the teacher.
tremendous thanks babbar sir.
Absolutely
Blessings of many students are with you
Keep going bhaiya 🙏🙏(respect)
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 &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}
hey did you get somewhere or this was your organic thinking if it was what was your approach
@@ashish7604 Nice bro
I found your playlist on dsa few days ago and I loved this playlist
Best way of teaching. Quick and quirky. Please keep on making such videos.
Thanks, bhaiya, I am continuously watching your DSA series. I am loving it so much
Thank you for this placement series Bhaiya
We're learning and enjoying a lot.
"why I started...?"
This line motivates me a lot...
Thnks bhaiya..❣️❣️
This is way better than uploading a 4 hr video In a day and taking break for weeks
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.
Going great, bhaiya!
Keep it up! We are with you!
Question 3) Add 2 arrays can also be done in the following way.
vector reverse(vectorv){
int s = 0;
int e = v.size()-1;
while(s=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);
}
This placement series is awesome bhaiya and best ever series on dsa ever...
The way you explain is really appreciable 🤩
I sticked until last minute. I am liking it. Let's keep the josh high🔥
Love bhaiyaa 🌹❤️
Watching your videos since i was in class 11th .....now i am in college and feel.glad that am following ur videos since then.....helpsss too much........🌹
me Saturday-sunday pura din beth ke, video dekhta hu aur practice bhi karta hu,
Thank you bhai💛🤗
Love bhaiya content bohot tagde level ka aa raha hai 🔥🔥😍 Aise hi banate raho
Hello Babbar bhaiya your videos are so helpful, Thanks for this type of guideline.
Explanation of sum of array is awesome bhaiya best explanation I did not see anywhere,explanation like yours thanx
This line "Hello jii this is love babbar" gives us a peace and motivation that i can do 🙏🙏🙏
Bhaiya,pehle DSA se daar lagta tha par jab se aapka course start kiya tab se maza aa raha hai.Thank you Bhaiya!!!
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
vector findArraySum(vector&a, int n, vector&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 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.
Rotate ques : Time complexity -> O(n)
space complexity -> O(n) , n is size of nums vector.
Sorted and Rotated : Time Complexity-> O(n)
space complexity -> O(1)
Add array : Time complexity -> O(m+n)
Space Complexity -> O(m) or O(n).
if I am wrong anywhere please correct me.
I think
Time Complexity-> O(max(m, n)); //only one of 2 while loops will execute //i,e i >= 0 or j >= 0
Space Complexity-> O(max(m, n));
//carry while loop does can actually be made into a if statement, i think carry can only be 0 or 1,
correct me if i am wrong
Question bahot sahi le rahe ho bhaiya...ekdum kadak level k🔥maja aagya
bhaiya aj 3no question khud se lagaye maza he aagya. confidence next level hai ab
gaining confidence day after day :) THANK YOU BHAIYA @
CodeHelp - by Babbar
First
By the review of your course in CZcams i don't visit your channel but now from this video i recommend my friend your channel.
bhaiya apka solutions ka approach ko 100 topoo ki salamii.maja araha hai dsa karna ma ab pahla stress ata tha rona ata tha.
Ek he to dil hai , kitne bar jitoge babbar bhai
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 !!
Why it is showing time limit exceeded the find sum question
In gfg ide
ig both time and space should be ----> O(min(M,N) + {max(M,N)-min(M,N)} + 1)
becz 1st loop will run till one of the array ends
2nd will run till other part and carry could not be of more than one digit
Am I thinking right🤔
in question 3 we can use stack for the (sum of two arrays) resultant array . we don't need to write the reverse function .
because the stack works on LIFO principal.
thank you love bhai
Was eagerly waiting ❤️
Thank you bhaiya 🤩
Sir well explained course is helpfull !!
Thank you
Present sir, forever greatful.❤️
Request: web dev ka course kb tk aayega because dsa itna achaa h to pta nhi dev fir Kitna khaas hoga
@@shubham5934 bro jitna jldi hoga utna shi rhega
Tu 4th year mai hai?
@@shubham5934 bhia web development itna important nahi jitna dsa he. Aur waise bhi web d samajh sakte ho apne ap se par dsa samajhne ke liye teacher. So let bhaiya teach dsa first
Ruko jara sabar karo
czcams.com/video/tAQIKe0UGH4/video.html
Top level consistency🔥🔥🔥
bhaiya apne ye kaisa logic soch liya mujhe ye dekh ke differentiation yaad aaa gya
Bhaiyaa Time Complexity bhii Code kii discuss karna video may.
After coding discuss the complexity of solution so Practise hoti rahagiii dono kiii code kii bhii aur complexity ki bhii.
will make it as a practice from next video
bhaiya thnx a lot , attendance marked, bss bhaiya eise hi 17 March tak course khtm krrdo . 🤗🔥
Amazing content Bhaiyaa.
Thank you very much.
I like your course more than MIT Data Structures and Algorithms!
Amazing Lecture and Good Choice of Questions
My approach for 3rd question :-
vector findArraySum(vector&a, int n, vector&b, int m) {
int x=a[0],y=b[0];
for(int i=1;i
this was the first approach that came to my mind after seeing the question
I did a similar code but was facing issue with the carry part.. Thanks for the help buddy>>!!
@@bishalchatterjee745 welcome bro :)
Thanks for the solution. @Amit Shukla but what would be the time complexity of this solution?
@@taranjotsingh2374 O(n)
Bhaiya, i just saw a comment in telegram channel which u shared... ignore that type of people they themselves are chu****...u are doing a great job... thanks a lot bhai for this type of content for free❣️🙌
Commenting for reach
Babbar bhai great job ! ❤️
attendance lecture 21
thank you bhaiya for this amazing video
aur notes ma lagta ha apke raw video file upload ho gaye haan
bhaiya teeno questions solve hogaye thankyou so much bhaiya>>>>>>
Love from Delhi bhaiya. Best DSA series.
"check if array is sorted and rotated" Sir is question me loop i=1 se i
In third question (Add Arrays), Shouldn't Space Complexity be O(max(n,m)) ?
Attendence marked....bhaiya
Really this is the best course for DSA
Present Bhaiya...🙋🏻♀️
But I started a bit late so I am currently on lecture 14...but I will cope up for sure....😇
Attendance ✅
Roz videos daalte raho bhaiya ❣️
great course so far 🙌🙌
sir your way of teaching is amazing. ❤
Time Complexities
Q1- TC = O(n)
SC = O(n)
OR
// For recursive approach
TC = O(n)
SC = O(1)
Q2- TC = O(n)
SC = O(1)
Q3- TC = O(max(n, m))
SC = O(max(n, m))
Check sorted and rotated :
{
int n = given.size();
vector temp(n);
temp = given;
sort(given.begin(), given.end());
vector 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;
}
Abhi aaya hu dekhne ke pehle hi bol diya done..👍👍
Present bhaiyya.... Enjoying the content🔥✌️✌️❤️❤️ ...
Present 🔥
Love bhaiya op ❤️
#consistency op
Both your and mine let's see who will break first 😂
samajh aa gya bhaiya achche se.Thanks bhaiya.
Was eagerly waiting 🎉
thanks for the beauty of coding
wah bahiya maza aa gya nice explanation ❣
Semester chalrahe hai
Semester khatam hote hi Sare vedios dekhlungaa
APP DAREHAATE RAHOO
BABBAR BRO
thank you bhaiya . vedio was completed
Present sir
Josh is always high of your students
second ques using modulus
class Solution {
public:
bool check(vector& nums) {
int n=nums.size();
int count = 0;
for(int i=0;inums[(i+1)%n]){
count++;
}
}
return count
Bhaiya best video series for cp🥰🥰
My Approach for the first question is -->
void rotate(vector arr, int key)
vector::iterator it = arr.begin() + key - 1;
reverse(arr.begin(), arr.end());
reverse(it+1, arr.end());
it = arr.begin() + key;
reverse(arr.begin(),it);
What can be the optimised solution for add arrays❓
Thank you for the course Bhaiya
Best explanation 🔥
what is &nums represent in vector&nums
we have to use ans.pop_back(sum);
in 3rd question to avoid reverse function
Another solution for finding if the array is rotated or sorted or both:
#include
using namespace std;
void check(int arr[], int n){
int count = 0;
for(int i = 0; iarr[i+1]){
count++;
}
}
if(count==1 && arr[0]>arr[n-1]){
cout
1) time complexity- O(n)
2) O(n);
3) O(n+m);
thanks so much bhaiya for your effort
(my approach to q3: using some old tricks taught by luv bhaiya ❤️)
vector findArraySum(vector&a, int n, vector&b, int m) {
int num1 = 0 ;
int num2 = 0 ;
int sum = 0;
vector ans;
for(int i = 0;i
Can you help in explaining the time complexity of this solution
your solution is good enough. but don't repeat the step to calculate num1 and num2. Just define a function to do that for u.
Bhaiya my approach was like first convert both the arrays in integer (by int ans=0,. For (int i=0;i
Thank you Bhaiya, bahut mast hai
Luv bhaiya kamaal kr dete ho app
Another solution for Array rotation : (Using Reversal Algorithm)
void rotate(vector& 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)
@babbar bhaiya for first question can we do it with space complexity O(1)
thanks a lot bhaiyya ...... shayad lagta hai iss bar mera DSA complete ho jayega .... target 17 march 2022
my apporach for q3
eg
first we take a[ ] = { 4,5,1} and b[ ] = { 3,4,5}
we add a as 451 by (ans x 10) + digit ( i.e here 4) == 4
now ( 4 x 10 ) + 5= 45
(45 x 10) +1 = 451
similarly we get 345
now we add them to get 796
now
Get the last digit of the number
Insert the digit at the beginning of the vector
number /= 10; // Remove the last digit from the number
to get c[ ] ={7,9,6}
open for suggestions
Present Bhaiya. Commenting for a better reach
Time Complexities
Q1- Time=O(n)
Space=O(n)
Q2- Time=O(n)
Space=O(1)
Q3- Time=O(n+m)
Space=O(n+m)
q3-
tc-> O(max(n,m))
sc->O(max(n,m))
is it correct?
bool check(vector &arr){
int count =0;
for(int i=1; iarr[i] % arr.size())
count++;
}
return count
bhaiya agar answer array mei store karteh toh problem ho sakti thi as size is fixed and swap karthe samay zero in front may be sme hacks nikal gae if carry>0 then only full array print kare nahi size -1.
how to do k rotate in space complexity o(1) without using extra space ?
q1) time complexity- O(n)
q2) O(n);
q3) O(n+m);
babbar is back 🔥🔥🔥
Reverse array algo to rotate the array is better than modulo algo as in-place solution is require and it has better space complexity O(1).
As usual marking present bhaiya
Bhaiyaa last ka code thoda nhi smja.. Parr phle do achhe the 🙌🤘🤘❤❤thanks bhiyaa👏👏🙌🤝nice session on leetcode..