POV you are in the early 2000's, here is my submission for @ChaiCodeHQ cohort, I thought the deadline was today and ended up submitting T-T, it still needs a lot of polish, it also has some songs for @piyushgarg_dev π, everyone pls try it out
What loneliness does to a man π₯ @piyushgarg_dev
In todays class learned:
- Loop engineering
- Harness engineering
- Different types of prompting
- Tools
and a lot of insights on AI industry #GenAIcohort2026
(6/6)
Example: arr = [2,3,4,5,6], k fixed at 6
2+5>6 β β 3+5, 4+5 also valid β +3 triangles instantly β right--
2+4>6 β β left++
One comparison confirms a whole range instead of checking every pair. That's the power of anchoring the largest value.
(5/6)
Fix index k (largest side), loop it backward from the end to index 2.
Set left = 0, right = k-1.
Check arr[left] + arr[right] > arr[k]:
β True β every pair between left and right also works (array is sorted, so sums only grow). That's (right - left) triangles at once. Move right--.
β False β sum too small, move left++.
(4/6)
Optimization #2: two pointers but backwards.
Most two-pointer problems go leftβright. This one fixes the largest side first and works inward. Here's why that matters π
#DepressedAndDSA#Day 3 of 30 days of DSA and mild despair
Did Leetcode 611 (valid triangle number) and leetcode 283 (move zeros)
Today's LeetCode problem taught me that sorting an array can turn an O(nΒ³) brute force into something that doesn't make me want to close my laptop.
Breaking down LeetCode 611 (Valid Triangle Number) π
(1/6) π§΅
(3/6)
Optimization #1: Sort the array first.
Once sorted, you only need to check a+b>c (where c is the largest of the three). The other two inequalities are automatically true once the array is in order
(2/6)
The core idea: Triangle Inequality Theorem
a+b>c, a+c>b, b+c>a
In plain words: the sum of any two sides must be greater than the third side, or you can't form a triangle.
(4/4)
Valid Palindrome:
This question can also be solved by two pointers, as we need to check if first letter/number = last letter/number or in other words left = right
we are going to solve this question by discarding all the negatives in the loop and that will tell us if left = right then we can do left++ and right--
ex = Race car
declare two pointers, left and right
then in the loop,
while left < right{
//discard spaces and non alphanumeric values
if(!alphanumeric(left value)) left++ and continue (skip this value)
if(!alphanumeric(right value)) right-- and continue (skip this value)
// case insensitive, if it doesn't pass this, then it is not a palindrome
if(lowercase(left value) != lowercase(right value)) return false
// if it passes all 3 conditionals above, that means left == right so we simply move them
left++
right--
}
return true;
Day 1 done. See you tomorrow, these were both the questions.
(3/4)
Two sum :
The problem statement is that given a sorted array we have to find two such values that add upto a certain number then return those indices,
for example, target = 19
arr = 2,5,8,11,15,19
as you can see 11 and 8 add upto 19 so answer will be indices 2 and 3
but that's you who can see and give the answer like that, we are supposed to make the computer solve it with some set of instructions
so let's go with two pointers approach:
first declare two variables left =0 and right = n-1
now we loop till left < right, because we would have scanned the whole array by the time left= right
then simply add the values present on those indices and check the sum with the target value,
if our sum > target we simply decrease right (as the array is sorted, so to get a lower sum we have to decrease right)
if sum < target, that means we need a bigger number so increase left
repeat this process until sum = target
And there you go, you solved two sum, ez right?
(2/4)
What is 2 pointers?
Basically these are just 2 integer variables you make to scan through either an array or string
say left =0 and right = n-1
then based on the question you check values located on these indices and either increase left++ or decrease right-- or sometimes both
#Day1 of 30 day #DepressedAndDSA series
check out https://t.co/GX4e9PYdxn for the beautiful visuals
feeling depressed these days, so I might just start DSA
and maybe DSA visual might just heal me
Started 2 pointers today, did two questions, leetcode 167 and 125
@Hiteshdotcom@nirudhuuu@piyushgarg_dev@ChaiCodeHQ
π§΅(1/4)