This is the best dsa series on TH-cam!!!!! My request is to everyone who is reading this comment plz share this playlist with your juniors and friends. You can share with at least one friend.
pivot you explain is other and array is sorted but in previous lecture you told us to solve a leetcode question 724 in which pivot means this-: The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
the approach of finding the piovt element is correct and in most o fthe cases it will retrun the correct value, but in inputs like {11,13,15,17}, the pivot will move to index 3 but it is wrong, so aas per me logic we should declare a temp variable outside the loop value 0. after the loop is terminated we will compare the value of temp index with the start index value if the value of the start index is smaller we will return start else we will return temp.. this will help to find the piovt in all cases, whether the array is rotated or not..
Sir you deserve all good and wealth in this world, because you love other as yourself and you always help and support. You have been a great teacher, lecturer, and a Professor to me, and I fully enjoyed your lectures, you explained everything in detail
Bhaiyya aap OP ho pivot wala problem teen baar dekhne ke baad samjh aaya uske baaad toh chize easy kardiye aapne.Thanks bhaiyya having a teacher like you can get me a job in Google.❤❤❤
You deserves millions of subscribers for your hardwork bro. You are way better than code with Harry telusku etc etc. You explains everything twice no one even do it once properly. Hats off to you sir. Keep growing.❤️
49:37 "mujhe lag rha hai ki aapko bilkul smjh nhi aa rha", "mujhe pata hai aapko nhi aa rha " "tension na le ," "mujhe pata lg gya aapko smjh nhi aa rha " VERY VERY ACURATE FOR ME AT THIS MOMENT mai shock ho gya laga aamne saamne padh rha hoon.......😊😊
Staying strong with you...14th lecture! No going back Babbar bro... I love the series till now. Especially the optimized way to find mid-value and also these binary search questions are making me think better. I'll comment in the next video. See you then! Regards, Dharrsan
It has been the best series so far on youtube. This series is really helping me to improve my thinking & logic building skills . Thank you bhaiya...keep it up.
@@yashgupta6574 yes bcoz it is a sorted array so either first element or last element bcome pivoted element so it,s obvious that it would be max or min
2:30 The way you use colors of Indian flag and explain concepts is soo satisfying. You are contributing a lot to India indirectly by making a big change in Education industry.
Thank you so much bhaiya.. ye series bahut achchi hai jab maine dsa start kiya tha to normal sa question banane mai bhi ghanto lag jate the but abhi apki vajah se aaj maine 3 achche questions 40 min mai kar liye.. thank you so much... ab confidence bhi aa rha hai ki ho jayega dsa
Sir two suggestion ..... 1) First one is If possible you give topic time table that which topic you will teach in which day or date so all can atleast prepare the topic understand the topic by themselves and solve some problems related to topic ..... which helps to understand all points that you taught in video 2) And second one is you give all difficulty level question links ( generally medium and hard) and maximum problems of that topic that all can master the topic and solve any problem of that topic in all formats in interview or in CP, or in leetcode and all other platforms....
I think it is not possible because love bhaiya search for topic and possible questions from that topic and make video accordingly. How can you expect that he can provide you dates before.
@@sjcreations2490 No sir ......I think he had some plan in mind what topics that he covered so what his plan related to topic covering , he should give and I am not saying that give 2 months plan day wise but my suggestion is atleast give weekly plan ........ and secondly I think bhaiyaa recorded the session few days before the session upload on TH-cam.....
if anyone finding 11:23 answer here it is class Solution { public: int pivotIndex(vector& nums) { // accumulate is a stl which is used to addition of array int rs=accumulate(nums.begin(),nums.end(),0); int ls=0; for(int i=0;i
31:40 Bhaiya At this timestamp of video , I would suggest try to start an array from [ 1 to n/2 ]. Reason is for any number n , square root of n is always smaller than n/2 ( Corollary Based on popular Theorem ).I don't remember the name of that theorem. This will slightly optimize an algorithm. Example : n = 36 ==> 6 < 18 n = 81. ==> 9 < 40 n = 625 ==> 25 < 312 and so on ..
yes brother, I searched about it and found that the one you mentioned is a corollary of the AM-GM inequality, it states: (m+1)/2 >= sqrt(m) (where m belongs to N)
First I have completed all DSA series and now conclude this is Greatest ever DSA series to exist on youtube or paid courses. Your contribution will be remembered. You're God of DSA for us🙇♂
I have watched many DSA series but your course is one of the best series that I have watched till now, thank you so much for putting so much of efforts for us. I wish you keep growing in every aspect of your life ... :), 1 question, does anyone came to know about the 2nd and 3rd approach of Finding an element in rotated sorted array?
in pivot element, if the array is not rotated in edge case then it will return last index that is wrong so we have to compare with last element not first element in array, int findMin(vector& nums) { int start = 0; int end = nums.size()-1; int mid = start+(end-start)/2; while(startnums[nums.size()-1]){ start = mid+1; }else{ end = mid; } mid = start+(end-start)/2; } return start }
//finding the target in sorted rotated array using binary search //ex-> A = {7,8,9,10,1} // key = 9 //output -> element found at index 2 //code written by harsh shukla #include using namespace std; int PivotInRotSortArr(int arr[] , int size){ int s = 0 , e = size - 1; while(s arr[mid+1]){ return mid+1; } else if(arr[s]
The code that he has explained for pivot index only works when array is rotated at least once, if array is not rotated or else it is rotated n times (where n is arr.size() - 1) it will give incorrect answer on leetcode question 153. And to fix this we can write this on very top of our function: if(nums[0]
Thank you so much bhaiya for bringing such a great, full fledged and premium content in free of cost for all of us ❤🙏🙏🙏..... We are putting our effort 👌 💪 🙂 👏 🙌
UPDATE GUYZZZZZ...........................Also correct me if i am wrong............the solution for sorted and rotated array can't solved every testcase in the leetcode platform it can solve 133/195 cases.............where array is [ 1 ] ...........target = 1 , output goes -1 but answer expected is 0.......we have to come up with an solution for this edge case....
Solution for Search in Sorted Array by splitting in two parts: Approach: 1. Get Pivot 2. If key == arr[pivot] it is the answer 3. if(key>=arr[0]) it lies in first half therefore, s=0, e = pivot-1 4. else, it lies in second half therefore s= mid, e = size-1 5. Now perform BS based on the selected values of e and s. Complexity = O(logn) Code: int getPivot(vector& arr,int n){ int s = 0; int e = n-1; int mid = s + (e-s)/2; while(s=arr[0]){ s= mid+1; }else{ e = mid; } mid = s+(e-s)/2; } return s; } int findPosition(vector& arr, int n, int k) { int pivot = getPivot(arr,n); if(arr[pivot]==k){ return pivot; } int s,e; //splitting array based on pivot element if(k>=arr[0]){ e = pivot-1; s = 0; } else{ e = n-1; s = pivot; } int mid = s + (e-s)/2; while(sk){ e = mid-1; //move left }else{ s = mid+1; } mid = s+(e-s)/2; } return -1; }
Easier way to implement precision using brute force double squareroot(int element, int integerpart, int precision){ double ans=integerpart; double factor=1; for (int i=1; i
Note - For precision part we can make a function and convert the precision int into floating point num.... like 3 gets converted to 0.001 float num = 37; float ans = findSqrt(num); while (true) { if ((ans + 0.01) * (ans + 0.01) < num) { ans += 0.01; } else { break; } } cout
A better approach for the second question. according to you, the process/flow chart is- 1- finding pivot. 2- apply that target formula and find the portion of the monotonic function. 3-Then apply Binary search to find the index value. according to me. 1- if(key>=arr[0]){ //apply binary in the first segment } else { //apply binary in second segment } I am not an expert on time complexity, just have seen your previous video. but for your approach time complexity should be 1 O(log2^n)+O(log2^n) and for my approach, it should be O(log2^n) P.s I've just started DSA, so correct me if I am wrong.
Edit- I found my mistake. How can I apply binary search in the first or second segment if I dont know the pivot element(i.e. start of second segment and end of the first segment.)
// THis is what i did and it was accepted in leetcode 33 int search(vector& nums, int target) { int s=0; int e=nums.size()-1; int mid=s+(e-s)/2; while(s=nums[0]){ if(nums[mid]>target) e=mid-1; else if(nums[mid]>=nums[0]) s=mid+1; else e=mid-1; } else{ if(nums[mid]=nums[0]) s=mid+1; else e=mid-1; } mid=s+(e-s)/2; } return -1; }
For the last question, i.e. sqrt upto floating part, this also works: #include float sqrt(float num) { float left = 0; float right = num; float midpoint = (right+left)/2; float prev_mid = -35; while(leftnum) { right = midpoint; } else if((midpoint *midpoint)>num; std::cout
Sorry sir , but the question in last video (Find Pivot in an array) was actually a point where left sum = right sum , but in video you are explaining something else . Please clarify this sir .
bro, at that point he means smallest in array. And in leet code question 33, there is definition of pivot where leftSum = rightSum. Hope you understand , that it is only a word that is vary from concept to concept
> The Pivot index** is the index where the sum of all the numbers **strictly** to the left of the index is equal to the sum of all the numbers **strictly** to the index's right. **Pivot element** is the only element in input array ( Which is already a rotated sorted array) which is smaller than it's previous element. A pivot element divided a sorted rotated array into two monotonically increasing array. >
ye jo first question hai pivot element wala usme hum arr[0] se na compare keke balki arr[end] se kare to jada best hoga kyuki hume nhi pta hota ki sorted array kitni baar rotate kia gya hai kya pata vo rotate hoke sirf sorted hi hogya ho jaise ki {11,12,13,14,15} ye array rotate hoke yhi ban gya to arr[0] se hamara ans galat ayega but arr[end] se hamesha shi ayega ....
Bhai really learning a lot from you... Really like your videoes. Now I feel like I am being helped by my elder brother. Thank you so much bhaiya... Just one thing more than 1 hr video ko miss kr rha hu... Thank you so much bhaiya for helping.
what did i learn from this lecture 1.how to find pivot element in rotated and sorted array 2. how to find an element in rotated and sorted array 3. how to find the square root of an element 4. how to find the precesion of the square root
Rotated Sorted array using the second approach. int search(int* arr, int n, int k) { // Write your code here. int s = 0 , e = n - 1 , mid; while( s k) e = mid -1; else if(k > arr[0] && arr[mid] < k) s = mid + 1; else if(k > arr[0] && arr[mid] > k) e = mid -1; else return mid; } return -1; }
Leaving this comment so that after an year if someone liked this cmmnt i'll be appreciating myself for my hard work. nd surely at that time i'll work in a good MNC. love from ghaziabad bhaiya:)
Sir the code for pivot element is not working for some edge cases such as if the sorted array is rotated by zero or rotated by n whr n being the no of elements in the array is that true or i m doing somewhere wrong.
bhaiya when we google search and on Leetcode also ..it defines pivot of an array as an index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. can i get some more clarity on pivot of an array .. I'm little confused.
thank you bhaiya..bhot mzza aa rha hai...I observed you are working late night till 5 in the morning😂😂. bhot hard...bhot hard....bs josh thanda na pdne dena
@CodeHelp - by Babbar! Bhaiya, you haven't mentioned the solution of " Q-724>> Find the Pivot Element" yet in leetcode? please, plz make our concept clear regarding that problem.
@@arnobbaiga309 bro comparision jo h vo phele hua like jab loop khtm nhi hua h and then jab ekk iteration puri hui uske baad s = 4 , e = 4 hua then loop end hogya
One doubt in sqre rot question - Can we not find precision again with binary search. For example- if we want precision of 2, then s=0.01 and e= 0.99 and then we can calculate via binary search.
I am so happy that i solved the square root of n question by myself after understanding the first 2 questions. Thank you so much for this useful lecture
My friend tell me about this daa lecture series and now I learning this series for placement this time my second year is about to completed and really thank you bhaiya ❤️❤️❤️❤️
What is the 2nd and 3rd approach for the Searching element in the sorted rotated array? He talks about it at 27:44 in the video. Can anyone please share their approaches?
// search in a sorted rotated array #include using namespace std; int search(vector& nums, int target) { int s=0; int e=nums.size()-1; while(s=nums[0]){ //left side if(target>=nums[s] && targetnums[mid] && target
//Search in a Rotated and Sorted Array #include using namespace std; int search(int a[], int n, int key) { int beg=0, end=n-1, mid; while(beg=a[0]) { if(keykey) end=mid-1; else beg=mid+1; } else if(a[mid]=a[0]) end=mid-1; else if(a[mid]>key) end=mid-1; else beg=mid+1; } } return -1; } int main() { int a[]={7,9,10,1,2,3}; int n=sizeof(a)/sizeof(a[0]); int key=2; cout
Square Root of an Integer int floorSqrt(int n) { int s=1; // take it from 1 because all postive integer's root at least one not 0 int e=n; int ans =1; while(s
Search in a Rotated and Sorted Array problem ! Another approach: int Search(vector& arr, int n, int k) { // Return the position of K in ARR else return -1. int start = 0; int end = n-1; // int mid = start +(end - start) /2; while(start
ksm s mja hi aa jata h bhai yr lecture dekh k...I appreciate u with bottom of my heart!...keep doing the good work. best wishes. p.s. I'm rewatching this series :)
Is find pivot in an array question (In this Video) is wrong??? Because I searched on Google and got definition of pivot array that The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. I am so confused 😕 Edit - I think bhaiya is discussing find pivot Index in sorted and rotated array and mistakenly tell that he is answering find pivot in an array.
bhaiya pivot array to google pr kuchh aur hi dikh rha h An element in an array is a pivot element if the sum of all the elements in the list to its left is equal to the sum of all the elements to its right. EX : {1,2,3,7,6} here 7 is pivot element as 1+2+3 = 6
bhaiya wo pivot wale question ka kuch or Matlab dia hua tha leet code pe... find pivot index:- it is a index where sum of all numbers strictly to the left side of the index is equal to the sum of all the no.s strictly to the index's right.Return the leftmost pivot index.
Commenting for reach bhaiya, mai abhi 10th mei but I am preparing for INOI, so cp mei c++ aata hai and aapka hi course dekhunga promise. Abhi mai sabmei commenting for reach krta rahunga Love you 3000 babbar bhai ❤️
search a element wala question aap agar vs code par karte to uske syntax ko likhne mai prolem nhi hoti but apney usko leetcode par solve kiya ab uske syntax solve karne mai bhot errors aa rahe hai bhaiya pls help
this fails at one edge case, when the array is not rotated or rotated by n elements v= [1,2,3] rotated by 3 [1,2,3] so look for the constraints in the question by how many places is the pivot rotated
In the finding the pivot element basically we are finding the minimum of the elements in the array and then displaying the index right? so that could also be done simply by finding the minimum element and rerturning its index?
yes but then you need to traverse the whole array and complexity will be O(n). Here, since we are doing Binary search, its O(log n). I think that's the only reason, otherwise yes, we are just finding the index at minimum value.
16:53 Sir Ap na yah jo pivot element ka code leakha hai es Array pay FAIL hogiya {50,1,60,70,80,90} It gives the last element which is wrong answer but the correct is second element
Do make sure to visit Relevel: relvl.co/tv0v :-)
Bhiya scope of data science par ek video bano please
🔥🔥
bhaiya vo square root wale me Baar Baar SIGTERM output para hai kya cru
#love bahiya yeh code studio vala square root question ka code dedo usme kuch case big no vale submit krne pe fail ho rhe h
th-cam.com/video/tAQIKe0UGH4/w-d-xo.html
This is the best dsa series on TH-cam!!!!!
My request is to everyone who is reading this comment plz share this playlist with your juniors and friends. You can share with at least one friend.
chal bhai age gyan mt pe***
pivot you explain is other and array is sorted but in previous lecture you told us to solve a leetcode question 724 in which pivot means this-:
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
exactly..the one he used is for rotated array question
the approach of finding the piovt element is correct and in most o fthe cases it will retrun the correct value, but in inputs like {11,13,15,17}, the pivot will move to index 3 but it is wrong, so aas per me logic we should declare a temp variable outside the loop value 0. after the loop is terminated we will compare the value of temp index with the start index value if the value of the start index is smaller we will return start else we will return temp.. this will help to find the piovt in all cases, whether the array is rotated or not..
Sir you deserve all good and wealth in this world, because you love other as yourself and you always help and support. You have been a great teacher, lecturer, and a Professor to me, and I fully enjoyed your lectures, you explained everything in detail
Another approach to find pivot in an array !!!
int getpivot(int arr[],int n){
int s = 0;
int e = n-1;
int mid = s + (e-s)/2;
while(s
if mid is the pivoted element?
Bhaiyya aap OP ho pivot wala problem teen baar dekhne ke baad samjh aaya uske baaad toh chize easy kardiye aapne.Thanks bhaiyya having a teacher like you can get me a job in Google.❤❤❤
You deserves millions of subscribers for your hardwork bro. You are way better than code with Harry telusku etc etc. You explains everything twice no one even do it once properly. Hats off to you sir. Keep growing.❤️
ok
@@sagartaak4017 😂😂😂🤦
@@lovishmittal4859 jukh ke rhena padega
Difference of Love babbar and Aman dattarwal course is :-
Aman dattarwal course made by FAANG people's and love babbar course made to crack FAANG
Do not forget that he has also worked in amazon for 2 years
49:37
"mujhe lag rha hai ki aapko bilkul smjh nhi aa rha",
"mujhe pata hai aapko nhi aa rha "
"tension na le ,"
"mujhe pata lg gya aapko smjh nhi aa rha "
VERY VERY ACURATE FOR ME AT THIS MOMENT
mai shock ho gya laga aamne saamne padh rha hoon.......😊😊
bhai yehi likne ke liye aya tha scroll krta niche...these lines were Fr
Staying strong with you...14th lecture!
No going back Babbar bro...
I love the series till now.
Especially the optimized way to find mid-value and also these binary search questions are making me think better. I'll comment in the next video. See you then!
Regards,
Dharrsan
placement hogyi?
placement hogyi?
@@lalitsharma3137 haa 5000 rupay mahina. maine hi internship di h isko
@@understanding-why 😂😂
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
It has been the best series so far on youtube. This series is really helping me to improve my thinking & logic building skills .
Thank you bhaiya...keep it up.
is it correct that pivot element is min or max element of an array?
@@yashgupta6574 yes bcoz it is a sorted array so either first element or last element bcome pivoted element so it,s obvious that it would be max or min
2:30 The way you use colors of Indian flag and explain concepts is soo satisfying. You are contributing a lot to India indirectly by making a big change in Education industry.
may be he didn't intend that!
@@arjunyadav-kt5jr
padle bhai bharat sarkaar free ki naukri nahi degi....khud hi karna h jo karna h
Thank you so much bhaiya.. ye series bahut achchi hai jab maine dsa start kiya tha to normal sa question banane mai bhi ghanto lag jate the but abhi apki vajah se aaj maine 3 achche questions 40 min mai kar liye.. thank you so much... ab confidence bhi aa rha hai ki ho jayega dsa
aapne yhi question 40 min mai kiye ki dusre que 40 min mai kiye
please reply
Sir two suggestion ..... 1) First one is If possible you give topic time table that which topic you will teach in which day or date so all can atleast prepare the topic understand the topic by themselves and solve some problems related to topic ..... which helps to understand all points that you taught in video 2) And second one is you give all difficulty level question links ( generally medium and hard) and maximum problems of that topic that all can master the topic and solve any problem of that topic in all formats in interview or in CP, or in leetcode and all other platforms....
Yes✌️✌️✌️✌️✌️✌️✌️
I think it is not possible because love bhaiya search for topic and possible questions from that topic and make video accordingly. How can you expect that he can provide you dates before.
@@sjcreations2490 No sir ......I think he had some plan in mind what topics that he covered so what his plan related to topic covering , he should give and I am not saying that give 2 months plan day wise but my suggestion is atleast give weekly plan ........ and secondly I think bhaiyaa recorded the session few days before the session upload on TH-cam.....
i just type the problem in google and leetcode in it and the first result is the leetcode problem. Hope this helps.
th-cam.com/video/tAQIKe0UGH4/w-d-xo.html
if anyone finding 11:23 answer here it is
class Solution {
public:
int pivotIndex(vector& nums) {
// accumulate is a stl which is used to addition of array
int rs=accumulate(nums.begin(),nums.end(),0);
int ls=0;
for(int i=0;i
31:40 Bhaiya At this timestamp of video , I would suggest try to start an array from [ 1 to n/2 ]. Reason is for any number n , square root of n is always smaller than n/2 ( Corollary Based on popular Theorem ).I don't remember the name of that theorem. This will slightly optimize an algorithm.
Example : n = 36 ==> 6 < 18
n = 81. ==> 9 < 40
n = 625 ==> 25 < 312 and so on ..
yes brother, I searched about it and found that the one you mentioned is a corollary of the AM-GM inequality, it states:
(m+1)/2 >= sqrt(m) (where m belongs to N)
Hii.. This will be true for all numbers except 2... because sqroot of 2 is 1.414, which is greater than its half (1).
Thanks.
@@deepbhatia_1511 also it won't hold true for 3 as well, as root 3= 1.732 and 3/2=1.5 ....
We all are with you bhaiya. please be constant till the end. Your way of teaching is excellent.
Truly Great series sir, You have truly helped me overcome my fear of DSA.
Thank You very Much.
First I have completed all DSA series and now conclude this is Greatest ever DSA series to exist on youtube or paid courses. Your contribution will be remembered. You're God of DSA for us🙇♂
Congratulations for the completion bro 🎉❤
are you placed
I have watched many DSA series but your course is one of the best series that I have watched till now, thank you so much for putting so much of efforts for us. I wish you keep growing in every aspect of your life ... :),
1 question, does anyone came to know about the 2nd and 3rd approach of Finding an element in rotated sorted array?
well the second approach can be done by using the if condition nums[start]
in pivot element, if the array is not rotated in edge case then it will return last index that is wrong so we have to compare with last element not first element in array,
int findMin(vector& nums) {
int start = 0;
int end = nums.size()-1;
int mid = start+(end-start)/2;
while(startnums[nums.size()-1]){
start = mid+1;
}else{
end = mid;
}
mid = start+(end-start)/2;
}
return start
}
If it is not rotated then use binary search algorithm since it is increasing Order
//finding the target in sorted rotated array using binary search
//ex-> A = {7,8,9,10,1}
// key = 9
//output -> element found at index 2
//code written by harsh shukla
#include
using namespace std;
int PivotInRotSortArr(int arr[] , int size){
int s = 0 , e = size - 1;
while(s arr[mid+1]){
return mid+1;
}
else if(arr[s]
very helpful, thanks!!
in 15:47 start and end both pointing at 4, so the loop stop here only as in while condition (s
Do I find any solution for that
The code that he has explained for pivot index only works when array is rotated at least once, if array is not rotated or else it is rotated n times (where n is arr.size() - 1) it will give incorrect answer on leetcode question 153.
And to fix this we can write this on very top of our function: if(nums[0]
we can also use this
int start=0,end=size-1,mid;
while(startarr[end])
start=mid+1;
else if(arr[mid]
faced this exact problem. thanks.
the code for this problem( search in rotated sort array) given on gihub gives compilation error...is it with everyone?
For that you can use the following condition in the beginning itself:-
if(arr[0]
TLE aarha
Thank you so much bhaiya for bringing such a great, full fledged and premium content in free of cost for all of us ❤🙏🙏🙏.....
We are putting our effort 👌 💪 🙂 👏 🙌
Explaining T.C. and S.C. of all the problems after soln. will be very helpful
ATTENDANCE MARKED, VISITED RELEVEL, MADE NOTES, SOLVED QUESTIONS, LIKED THE VIDEO, EXITED WITH A SMILE.
BEST TEACHER AWARD GOES TO YOU BHAIYA😇
UPDATE GUYZZZZZ...........................Also correct me if i am wrong............the solution for sorted and rotated array can't solved every testcase in the leetcode platform it can solve 133/195 cases.............where array is [ 1 ] ...........target = 1 , output goes -1 but answer expected is 0.......we have to come up with an solution for this edge case....
good
Solution for Search in Sorted Array by splitting in two parts:
Approach:
1. Get Pivot
2. If key == arr[pivot] it is the answer
3. if(key>=arr[0]) it lies in first half therefore, s=0, e = pivot-1
4. else, it lies in second half therefore s= mid, e = size-1
5. Now perform BS based on the selected values of e and s.
Complexity = O(logn)
Code:
int getPivot(vector& arr,int n){
int s = 0;
int e = n-1;
int mid = s + (e-s)/2;
while(s=arr[0]){
s= mid+1;
}else{
e = mid;
}
mid = s+(e-s)/2;
}
return s;
}
int findPosition(vector& arr, int n, int k)
{
int pivot = getPivot(arr,n);
if(arr[pivot]==k){
return pivot;
}
int s,e;
//splitting array based on pivot element
if(k>=arr[0]){
e = pivot-1;
s = 0;
}
else{
e = n-1;
s = pivot;
}
int mid = s + (e-s)/2;
while(sk){
e = mid-1; //move left
}else{
s = mid+1;
}
mid = s+(e-s)/2;
}
return -1;
}
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
@@arnobbaiga309 sir ne ek step aur lga de galti sai
actually it dont take olace
@@rahulgopalka2403 right!!
Present sir.... Your student josh is always high for the exciting class of DSA#lovebabbar
This series is very helpful; thanks a lot ❤️
Easier way to implement precision using brute force
double squareroot(int element, int integerpart, int precision){
double ans=integerpart;
double factor=1;
for (int i=1; i
Note - For precision part we can make a function and convert the precision int into floating point num.... like 3 gets converted to 0.001
float num = 37;
float ans = findSqrt(num);
while (true) {
if ((ans + 0.01) * (ans + 0.01) < num) {
ans += 0.01;
} else {
break;
}
}
cout
Nested for loops bro... 😐😐😐
t's my humble request please maintain this consistency till end. We all are with you bhaiya. Sab ek sath placed honge👍and thankyou
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
Search in rotated sorted array brute force solution:-
int search(vector& arr, int n, int k)
{
for(int i =0; i
A better approach for the second question.
according to you, the process/flow chart is-
1- finding pivot.
2- apply that target formula and find the portion of the monotonic function.
3-Then apply Binary search to find the index value.
according to me.
1- if(key>=arr[0]){
//apply binary in the first segment
}
else {
//apply binary in second segment
}
I am not an expert on time complexity, just have seen your previous video.
but for your approach time complexity should be
1 O(log2^n)+O(log2^n)
and for my approach, it should be O(log2^n)
P.s I've just started DSA, so correct me if I am wrong.
Edit- I found my mistake.
How can I apply binary search in the first or second segment if I dont know the pivot element(i.e. start of second segment and end of the first segment.)
Exactly you can not apply binary search in an array without knowing 1st and last index of array
// THis is what i did and it was accepted in leetcode 33
int search(vector& nums, int target) {
int s=0;
int e=nums.size()-1;
int mid=s+(e-s)/2;
while(s=nums[0]){
if(nums[mid]>target)
e=mid-1;
else if(nums[mid]>=nums[0])
s=mid+1;
else
e=mid-1;
}
else{
if(nums[mid]=nums[0])
s=mid+1;
else
e=mid-1;
}
mid=s+(e-s)/2;
}
return -1;
}
@@adarshtiwari2727 brother could you please explain me the Algo ??i can't solve it in leetcode
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
For the last question, i.e. sqrt upto floating part, this also works:
#include
float sqrt(float num)
{
float left = 0;
float right = num;
float midpoint = (right+left)/2;
float prev_mid = -35;
while(leftnum)
{
right = midpoint;
}
else if((midpoint *midpoint)>num;
std::cout
Sorry sir , but the question in last video (Find Pivot in an array) was actually a point where left sum = right sum , but in video you are explaining something else . Please clarify this sir .
bro, at that point he means smallest in array.
And in leet code question 33, there is definition of pivot where leftSum = rightSum.
Hope you understand , that it is only a word that is vary from concept to concept
@@anuragsrivastava1995 thank you for trying but you just made it worse for me 😂😂🙏🏽
@@krishnaprabeesh2415 😂😂
> The Pivot index** is the index where the sum of all the numbers **strictly** to the left of the index is equal to the sum of all the numbers **strictly** to the index's right.
**Pivot element** is the only element in input array ( Which is already a rotated sorted array) which is smaller than it's previous element. A pivot element divided a sorted rotated array into two monotonically increasing array.
>
@@vaibhavagrawal431 take one example like 3 4 1 2
Pivot element -1
Sum of left of pivot element -7
Sum of right of pivot element-2
Which is not equal
ye jo first question hai pivot element wala usme hum arr[0] se na compare keke balki arr[end] se kare to jada best hoga kyuki hume nhi pta hota ki sorted array kitni baar rotate kia gya hai kya pata vo rotate hoke sirf sorted hi hogya ho jaise ki {11,12,13,14,15} ye array rotate hoke yhi ban gya to arr[0] se hamara ans galat ayega but arr[end] se hamesha shi ayega ....
East or west, bhaiya is the best!
Ok didi
Bhai really learning a lot from you... Really like your videoes. Now I feel like I am being helped by my elder brother. Thank you so much bhaiya... Just one thing more than 1 hr video ko miss kr rha hu... Thank you so much bhaiya for helping.
MINIMUM ELEMENT IN AN SORTED AND ROTATED ARRAY-
int findMin(int arr[], int n){
//complete the function here
if(arr[0]
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
what did i learn from this lecture
1.how to find pivot element in rotated and sorted array
2. how to find an element in rotated and sorted array
3. how to find the square root of an element
4. how to find the precesion of the square root
Search an Element in a Sorted & Rotated array using Binary Search
beat 100 percent without see and discuss answer , now feelling motivated
Rotated Sorted array using the second approach.
int search(int* arr, int n, int k) {
// Write your code here.
int s = 0 , e = n - 1 , mid;
while( s k) e = mid -1;
else if(k > arr[0] && arr[mid] < k) s = mid + 1;
else if(k > arr[0] && arr[mid] > k) e = mid -1;
else return mid;
}
return -1;
}
Code sahi run nahi kar raha bhai!!...
bhai aapke code ki second and third condition wrong he.....
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
apke ka course bhai sab courses s unique is liye h kyuki apne har topic p bohot sare question karaye h , jo kisi ne nhi karaye , best than paid course
AMAZING! I was feeling really low today and I really wanted to give up, but I finished it in the end! Super glad :) Thanks bhaiya!
Bhai aap fire ho... aapne ye course na krwaya hota toh DSA will alaways be a scary thing for most of us. Keep growing brother🔥🔥
Leaving this comment so that after an year if someone liked this cmmnt i'll be appreciating myself for my hard work. nd surely at that time i'll work in a good MNC.
love from ghaziabad bhaiya:)
what's your update bro
@@Codeycodee well right now I'm pursuing MCA from BHU and getting placed in my 3rd sem🫂
@@Codeycodee what about you brother
@@tusharsingh1915 update brother?
got placed?
good love this video.....become millionare soon bro
Thank u bhaiya for giving such a great content 🥰🥰
Bhaiya aap kamaal ho mazaa hi ajata hai ye quality Maine abhi tak kisi course main nahin dekhi
Sir the code for pivot element is not working for some edge cases such as if the sorted array is rotated by zero or rotated by n whr n being the no of elements in the array
is that true or i m doing somewhere wrong.
Not going to quit ...definitely i will complete this whole series properly babbar bhaiya
so on which video u r on?
bhaiya when we google search and on Leetcode also ..it defines pivot of an array as an index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. can i get some more clarity on pivot of an array .. I'm little confused.
broh, apka confusion solve hua if yes then mujhe v btayen plz
thank you bhaiya..bhot mzza aa rha hai...I observed you are working late night till 5 in the morning😂😂.
bhot hard...bhot hard....bs josh thanda na pdne dena
@CodeHelp - by Babbar! Bhaiya, you haven't mentioned the solution of " Q-724>> Find the Pivot Element" yet in leetcode?
please, plz make our concept clear regarding that problem.
bhai pivot element hota kya h , plzz explain !!!!
@@summitrawat1093 pivot = minimum element in rotated sorted array
Search sorted array mein ham log direct arr[0] se compare nhi kr skte??
If k>arr[0] lies in first prt
Else lies in 2nd part
Quality of the content is great, please continue the work.
Much appreciated!
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
@@arnobbaiga309 bro comparision jo h vo phele hua like jab loop khtm nhi hua h and then jab ekk iteration puri hui uske baad s = 4 , e = 4 hua then loop end hogya
maza aagay bhaayya ji . By watching you it keeps increasing my interest in dsa❤❤
Hat's off to the dedication, every concept is crystal clear, commenting for better reach!!!
can u tell me why comparision takes place in 15;40 _ as s=4 and end =4 while statement doesnt satisfy here
One doubt in sqre rot question - Can we not find precision again with binary search.
For example- if we want precision of 2, then s=0.01 and e= 0.99 and then we can calculate via binary search.
bro can u help me with my doubt,
@@computerscience1099 Yes tell me
Excellent stuff and loving this DSA series
I am so happy that i solved the square root of n question by myself after understanding the first 2 questions. Thank you so much for this useful lecture
Love you bro please be constant till the end keep sharing your knowledge May god bless you
Love bhai how to write the code if the question is twisted
My friend tell me about this daa lecture series and now I learning this series for placement this time my second year is about to completed and really thank you bhaiya ❤️❤️❤️❤️
You are amazing bhai ❤❤. Just one Question can we find square root of float numbers too using binary search?
Mast Samjhaya ha Bhaiyya Aapne. Maza aa gaya.
On to the Next Video.
14 Videos Completed. 135 more to go.
लाजबाब जबरजस्त जिंदाबाद🙏🙏🙏
All are going good...The way you present the lecture is excellent. Thank you so much for this DSA course
What is the 2nd and 3rd approach for the Searching element in the sorted rotated array?
He talks about it at 27:44 in the video.
Can anyone please share their approaches?
Approch 2:
int start = 0, end = n - 1;
int mid;
while (start
Samajh toh aa raha hai but I think I need to practice these questions few times in a single week !!! Thank you
// search in a sorted rotated array
#include
using namespace std;
int search(vector& nums, int target) {
int s=0;
int e=nums.size()-1;
while(s=nums[0]){
//left side
if(target>=nums[s] && targetnums[mid] && target
Great work ✅👍🏻👍🏻
anuj bhiya ka chap diya kya?
Over powered -------> Babbar Bhaiya
//Search in a Rotated and Sorted Array
#include
using namespace std;
int search(int a[], int n, int key)
{
int beg=0, end=n-1, mid;
while(beg=a[0])
{
if(keykey)
end=mid-1;
else
beg=mid+1;
}
else if(a[mid]=a[0])
end=mid-1;
else if(a[mid]>key)
end=mid-1;
else
beg=mid+1;
}
}
return -1;
}
int main()
{
int a[]={7,9,10,1,2,3};
int n=sizeof(a)/sizeof(a[0]);
int key=2;
cout
Square Root of an Integer
int floorSqrt(int n)
{
int s=1; // take it from 1 because all postive integer's root at least one not 0
int e=n;
int ans =1;
while(s
Just completed the video explanation with the graph of pivot was soo good.
Akri thak data raha 🎉
Approach for Pivot Index:
int numsLeft = 0;
int numsRight = accumulate(nums.begin(), nums.end(), 0);
for(int i = 0; i < nums.size(); i++)
{
numsRight -= nums[i];
if(numsRight == numsLeft)
{
return i;
}
numsLeft += nums[i];
}
return -1;
but complexity will be O(n) right?
Search in a Rotated and Sorted Array problem !
Another approach:
int Search(vector& arr, int n, int k)
{
// Return the position of K in ARR else return -1.
int start = 0;
int end = n-1;
// int mid = start +(end - start) /2;
while(start
thanks, sir wali approach mein problem thi 🙏
I am strongly following you bhaiya.... Thanks, bhaiya for your efforts. This is my consistent 14th lecture.
Precision finding was absolutely awesome
You really deserve more views and likes
ksm s mja hi aa jata h bhai yr lecture dekh k...I appreciate u with bottom of my heart!...keep doing the good work.
best wishes.
p.s. I'm rewatching this series :)
just appreciate it with the your back bottom he wiill be happy
you are the best teacher for DSA babbar bhaiya ,
thank to that friend who recommended your channel for DSA
Is find pivot in an array question (In this Video)
is wrong???
Because I searched on Google and got definition of pivot array that The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
I am so confused 😕
Edit - I think bhaiya is discussing find pivot Index in sorted and rotated array and mistakenly tell that he is answering find pivot in an array.
bhaiya pivot array to google pr kuchh aur hi dikh rha h
An element in an array is a pivot element if the sum of all the elements in the list to its left is equal to the sum of all the elements to its right. EX : {1,2,3,7,6} here 7 is pivot element as 1+2+3 = 6
if only he's a professor in college, then that college would be happiest place on earth --(big fan bhai🖤🖤)
East to West Love Babbar Bhaiya is the best ❤️🙇
bhaiya wo pivot wale question ka kuch or Matlab dia hua tha leet code pe...
find pivot index:-
it is a index where sum of all numbers strictly to the left side of the index is equal to the sum of all the no.s strictly to the index's right.Return the leftmost pivot index.
Commenting for reach bhaiya, mai abhi 10th mei but I am preparing for INOI, so cp mei c++ aata hai and aapka hi course dekhunga promise. Abhi mai sabmei commenting for reach krta rahunga
Love you 3000 babbar bhai ❤️
Commenting before completing the video, kyoki ye series mae maza aa raha hai😁 and for better reach
world best free course of DSA thank you Bhai Costly content ko Free me dene ke liye❤❤
This course is best course for improving the problem solving in DSA
One hour and I am going to say that you are a great teacher
search a element wala question aap agar vs code par karte to uske syntax ko likhne mai prolem nhi hoti but apney usko leetcode par solve kiya ab uske syntax solve karne mai bhot errors aa rahe hai bhaiya pls help
this fails at one edge case, when the array is not rotated or rotated by n elements v= [1,2,3] rotated by 3 [1,2,3] so look for the constraints in the question by how many places is the pivot rotated
In the finding the pivot element basically we are finding the minimum of the elements in the array and then displaying the index right? so that could also be done simply by finding the minimum element and rerturning its index?
is it correct that pivot element is min or max element of an array?
yes but then you need to traverse the whole array and complexity will be O(n). Here, since we are doing Binary search, its O(log n). I think that's the only reason, otherwise yes, we are just finding the index at minimum value.
Bhaiya apke vedio to jabardast hai hi kya hi kehna ,
lekin jo vedio ke starting me codehelp ka logo ata hai uska background music bhi jabardast hai
16:53 Sir Ap na yah jo pivot element ka code leakha hai es Array pay FAIL hogiya {50,1,60,70,80,90}
It gives the last element which is wrong answer
but the correct is second element
bhaiya isse bhadiya content mujhe kahi nhi mila.I really your appreciate your efforts.Keep it up Bhaiya ji