this is the best channel for finding best algorithm to problem. not even @neetcode can explain easy and efficient algorithm which this channel does
@shivanggupta54214 жыл бұрын
I was stuck on this for a while and saw 2 more videos before this. But you explained it really well. Thanks
@techdose4u4 жыл бұрын
Welcome
@RajVeerEkSoch3 жыл бұрын
I agree
@abdulrehmanamer42528 ай бұрын
totally agreed
@goutamkundu63923 жыл бұрын
Your channel is gold. Just 1 small optimisation can be not using the map but using an array to contain the sum. We can take an array of length 2*n+1 where n=length of given nums array. We can add n to each sum i.e., we can initialise our sum variable with n rather than 0. Next all the things are same. The logic is quite obvious. If the given nums array contains all 1's, then max sum possible is n and we are adding n to it, so total n+n=2n. That's why taking an array for hash of length 2*n+1. If the given nums array contains all 0's, then sum will become -n+n=0. Hence, we can accomodate all values in our 2*n+1 hash array.
@techdose4u3 жыл бұрын
👍🏼
@aadil42368 ай бұрын
Thank you mate. for the edge case when we get zero as current sum, we can just add "0: -1" in the hashmap in the start and don't have to worry about it in the code to check that edge case.
@gourabbanerjee41524 жыл бұрын
The best explanation for this problem! Good Job!
@techdose4u4 жыл бұрын
Thanks :)
@PremPal-uy4nm Жыл бұрын
For the people who heard "Contiguous Subarray" first time. Contiguous Subarray means any part of array where no elements are skipped. [1,2,3,4,5] has [3,4,5] as contiguous subarray but [1,4,5] is not Contiguous Subarray. We can think kind of continuous subarray.
@lemax29804 жыл бұрын
Ur channel is a gem bro.I feel lucky to be a subscriber😅 .All the best bro..🙌🙌
@techdose4u4 жыл бұрын
Thanks :)
@abhijitbandyopadhyay48213 ай бұрын
Good Explanation. Clear & concise.
@qilinzhang69584 жыл бұрын
Thanks very much, I completely understand your explanation, great job.
@techdose4u4 жыл бұрын
Thanks :)
@aswathsrimarir25014 жыл бұрын
You are doing a good job...following all your videos. ..keep uploading.
@techdose4u4 жыл бұрын
Thanks :)
@pranayraj79384 жыл бұрын
Nice Explanation !!...I was waiting for ur video.
@techdose4u4 жыл бұрын
Thanks :)
@juuzousuzuya695 ай бұрын
I was stuck on this but you explained it well. Im just thinking no way I would know this solution unless Ive seen it :D
@aparnamane11644 жыл бұрын
I really love your videos. The explanation is quite crisp and clear. Keep posting. Thankyou.
@techdose4u4 жыл бұрын
Welcome :)
@sarvesh67852 жыл бұрын
Python3 code: class Solution: def findMaxLength(self, nums: List[int]) -> int: dic = {} sum = count = 0 for i in range(len(nums)): sum += 1 if nums[i] == 1 else -1 if sum == 0: count = max(count,i + 1) elif(sum in dic): count = max(count,i - dic[sum]) else: dic[sum] = i return count
@najimali324 жыл бұрын
This question is good & you explain it the nice way. I also have a similar approach, but your code is more compact.
@techdose4u4 жыл бұрын
Thanks :)
@amandeepnokhwal29773 жыл бұрын
@@techdose4u Aap bhagvaan ho kya?
@venkateshthirunagiri854 жыл бұрын
how will you get this type of IDEAS man? like replace 0 with -1 then compute the problem........this is mind-blowing. love your channel dude and thanks for making videos " SURYA PRATAP KAHAR " :)
@techdose4u4 жыл бұрын
Actually I had seen such trickery in some other problems. So whatever is seen, quickly comes into mind 😅
@bisheshwardevsharma32434 ай бұрын
Thank you. It was a good explanation
@ryanchen4672 жыл бұрын
The is the best explanation I've seen
@ganeshjaggineni40975 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@meet26373 жыл бұрын
This channel is gold.
@techdose4u3 жыл бұрын
❤️
@anjalisinha33773 жыл бұрын
Code in Java -- @TechDose class Solution { public int findMaxLength(int[] nums) { HashMap hm = new HashMap(); int sum =0; int max=0; int temp=0; for (int i=0; i
@techdose4u3 жыл бұрын
👍🏼
@Yash-uk8ib4 жыл бұрын
sir, i appreciate ur method. It's nice. Maybe, i have another approach, plzz tell me whether it works or not. If not, then plz explain why? APPROACH: compute the count of zeroes and ones from the array and find min(count_of_zero, count_of_one)*2. This expression will give you maximum length of subarray. If any of these counts is/are 0, then no subarray is possible which meets the given criteria.
@techdose4u4 жыл бұрын
I had thought this as my first idea and you did too 😅 This won't work because counting is not taking contiguous element property into consideration. It may skip some elements in between and say the answer. This can work for subsequence where you can skip any element but this won't work for subarray. Take the example of 0 1 1 1 0. You method will give 4 but answer is only 2. I hope you got it :)
@Yash-uk8ib4 жыл бұрын
@@techdose4u ohhkk... i got it
@techdose4u4 жыл бұрын
:)
@stankafan66884 жыл бұрын
hahaha bhaiya i too thought same for the first time 😅
@soumavabanerjee59252 жыл бұрын
I tried this one too at first and got WA XD
@AnkitRaj-jn8ew4 жыл бұрын
Instead of having the condition sum == 0, we can initialize the map mymap[0] = -1 and when we encounter 0, i+1 will be added to longest_subarray.
@techdose4u4 жыл бұрын
We could have done map[0] = 1 and then iterated.
@AnkitRaj-jn8ew4 жыл бұрын
@@techdose4u shouldn't it be map[0] = -1 and not map[0] = 1 because this way, when sum is 0, it will be i - (-1) = i + 1
@techdose4u4 жыл бұрын
The idea is to add. If you take minus sign then take -1. This time around I solved with 1 😅 It all depends on how you manipulate the calculation.
@AnkitRaj-jn8ew4 жыл бұрын
True, I said -1 in agreement to the equation in the video "longest_subarray = i - mymap[sum]" where using +1 could make it wrong.
@sumitSharma-ed5mi3 жыл бұрын
Very good explanation and its very helpful
@techdose4u3 жыл бұрын
Thanks :)
@Giri14_188 ай бұрын
Great explanation ........Thank you sir
@dhanashreegodase44452 жыл бұрын
Keep posting. Thankyou.
@techdose4u2 жыл бұрын
Welcome 😄
@subham-raj4 жыл бұрын
*Closer to prefix sums*
@aviligondagowtham11534 жыл бұрын
Sir,could u pls explain the c code based on whatever u will say the problem so that it will be better .. u r the best tutor I never seen really
@techdose4u4 жыл бұрын
If you are doing coding in C then will recommend you to switch to C++ as soon as you can. It will be a cakewalk to switch. C doesn't have standard libraries. You will be at ease. Believe me.
@aviligondagowtham11534 жыл бұрын
As per our placement the companies will give more preference to c languge so tht I'm learning
@techdose4u4 жыл бұрын
I heard this the first time. Can you name some companies who prefer C over CPP?
@akshitajha12092 жыл бұрын
Yes plzz switch to C, it's involves tedious work even for basic things, change it ASAP, I also made this mistake so highly recommend u! Well this comment is 1 year ago M sorry but to whomsoever it may concern!
@pravyn3502 ай бұрын
@@techdose4u better move to python 🐍 sir
@akhilesh592 жыл бұрын
Thanks for the explanation :)
@techdose4u2 жыл бұрын
Welcome 😄
@ahmedfouzan2 жыл бұрын
Thanks for the clear explanation
@techdose4u2 жыл бұрын
Welcome 😊
@mohamedhesham60082 жыл бұрын
That's really amazing explanation
@techdose4u2 жыл бұрын
Thanks
@halfaddercircuit3 жыл бұрын
Your approach is great. I used map as well but very nasty. Great explanation.
@ganakkathuria48932 жыл бұрын
Nice explanation
@techdose4u2 жыл бұрын
Thanks 😊
@chrisjurgens8043 Жыл бұрын
Great explanation, helped me solve for sure!
@akshayaamar98252 жыл бұрын
Very well explained. Thank you.
@techdose4u2 жыл бұрын
Welcome 😄
@priyamani43494 жыл бұрын
Great work.
@techdose4u4 жыл бұрын
Thanks :)
@kumarswamy35543 жыл бұрын
very clear explanation. Thank you so much.
@techdose4u3 жыл бұрын
Welcome
@saurabh.gupta228 ай бұрын
Great Explanation ❤
@abhishekkumarjhariya13402 жыл бұрын
Great explanation as always.
@techdose4u2 жыл бұрын
Thanks :)
@suryajanardhan8 ай бұрын
very good explanation
@praveenchouhan63884 жыл бұрын
brilliant work!!!!!!!!!!!!!!!!!!!!
@techdose4u4 жыл бұрын
Thanks :)
@tanishqaggarwal1197 Жыл бұрын
Nice approach
@techdose4u Жыл бұрын
:)
@ajaywadhwa33984 жыл бұрын
Really Impressive. Helped a lot !!
@techdose4u4 жыл бұрын
Welcome
@kushgupta11874 жыл бұрын
Very well explained
@debashisdas65934 жыл бұрын
Nice explanation !!!
@techdose4u4 жыл бұрын
Thanks
@priyankadey33373 жыл бұрын
Awesome explanation!
@techdose4u3 жыл бұрын
Thanks :)
@ishmeetbindra54284 жыл бұрын
Nicely Explained!
@techdose4u4 жыл бұрын
Thanks :)
@sujiths91992 жыл бұрын
I really appreciate your thought process
@audioplatform62993 жыл бұрын
All good... very clear explanation as always in your channel... But, is it fair for the companies to expect someone to think of a solution for this kind of hard problem and code it in 45 minutes?
@techdose4u3 жыл бұрын
Generally you won't be asked hard problems.
@Learner0104 жыл бұрын
the best explanation!!
@techdose4u4 жыл бұрын
Thanks :)
@ryan.aquino4 жыл бұрын
python data = nums hm = {} res = 0 ctr = 0 for i in range(len(data)): if data[i] == 0: ctr -= 1 else: ctr += 1 if ctr == 0 and i+1 > res: res = i+1 if ctr not in hm: hm[ctr] = i else: val = i-hm[ctr] if val > res: res = val return res
@shubhamgarg71984 жыл бұрын
How you build your such good concepts?? To produce optimal solutions
@techdose4u4 жыл бұрын
First I think atleast one solution and try solving. If it works then I try optimizing different steps for that problem. This method works for me.
@jaydeepmahajan65984 жыл бұрын
I have seen this video last month still i need to watch this video agin , i am just getting demotivated for this , why ideas are not coming in my mind ?
@techdose4u4 жыл бұрын
You should fully understand a problem and then solve similar problems yourself in order to remeber it.
@mayankchauhan66803 жыл бұрын
how does a solution strike the mind? Is it just about intuition who just a few smart people happen to have, or you get to learn a particular pattern over a long period of time by solving problems over and over?
@techdose4u3 жыл бұрын
Your last guess is right. Everyone is smart in common matters but once we talk about a specific skill then you need to train your brain over a period of time.
@nidhisingh25_4 жыл бұрын
Had a doubt!! What if the array starts with 0 this code will not work then. For example : [0,0,1,0,1,1,1,1,0]. Can you please explain? If sum becomes -1 or -2, then it will be stored in map which might be wrong I guess? Correct me if I am wrong...
@techdose4u4 жыл бұрын
I guess u r thinking how can we have negative index, right? Well, we are not actually accesing by index but we are storing index. We are storing both key and value as pair in map. Key can have any value. So don't worry, it will work.
@damodaranm30444 жыл бұрын
bro . you have done a excepional job .youre better than nick white kelvin naugton . pls dont go fast while u teach . thanks
@techdose4u4 жыл бұрын
Thanks bro :)
@damodaranm30444 жыл бұрын
@@techdose4u go slow while u teach bro . because we are like small kids in problem solving . and the way u teach is amazing keep it up i appreciate it
@syed79964 жыл бұрын
Thanks for the video, but how you decided to use hash map? How do you get that intuition. why not variables?. Please make a series of videos on how to get that intuition. It requires practice as suggested by many but still curious to know
@techdose4u4 жыл бұрын
Will make it in near future for sure :)
@yashshreeshinde43943 жыл бұрын
Thank you!!!!
@techdose4u3 жыл бұрын
Welcome :)
@ssaha77142 жыл бұрын
I am just wondering what is wrong if we take a loop, 2 variables and then the lower count multiplied by 2 is the answer. Am I missing something from the question. private int maxLength(int[] arr){ int zeroCount =0; int oneCount =0; for(int I=0;I
@manjunathp94664 жыл бұрын
l=list(map(int,input().split())) dic={} sum=maxarray=0 for i in range(len(l)): sum+=(-1) if l[i]==0 else 1 if sum==0: maxarray=i+1 if sum not in dic.keys(): dic[sum]=i else: maxarray=max(maxarray,i-dic[sum]) print(maxarray)
@techdose4u4 жыл бұрын
👍
@parekshamanchanda80834 жыл бұрын
Can be done with stack as well
@techdose4u4 жыл бұрын
Code please :)
@amanvijayvargiya34684 жыл бұрын
thank you so much ..
@techdose4u4 жыл бұрын
Welcome :)
@locusoflearning4 жыл бұрын
Thanku sir.
@techdose4u4 жыл бұрын
Welcome :)
@muskanmangal89384 жыл бұрын
It helped!
@techdose4u4 жыл бұрын
:)
@rahulsaini2023 жыл бұрын
@TECH DOSE To check element is present in map or not. Sometimes we write mymap[sum] and sometime we write mymap.find(sum)!= mymap.end()... Please can you explain this? Thanks.
@techdose4u3 жыл бұрын
Mymap[sum] will return value stored at key Sum (provided the entry is already present in map). Find is used to check if there is any valid entry present with the given key Sum. So, 2nd one can be used to check. 1st one is used to retrieve value when you know it's present else you want to initialise a new value.
@rahulsaini2023 жыл бұрын
@@techdose4u Thanks for the response.
@stankafan66884 жыл бұрын
bhaiya can't we use stack. int findMaxLength(vector& nums) { int s,i,count=0,max=0; s=nums.size(); stackst; st.push(nums[0]); for(i=1;i
@ApexPlayground_divineАй бұрын
Thanks brother, explanation was so good , i could solve without waiting for code section.
@techdose4uАй бұрын
welcome :)
@akshitajha12092 жыл бұрын
I m loving the explanation but have a sense of guilt too that I was unable to solve it on my own. I tried and know about basic prefix sum but still loving this solution which I was unable to think of. What shall do? Is it fine?
@techdose4u2 жыл бұрын
It's fine. Just don't watch the code implementation and try yourself. If you can't do then see it :)
@rahulchaubey89884 жыл бұрын
👌👌👌👌👌👌
@techdose4u4 жыл бұрын
:)
@najimali324 жыл бұрын
I think when sum is zero is not handle correctly , i m getting wrong ans, Python sol: class Solution: def findMaxLength(self, nums: List[int]) -> int: n = len(nums) sum ,c_arr = 0,0 //hash map d = dict() d[0]=-1 for i in range(n): if nums[i]: sum+=1 else: sum-=1 // checking if the key is present in hashmap if sum in d: c_arr = max(c_arr,i-d[sum]) else: d[sum]= i return c_arr
@vinoltauro91604 жыл бұрын
can we use the sliding window technique ?
@akashmittal77954 жыл бұрын
can you explain what this line means? sum += nums[i]==0?-1:1;
@techdose4u4 жыл бұрын
It is Ternary operator. It means if mums[i] == 0 then select value -1 else select 1. After selection, we have given a shorthand addition operator to add the result to sum. So sum will be added by either -1 or 1 depending on the check I performed. I hope you got it :) It is easier than writting bunch of lines of if else statement to do it.
@mayankchauhan66803 жыл бұрын
if(nums[i] == 0) { sum = sum-1; } else sum = sum+1
@muskaangupta49204 жыл бұрын
Why did you compare mymap.find(sum) with mymap.end()? Can't be simply If(mymap.find(sum))
@techdose4u4 жыл бұрын
It is given to see if on searching the element you reached end of map or not. If you did, that means the element is not present. You can use map.count(sum)
@muskaangupta49204 жыл бұрын
Okay. Got it!!
@techdose4u4 жыл бұрын
👍
@subhadippatra79304 жыл бұрын
Will you keep all the videos free in future or you will move to a paid platform ?
@techdose4u4 жыл бұрын
Problems will be free. I will include donations in near future. If it goes well then everything will be free forever.
@ankitchaturvedi2941 Жыл бұрын
can we solve it without map
@DEEPAKYADAV-vb2ee4 жыл бұрын
Would you help me. How to find maximum GCD of the subarray. For eg. arr = { 2, 3,4,4,4} Here max subarray as welll as max GCD is ={4,4,4}. Help me what will be your efficient approach. @Tech Dose
@techdose4u4 жыл бұрын
What can be the size of subarray?
@DEEPAKYADAV-vb2ee4 жыл бұрын
@@techdose4u all the size of subarray >=2 of the given array
@DEEPAKYADAV-vb2ee4 жыл бұрын
@@techdose4u it is just same as the max sum of the subarray. But here instead of finding the sum, we have to find the max gcd .
@spetsnaz_24 жыл бұрын
@@DEEPAKYADAV-vb2ee bro remember - maximum GCD value will always be equal to the largest number present in the array. Let’s say that the largest number present in the array is X. Now, the task is to find the largest sub-array having all X.
@techdose4u4 жыл бұрын
Max gcd will always be of length 2. So find all possible subarray gcds of length 2. There will be N-1 such subarrays.
@baxtables3 жыл бұрын
is it a dp problem?
@spetsnaz_24 жыл бұрын
int findMaxLength(vector& nums) { // Fast I/O ios_base::sync_with_stdio(false); cin.tie(NULL); int n = nums.size(); if(n == 0 or n == 1) return 0; unordered_map mp; int res = 0, sum = 0; mp[0] = -1; //for handling first element as 0 so index is -1 default for(int i=0; i
@hemantranjan22972 жыл бұрын
2-0+1 = 2.
@hemantranjan22972 жыл бұрын
You corrected. Nice.
@sahilanand302 жыл бұрын
You just explained the solution But there is no intuition
@nuclearbaba1769 Жыл бұрын
can you give me solution for java
@lakshyavijh12494 жыл бұрын
Laga de bhai sum wala concept 0 ko -1 rakhne wala
@techdose4u4 жыл бұрын
😂
@lakshyavijh12494 жыл бұрын
@@techdose4u bhai placement h 🤣bta de kaise padu
@techdose4u4 жыл бұрын
Bhai target company btao...fir kuchh btate hain 😅
@lakshyavijh12494 жыл бұрын
@@techdose4u bhai flipkart ya amazon
@abhinavsingh86752 жыл бұрын
Naruto fan spotted
@BRBallin15 ай бұрын
I'm sorry but you were a bit hard to follow along
@gauthamsssomanna9388 Жыл бұрын
Java Solution Map sumMap = new HashMap(); int sum = 0; int longestSubArr = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i] == 0 ? -1 : 1; // make 0s as -1 and then add if (sum == 0) { longestSubArr = i + 1; // then we will put the index as the longestSubStr } else if (sumMap.get(sum) != null) { longestSubArr = Math.max(longestSubArr, i - sumMap.get(sum)); } else { sumMap.put(sum, i); } } return longestSubArr;