THREE SUM: What is 3-Sum? 3-Sum = Fix one number + solve 2-Sum on the rest. We've already know 2-Sum with two pointers. So 3-Sum is just reusing that skill. Step-by-Step thinking (Human way): Step 1: Sort the array why? Enables two pointer makes duplicate skipping easy Step 2: Fix one number(loop) imagine you say: "Let me lock this number, and find two others that cancel it." if the fixed number is x, then; we need two numbers with sum = -x Step 3: Apply Two pointer for the remaining part —left pointer > next index after fixed —right pointer > end of array Now this becomes a 2-Sum problem. In the next, I'll share the Visual Example to understand clearly.👇👇
👍1🌚1
Visual Example with best clarifications: Read the👇👇👇You understand clearly about 3-Sum in easy way👌
I HOPE YOU UNDERSTAND THE LOGIC OF 3-SUM😄
Array: [-4, -1, 0, 1, 3, 5] #already sorted(ordered) Step 1: Fix the number -4 😎Fix means take as a constant - 4 or u can say freeze -4,Don't move it 👉🏽3-Sum goal is: a + b + c = 0 substitute -4 =a: -4 + b + c = 0 => b + c = 4 :) Target for the remaining two numbers sum = 4 Step 2: Choose where b and c can come from: We mustn't reuse -4 , so we search to the right👇 [-4 | -1, 0, 1, 3, 5]
↑ ↑ ↑ ↑ ↑ now pointer is doing this: left = -1, right = 5. then -1 + 5 = 4✅ :- b = -1 & c = 5 Step 4: Build the triplet: and don't forget the fixed number😀-4 => -4 + (-1) + 5 = 0 👏 Valid triplet found: (-4, -1, 5) Step 5: What happen next? After then finding a valid pair: -move both pointer: means the left to the right and the right to the left at the same time. & If there's duplicate skip New pointers: left = 0, right = 3. 0 + 3 !=4(target value). So since Sum < target move the left to the right. 1 + 3 = 4 👏Again a valid triplet found: (-4, 1, 3) keep in mind: if left and right crossed each others stop the condition. (left = 3 then right = 1)CROSSED CONDITIONS⚠️ FINAL RESULT FOR FIXED -4 Valid triplet: (-4, -1, 5) & (-4, 1, 3) Generally: 3-Sum = Freeze one number → solve a 2-sum problem beside it.
I HOPE YOU UNDERSTAND THE LOGIC OF 3-SUM😄
Implementation of 3-Sum:
👉🏽the algorithm said hey, for 0, 1, 3, 5 I don't have any idea, since these haven't valid triplet, so I don't talk about the😁
Don't forget about two pointer👌 In simple the program is talks about fixing (keep constant each elements if it's true and find sum of other two values that equal to target. Therefore, fixed value -4 and -1. here algorithm say this's VALID TRIPLET but for others since they're not equal to their target value.
👉🏽the algorithm said hey, for 0, 1, 3, 5 I don't have any idea, since these haven't valid triplet, so I don't talk about the😁
Guys I hate class😏
Just we went to class but no teacher.
He said am coming but nothing happend we waited him about 30 minute.
Now am in my dorm.
..what do u think🤔
Just we went to class but no teacher.
He said am coming but nothing happend we waited him about 30 minute.
Now am in my dorm.
..what do u think🤔
if he said now I'm in class but there's no anyone. I need all of u to back to class early😔
😢1
4️⃣ SUM: On Loading... That's similar with 3️⃣ SUM. Just only on small d/c.🕒
Keep point: 4️⃣ SUM Four Sum = Fix two numbers + solve two-Sum with two pointers. a + b + c + d = target This's only new idea but the other is as it's.
😁2❤1
https://leetcode.com/problems/4sum/ I'm stucked on this questions and now am trying to do my best. After then, I'll share for u my work progress
LeetCode
4Sum - LeetCode
Can you solve this real interview question? 4Sum - Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
* 0 <= a, b, c, d < n
* a, b, c, and d are distinct.
* nums[a] + nums[b]…
* 0 <= a, b, c, d < n
* a, b, c, and d are distinct.
* nums[a] + nums[b]…
❤1
After many challenges😯 here I've solved 4Sum - LeetCode problem😁 https://leetcode.com/problems/4sum/submissions/1934060254/ U can try it so💪🏻
LeetCode
4Sum - LeetCode
Can you solve this real interview question? 4Sum - Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
* 0 <= a, b, c, d < n
* a, b, c, and d are distinct.
* nums[a] + nums[b]…
* 0 <= a, b, c, d < n
* a, b, c, and d are distinct.
* nums[a] + nums[b]…
🎉1
Guys, I thought as long as we learn DSA. It's good to learn Python libraries.
Therefore we need your vote🗳
Therefore we need your vote🗳
Anonymous Quiz
82%
Yes, we'll start Python libraries
6%
No, we can't learn them together
12%
I don't have any idea
And then guys good night
tommorow there issome gift for u🎁
I will share in this channel...stay tuned and Stay focus on. U don't have to lose this opportunity👌
tommorow there is
I will share in this channel...stay tuned and Stay focus on. U don't have to lose this opportunity👌
🥰2
Now am gonna post so pls don't lose this opportunity. Keep in mind: After 5 minute it gonna expired so try ur best before that. About 5 minute left......
Am gonna start NUMPY. What do u think. Your vote is needed.
Anonymous Quiz
75%
Yup, go a head
19%
No, I don't care
6%
Amn't understand