No video

Search Insert Position | Leetcode #35

  Рет қаралды 51,603

Techdose

Techdose

Күн бұрын

This video explains a very basic programming interview question which is to find the correct position to insert an element in a already sorted array.All elements of array are sorted.The element may be already present in array and so we just need to return its index in this case.If element is not present then return the index where it can be inserted so that array remains in ascending order.This can be simply solved in linear time using linear search but since the array is sorted, we can apply binary search which will reduce time to logN.I have shown all possible cases with examples and the code walkthrough at the end of the video.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
=================================================================
CODE LINK: gist.github.co...

Пікірлер: 74
@manmeetsingh8349
@manmeetsingh8349 Жыл бұрын
class Solution { public: int searchInsert(vector& nums, int target) { for (int i = 0; i < nums.size(); i++) { if (target
@chetanmuliya6030
@chetanmuliya6030 4 жыл бұрын
Consisteny is the key to get you anywhere you wish, and now i feel i am getting there thank you sir learn a lot from you Big hug.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@anirudh8575
@anirudh8575 3 жыл бұрын
for i in range(len(nums)): if nums[i]==target: return i if nums[i]>target: # If nums[i] is greater, this means that target should be in nums[i] return i if targetnums[len(nums)-1]: return len(nums)
@abhishekverma7604
@abhishekverma7604 Жыл бұрын
ultimately,in all cases we have to return low ,which simultaneously cover all the corner cases.. great explanation sir...
@avadheshsingh4255
@avadheshsingh4255 3 жыл бұрын
sir actually we need to apply this condition in order to know the correct position of the element to be inserted ->>>> return nums[low]
@ayeshaadhikari6123
@ayeshaadhikari6123 2 жыл бұрын
Thank you so much sir for helping so many of us with your knowledge! :)
@vishalakkalkote1344
@vishalakkalkote1344 2 жыл бұрын
class Solution { public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length - 1; while(start target) { return start; } else { start ++;} if(nums[end] < target) { return end + 1;} else {end --;} } return start; } }
@quandingleberry445
@quandingleberry445 Жыл бұрын
class Solution { public int searchInsert(int[] nums, int target) { if (target > nums[nums.length-1]) { return nums.length; } if (target < nums[0]) { return 0; } for (int i = 0; i < nums.length; i++) { if (nums[i] == target) { return i; } else if (nums[i] < target && nums[i+1] > target) { return i+1; } } return 0; } }
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
Thanks again for the explanation! I just printed low and high at the end of loop for 5-6 test cases and saw that low is always pointing to the correct index. :))) Also congo for 16k subs. Way to go! I think in about a month or so there will definitely be 50k subs or more.
@techdose4u
@techdose4u 4 жыл бұрын
😂 You are overestimating man 😅
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
@@techdose4u No sir I remember we had 6k subs in April challenge. Now more and more people are doing June challenge, also placement preparation is in full swing, if anyone comes across this channel they don't need to go anywhere else for placement prep once they see the videos here. I firmly believe by July end we will have 50k!!
@mohammedfalih8713
@mohammedfalih8713 3 жыл бұрын
his great efforts truly deserve ... i hope he will reach it someday soon
@jobanpreetsingh2370
@jobanpreetsingh2370 2 жыл бұрын
@@agileprogramming7463 and now it's 95k
@Amit.01
@Amit.01 10 ай бұрын
@@jobanpreetsingh2370 and now it's 153k
@antonios-m4291
@antonios-m4291 2 жыл бұрын
class Solution { public: int searchInsert(vector& nums, int target) { auto it = std::find(nums.begin(), nums.end(), target); if(it == nums.end()) it = std::find_if(nums.begin(), nums.end(), [&](int num){ return num > target; }); return it - nums.begin(); } }; using C++ find algorithms
@hackytech7494
@hackytech7494 4 жыл бұрын
This was the best explanation for the solution as well as for binary search algorithm. Thank you so much
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@md_aaqil8027
@md_aaqil8027 4 жыл бұрын
Java Solution class Solution { public int searchInsert(int[] nums, int target) { int s=0; int e=nums.length; while(s
@martandkaushik7468
@martandkaushik7468 Жыл бұрын
thanks bro
@techdose4u
@techdose4u Жыл бұрын
Welcome :)
@yitingg7942
@yitingg7942 3 жыл бұрын
Thank you for the explanation. I understand the concept which is fairly simple. But I get so confused every time if I should use while(left < right) or while (left
@techdose4u
@techdose4u 3 жыл бұрын
Binary search is pretty hard you know. You need to try out the possible cases yourself. You can't really be sure all variants of problem 😅
@yitingg7942
@yitingg7942 3 жыл бұрын
@@techdose4u So I do need to try out all the possible cases? There is not magic formula that can handle almost everything and I just write it with my eyes closed ? lol I thought everybody is saying it's so easy.
@harshitmalhotra8627
@harshitmalhotra8627 2 жыл бұрын
Read the leetcode card of binary search. It has great explanation on what you are seeking.
@asanitian6218
@asanitian6218 Жыл бұрын
Why mid=low+(high-low)/2 ,we can take it as mid=(low+high)/2
@evgeni-nabokov
@evgeni-nabokov 4 жыл бұрын
Rust has this function: (match nums.binary_search(&target) { Ok(i) => i, Err(i) => i, }) as i32
@techdose4u
@techdose4u 4 жыл бұрын
👍
@seal0118
@seal0118 4 жыл бұрын
i completely understand, but why does the correct index has to be "l" or "start" instead of "end" or "h", why is "l" always the correct index ? does it have to do with the nature of the array itself ?
@akshatbhandari7237
@akshatbhandari7237 2 жыл бұрын
perfect
@ManishaKumari-wi6kg
@ManishaKumari-wi6kg 4 жыл бұрын
Sir can u make a video on sort element based on frequency just I am not able to understand this after viewing code on gfg it used map, pair, vector......
@techdose4u
@techdose4u 4 жыл бұрын
I have already used this technique in many of my videos. But I will still make it in future. I used to have this problem for a long time when I was a beginner myself 😅 So I can understand it well.I will add it to todo list.
@ManishaKumari-wi6kg
@ManishaKumari-wi6kg 4 жыл бұрын
@@techdose4u ok
@twbouji7580
@twbouji7580 2 жыл бұрын
Hello, thank you for the video! Why mid = (l+h)/2 isn't enough? my guess is that by writing l+(h-l)/2 we reduce the boundary of the array that we already visited.
@arjunrai8126
@arjunrai8126 2 жыл бұрын
The thing is that h and l being integers h+l can sometimes exceed the integer limit which is 2power 32 -1. So to avoid that extreme case of overflow we generally use h + (h-l)/2 which is the equivalent of (h+l)/2
@shreyamatade409
@shreyamatade409 2 жыл бұрын
Sometimes while coding on leetcode i get an error like "non void function doesn't return a value for all control paths". Can anyone tell me why does this happen?
@techdose4u
@techdose4u 2 жыл бұрын
You need to have return statement outside of all if-else etc statements.
@shreyamatade409
@shreyamatade409 2 жыл бұрын
@@techdose4u thanks a lot
@techtransformers7464
@techtransformers7464 4 жыл бұрын
can you please make a video on 763. partition labels - leet code
@techdose4u
@techdose4u 4 жыл бұрын
Once the challenges are over I can :)
@pratikshah9219
@pratikshah9219 2 жыл бұрын
this solution does not give a correct ans to [1,3] 3
@murtaza4989726
@murtaza4989726 2 жыл бұрын
Why is no one interested in understanding why is 'low' always pointing to the index where the missing target should be inserted? Thanks alot for the explanation but it feels like feeding fish to the hungry and not actually teaching the hungry to catch fish.
@techdose4u
@techdose4u 2 жыл бұрын
👍🏼
@bhupeshbhatt4849
@bhupeshbhatt4849 2 жыл бұрын
Yes brother, I was searching for the same , I was returning max(low, end) and this is actually working. I want to know why returning "low" is working
@vinaychowdary7693
@vinaychowdary7693 4 жыл бұрын
Can u make videos on concepts of data structures and algorithms
@techdose4u
@techdose4u 4 жыл бұрын
Can you please elaborate. I will continue with only Data structure after this June challenge.
@vinaychowdary7693
@vinaychowdary7693 4 жыл бұрын
@@techdose4u in this session mainly concentration on coding pblms, before going to coding we should know the concepts of data structures, algorithms and dynamic programming
@safalyaghoshroy2405
@safalyaghoshroy2405 4 жыл бұрын
First viewer😁😁😁
@techdose4u
@techdose4u 4 жыл бұрын
😁
@tanishqgupta1071
@tanishqgupta1071 4 жыл бұрын
Nice explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@anitasrivastava983
@anitasrivastava983 4 жыл бұрын
Hello bhaiya! Is competitive programming necessary for interviews as there are some people who say that interviews 'won't be cracked' without CP , or leetcode is sufficient?
@arunraju9705
@arunraju9705 4 жыл бұрын
Yes it is necessary, interview paradigm is constantly changing, today's methods are not used for tomorrow, why not just focus on something that you are very uncomfortable at ?
@techdose4u
@techdose4u 4 жыл бұрын
CP is good to improve your problem solving ability and keep you thinking like a programmer. When a new problem comes then CP guys have higher chance of solving it. But current placement scenario for most companies (apart from a handful like Codenation), you will be asked from questions on geeksforgeeks and leetcode. So if you focus on just leetcode itself, you will be very good and can crack any company interview. Many think that CP is the way, but CP is a way to improve problem solving ability but Leetcode is also very good alternative which is more placement oriented. Most of the CP concepts will never be asked in most of the placements. So, my advice is, do CP if you LOVE it and can give your good amount of time for it otherwise there is no point in doing CP for some time. You can improve on interview preparation in just a couple of months but CP requires much more time and dedication. So, it varies from person to person. But if you have an year and less then obviously go for Leetcode. I hope I answered your question.
@Ck-ir8ts
@Ck-ir8ts 4 жыл бұрын
@@techdose4u you are doing great, people need mentors like you.
@anitasrivastava983
@anitasrivastava983 4 жыл бұрын
Thank you bhaiya!!! You are doing a fantastic job, your videos are gonna blow up soon or later.
@farheenfirdous6530
@farheenfirdous6530 3 жыл бұрын
@@techdose4u thanks for the answer
@ROHITKUMAR-pn4gj
@ROHITKUMAR-pn4gj Жыл бұрын
My question is why low index is alway going to correct position whats the logic behind this If anybody can help that would be a great😊
@learnenjoyandgrow6601
@learnenjoyandgrow6601 4 жыл бұрын
Loved it
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@MGtvMusic
@MGtvMusic 3 жыл бұрын
Solution in C int searchInsert(int* nums, int numsSize, int target){ int *Iter = nums; int *End = nums + numsSize; for (int I = 0; Iter != End; I++, Iter++) { if (*Iter >= target) { return I; } } return numsSize; }
@caminante4222
@caminante4222 3 жыл бұрын
Why is l always at the correct position?
@sameer23377
@sameer23377 Жыл бұрын
Thanks bade bhai ,thoda sa hindi bhi bol diya kare🙂
@techdose4u
@techdose4u Жыл бұрын
Can't do it :)
@aaqibhamdule73
@aaqibhamdule73 2 жыл бұрын
This solution runs the fastest : public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length-1; int result = -1; if(nums.length==1){ if(nums[0]>=target){ return 0; }else{ return 1; } } while(end >= start){ int midPoint = (start+end)/2; if(end - start == 1){ if(nums[start]>=target){ result = start; } else if(nums[start]target){ result = start+1; }else if(nums[end]==target){ result = end; }else{ result = end + 1; } break; } if(nums[midPoint]==target){ result = midPoint; break; }else if(nums[midPoint]>target){ end = midPoint; }else { start = midPoint; } result = midPoint; } return result; }
@ImranAhmed-wd5tr
@ImranAhmed-wd5tr 4 жыл бұрын
Please share the software name that you use to write using pen tablet?
@HimanshuSingh-ob9qo
@HimanshuSingh-ob9qo 3 жыл бұрын
👍
@techdose4u
@techdose4u 3 жыл бұрын
👍
@Rizz_trader
@Rizz_trader 9 ай бұрын
so confusing for me as a beginner
@khokamohsin9307
@khokamohsin9307 3 жыл бұрын
Can anyone give the c source file code for this program?
@kyeiiih4422
@kyeiiih4422 Жыл бұрын
Great explanations. I have solved the same problem using 3 languages Java: class Solution { public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length - 1; //traverse through the array while (start nums[mid]) { start = mid + 1; } else if (target < nums[mid]) { end = mid - 1; } else { return mid; } } //return insert position return start; } } JavaScript: var searchInsert = function(nums, target) { let start = 0; let end = nums.length - 1; while(start nums[mid]) { start = mid + 1; } else if (target < nums[mid]) { end = mid - 1; } else { return mid; } } return start; }; Python 3: class Solution: def searchInsert(self, nums: List[int], target: int) -> int: l, r = 0, len(nums) - 1 while l nums[mid]: l = mid + 1 if target < nums[mid]: r = mid - 1 if target == nums[mid]: return mid return l Hope it helps someone
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 840 М.
Blue Food VS Red Food Emoji Mukbang
00:33
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 32 МЛН
Люблю детей 💕💕💕🥰 #aminkavitaminka #aminokka #miminka #дети
00:24
Аминка Витаминка
Рет қаралды 1,2 МЛН
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 88 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 370 М.
Single element in a sorted array | Leetcode #540
8:26
Techdose
Рет қаралды 54 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 462 М.
Easy Google Coding Interview With Ben Awad
28:00
Clément Mihailescu
Рет қаралды 1 МЛН
Search Insert Position - Binary Search - Leetcode 35 - Python
13:42
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 647 М.
Search Insert Position | Leetcode 35
15:30
Technosage
Рет қаралды 9 М.
Winning LeetCode Weekly Contest 198
40:27
William Lin
Рет қаралды 112 М.
Blue Food VS Red Food Emoji Mukbang
00:33
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 32 МЛН