Тёмный

Product of Array Except Self (LeetCode 238) | Full solution with visuals | Study Algorithms 

Nikhil Lohia
Подписаться 43 тыс.
Просмотров 45 тыс.
50% 1

To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
A very important and an interesting problem that will assess your logical thinking skills. From an array find the product of all other elements except self. Watch this video to understand why the most obvious way to approach this problem will give you a wrong answer, and how can you come up with an efficient solution. Follow along with animations, visual aids and a dry-run of code in JAVA
Chapters:
00:00 - Intro
00:50 - Problem Statement and Test cases
03:05 - The most obvious way fails
06:33 - Thinking out of the box
10:55 - Dry-run of Code
12:57 - Final Thoughts
Actual problem on LeetCode: leetcode.com/problems/product...
📚 Links to topics I talk about in the video:
LeetCode Problems: • Leetcode Solutions
Problems on Arrays: • Arrays
What is Big O?: • Big O Notation Simplif...
Other medium difficulty problems: • Medium Problems
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/3sJm8Wl
Favorite book to understand algorithms: amzn.to/4848xJH
Favorite book for data structures: amzn.to/3P96YBv
Get started for interview preparation: amzn.to/44Nn5du
🎥 My Recording Gear:
Recording Light: amzn.to/3PdsViT
Microphone: amzn.to/3Exv83x
Recording Camera: amzn.to/3PwyN8e
Tablet to sketch and draw: amzn.to/3ZdKVy7
Sketching Tool: amzn.to/45XJEgY
Laptop to edit videos: amzn.to/460ofDu
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview

Опубликовано:

 

4 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 109   
@Sai-fn4lq
@Sai-fn4lq 10 месяцев назад
Next level teaching sir !! You are literally a hidden gem!!
@nikoo28
@nikoo28 9 месяцев назад
glad you feel that way
@sivaramansankar7463
@sivaramansankar7463 Год назад
thanks for the lucid explanation , you're a hidden gem :) , you deserve way more subscribers
@nataliechan8535
@nataliechan8535 8 месяцев назад
Your explanation is so clear! I like how you draw out everything to explain it! Thank you
@nikoo28
@nikoo28 7 месяцев назад
The drawing really helps a lot 😄
@saisree04
@saisree04 2 месяца назад
Excellent explanation. I am so glad that I came across your YT channel
@user-qn9um8ud1t
@user-qn9um8ud1t Месяц назад
Thank you so much for explaining each line in a very simple manner.
@gameplays8457
@gameplays8457 19 дней назад
Really good explanation. Clearly understood the concept. Thank you!
@meetshah545
@meetshah545 4 месяца назад
Very nice explanation, I really search your channel for Ds-algo questions . Thanks sir for teaching free.
@Aryan-oc1fq
@Aryan-oc1fq 9 месяцев назад
recently found your videos, and im loving the explanations. thank you !
@nikoo28
@nikoo28 9 месяцев назад
hope they are of help to you
@SMK1455
@SMK1455 22 дня назад
Thank you a lot for such great explanation of the problems!
@Max-tq1ig
@Max-tq1ig 11 месяцев назад
Totally understood. Thanks a lot.
@abhishekkeshri3974
@abhishekkeshri3974 Год назад
like always your explanation and video quality is awesome. i will try to share your videos as much as possible .
@nikoo28
@nikoo28 Год назад
So nice of you
@thenormalpen1900
@thenormalpen1900 25 дней назад
Wow, that is such a clear soln, thanks!
@kamehameha38
@kamehameha38 11 месяцев назад
Nicely explained. Thank you very much
@LongNguyen-pe5uo
@LongNguyen-pe5uo Год назад
Good video, very easy to understand.
@anishasaini380
@anishasaini380 5 месяцев назад
Sir Best Explanation!!
@kirthananambiar8873
@kirthananambiar8873 Год назад
Thank you so much sir. Explained very well.
@abhigoku2022
@abhigoku2022 6 месяцев назад
Hi Sir, Eveytime I am searching for a leeetcode problem, I add your name in the suffix hoping you have done a video on it. I have understood each and every video that I have watched. Please do solve all the problems, that will be very helpful for people like me.
@Piyush-me9nu
@Piyush-me9nu 7 месяцев назад
Amazing explanation!
@gayatriyadav4007
@gayatriyadav4007 День назад
Very well explained!
@kenilkanani16
@kenilkanani16 13 дней назад
Awesome explanation 🎉
@user-rg1dr1ou6f
@user-rg1dr1ou6f 8 месяцев назад
Fall in love with your explanation ❤❤❤❤❤❤❤❤
@vivekjaiswal4391
@vivekjaiswal4391 Год назад
Great teacher 👏
@sianwa11
@sianwa11 8 месяцев назад
Recently found your channel, whenever I don't understand neetcode solutions I come here. Thank you sir!
@nikoo28
@nikoo28 8 месяцев назад
@neetcode is also an amazing channel :)
@simiiv5021
@simiiv5021 5 месяцев назад
Same!
@abhishekanand8574
@abhishekanand8574 5 месяцев назад
THANK YOU , I TRIED VERY HARD BUT COULD NOT SOLVED. YOU CLEARED IT IN A MIN .❤❤❤❤❤❤
@piyushanand3135
@piyushanand3135 5 месяцев назад
Amazing Explaination
@c4rtel
@c4rtel Год назад
Very nice explanation.
@Infinitycoder01
@Infinitycoder01 Год назад
I like your way of explaining the problem , with this i learnt to solve problems and as well as the way of explaining the code to others. It helped me a lot in my interview.Thank you so much.
@nikoo28
@nikoo28 Год назад
I try to approach the problem in a way as you would do in an interview. Glad it helps :)
@AravindKumar-lj7kx
@AravindKumar-lj7kx Год назад
Thanks a lot....you are great....
@sameerdas2468
@sameerdas2468 3 месяца назад
Man you are god 😭🙏 your explanation is outstanding 👏 you earned a subscriber today ❤️.
@covert000
@covert000 2 дня назад
Exellent Explaination sir
@ass2412
@ass2412 Год назад
I was stuck in neetcode and i thought it would be impossible. After seeing your video, i understood clearly
@nikoo28
@nikoo28 11 месяцев назад
thanks for your feedback and support :)
@mustafahassan9383
@mustafahassan9383 10 месяцев назад
OMG , same happened to me 🤣🤣🤣🤣
@anjuverma285
@anjuverma285 10 месяцев назад
​@@mustafahassan9383😊
@devdattrupapara1890
@devdattrupapara1890 7 месяцев назад
same happened to me returns..
@shreehari2589
@shreehari2589 6 месяцев назад
Same
@divyasingh6757
@divyasingh6757 Год назад
Here our space complexity can be reduced to O(1)..overall nice explanation
@yxs7
@yxs7 11 месяцев назад
u are the best! thx! ❤👏✨
@nikoo28
@nikoo28 10 месяцев назад
you are the best
@rishabhsaini7836
@rishabhsaini7836 Год назад
Nicest explanation ❤❤
@nenuanenenuane6645
@nenuanenenuane6645 Год назад
Thank you bro❤❤
@TalhaTabrez7
@TalhaTabrez7 6 месяцев назад
thank you
@zahrabt6705
@zahrabt6705 10 месяцев назад
you explained it very well. thanks
@nikoo28
@nikoo28 9 месяцев назад
So nice of you
@MdArif-pc9bz
@MdArif-pc9bz Месяц назад
Thank you
@papayaw2920
@papayaw2920 10 месяцев назад
Keep going Nikhil, I was stuck and I was about to give up too. I feel taught today
@nikoo28
@nikoo28 9 месяцев назад
glad that was helpful
@infinite639
@infinite639 Год назад
i have shared your tutorials to my friends
@nikoo28
@nikoo28 Год назад
Thank you so so much 😄
@kaushit
@kaushit 4 месяца назад
I usually don't comment but this kind of video I see my hands automatically goes in comment section for comment. Thanks Nikhil you even demystified how dividing will give results, most of people don't know how dividing is giving the answer and thanks for explain postfix and prefix in detail.
@infinite639
@infinite639 Год назад
Thanks bhai leetcode ke videos banate raho mere liye or sabke liye Love you bro
@nikoo28
@nikoo28 Год назад
Thanks for the love
@bun_bun17
@bun_bun17 Год назад
you are great :)
@clasherrony6526
@clasherrony6526 3 месяца назад
amazinggggggg
@mikedelta658
@mikedelta658 7 месяцев назад
Excellent explanation.
@nikoo28
@nikoo28 7 месяцев назад
Glad you liked it
@ashishgoyal6256
@ashishgoyal6256 Год назад
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
@benmyths
@benmyths 13 дней назад
thanks
@SingwithSuhas
@SingwithSuhas 7 месяцев назад
superb
@subee128
@subee128 7 месяцев назад
Thanks
@user-ph5ek8tg5l
@user-ph5ek8tg5l Месяц назад
Leetcode has mentioned to try O(1) space solution. You should add one more part in the video for such optimizations.
@ankit_yr_senpai
@ankit_yr_senpai Месяц назад
great
@sainathkadam2213
@sainathkadam2213 Год назад
You r osm 🔥
@nikoo28
@nikoo28 Год назад
Thank you so much 😀
@vlogsaryan2540
@vlogsaryan2540 Год назад
Bhaiya why would you used num[i-1] and [n+1] in both the left and right Loop pls explain
@nikoo28
@nikoo28 Год назад
please try to look at the explanation of the left and right array once again, we have to skip those elements. if you are still confused..let me know
@jayeshjain4540
@jayeshjain4540 6 месяцев назад
Sir the question also says You must write an algorithm that runs in O(n) time and without using the division operation.
@nikoo28
@nikoo28 5 месяцев назад
I don't use the division operator.
@jagdishbehera5010
@jagdishbehera5010 Год назад
The way you teach it feels like there is nothing in DSA
@nikoo28
@nikoo28 Год назад
Thanks for the appreciation…I really try to simplify things as much as possible.
@vikrantt22
@vikrantt22 17 часов назад
I used the same approach but instead of having left and right array I create a function that calculates prefix product and another function to calculate suffix product and at the end multiply prefix product and suffix product to obtain final answer but this answer exceeds the time limit. Thank you for simple explanation of same logic from another POV.
@nikoo28
@nikoo28 15 часов назад
that approach works too
@vikrantt22
@vikrantt22 7 часов назад
@@nikoo28 Yeahh but for that approach time limit exceeds.
@robertnant1264
@robertnant1264 4 месяца назад
Thank you for the video. But somehow I don't know how it's possible to find the solution without knowing this trick (prefix, suffix). What am I missing in my reasoning?
@nikoo28
@nikoo28 3 месяца назад
This is a kind pf problem which comes with practice. The idea is that once you solve a problem like this, it will open your mind and you will try to apply the same pattern to future problems.
@thakurabhishtpratapsingh1686
@thakurabhishtpratapsingh1686 4 месяца назад
i was getting time limit exceeded for one of the test cases.
@nikoo28
@nikoo28 3 месяца назад
Check out the code given in video description
@truecoding8659
@truecoding8659 Год назад
Here is my solution in python, i used to for loop and it givs the same answer. arr=[2,1,3,4] arr1=[] r=1 for i in range(len(arr)): pro=arr[i] for j in arr: if j is not pro: r=r*j arr1.append(r) r=1 print(arr1)
@nikoo28
@nikoo28 Год назад
but the time complexity of your solution is O(n^2)...because of the nested loops. You can try to improve that.
@avanishraj386
@avanishraj386 Год назад
@@nikoo28 this will fail when arr = [0,0]
@nikoo28
@nikoo28 Год назад
@@avanishraj386 did you try the code given on github? It will work with your sample test case. What error are you getting?
@avanishraj386
@avanishraj386 Год назад
@@nikoo28 I am not talking about your code, I have replied the above comment of "True Coding".
@benvan3721
@benvan3721 8 дней назад
What each line of code does matters and very impotant to understand too. I wish you explain that rather than reading it.
@nikoo28
@nikoo28 7 дней назад
Thanks for your feedback. But my friend, my channel primarily focuses on problem solving rather than writing the code. Because the code languages will keep on keep on changing with time. Problem solving skills will always remain the same. I would always encourage you to write the code on your own to become a better developer. If you are looking to learn the code line by line, there are a lot of videos who do a better job than me. :) Plus you have so many AI technologies to explain the code.
@siddharthmehta4223
@siddharthmehta4223 Год назад
it gives the following error for the array you created to store right side value. error: cannot find symbol [in __Driver__.java] int[] ret = new Solution().productExceptSelf(param_1); ^ symbol: class Solution location: class __DriverSolution__
@nikoo28
@nikoo28 Год назад
Check the github link in the description below, for a working code :)
@Dineshkumar_dk
@Dineshkumar_dk 9 месяцев назад
but we have to solve this with O(1) space
@nikoo28
@nikoo28 9 месяцев назад
let me come back to you.
@prakhargarg4166
@prakhargarg4166 7 месяцев назад
Question says no extra space, then why left and right arrays
@nikoo28
@nikoo28 7 месяцев назад
That part got added to the question recently. It wasn't there when I created the video. Will post an update soon on it.
@yuvamamidipalli7127
@yuvamamidipalli7127 2 месяца назад
Sir i didn't understand the approach that you have explained about 2^32 and 10^12>2^32
@nikoo28
@nikoo28 2 месяца назад
What part are you getting confused with?
@user-om9lc1qt9t
@user-om9lc1qt9t 10 месяцев назад
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: # Check if the input list is empty if not nums: return [] length_of_nums = len(nums) # Initialize the result list with 1s result = [1] * length_of_nums product = 1 # Calculate the product of all elements to the left of each element for index in range(length_of_nums): result[index] = product # Store the product of elements to the left product *= nums[index] # Update the product for the next element product = 1 # Calculate the product of all elements to the right of each element for index in range(length_of_nums - 1, -1, -1): result[index] *= product # Multiply by the product of elements to the right product *= nums[index] # Update the product for the next element return result
@user-kd5gp4ds4p
@user-kd5gp4ds4p 10 месяцев назад
i think this is good approach then your code please review it solving in O(n) time and o(1) space complexity class Solution { public: vector productExceptSelf(vector& nums) { int zero = 0; int tmul = 1; for(auto num : nums){ if(num) tmul *= num; else{ if(zero){ tmul = 0; break; } zero++; } } for(auto &num : nums){ if(num) num = zero>0 ? 0 : tmul/num; else num = tmul; } return nums; } };
@58harshverma57
@58harshverma57 Год назад
brackets are not allowed here; to declare an array, place the brackets after the name int[] left = new int[nums.size()]; ~~ ^ [] this error is coming
@nikoo28
@nikoo28 Год назад
Please recheck your code. The declaration is correct. Also check my complete code in the github link available in video description.
@58harshverma57
@58harshverma57 Год назад
@@nikoo28 yes sir I have corrected it ..I haven't made left and right as vectors! Thanks for the solution ! 😄
@ahamedsha9688
@ahamedsha9688 7 месяцев назад
I am dumb
@danishmehmood6110
@danishmehmood6110 22 дня назад
bro this problem has a better and more simple solution step 1 - just calculate the product of the whole array in this example the answer would be 1*2*3*4=24 step 2 - in the second step to calculate result [ i ] just divide the calculated result by nums [ i ] e.g to calculate result [ 0 ] = 24/1 =24 result [ 1 ] =24/2 =12 and so on
@nikoo28
@nikoo28 21 день назад
does this method get accepted? I believe you will hit limits when trying to multiply all the numbers.
Далее
Product of Array Except Self - Leetcode 238 - Python
11:54
Most Common Concepts for Coding Interviews
6:08
Просмотров 295 тыс.
8 patterns to solve 80% Leetcode problems
7:30
Просмотров 286 тыс.