#SDESheetChallenge@striver_79@takeUforward_
a lil lil late due to fever but here i am
so in today's episode of sde sheet s1 e14
1. max ones
whenever u find a problem related to subarray pray to sliding window god
since it was a pretty straightforward one i dint think of the brute for or O(n^2)
2 remove dublicates
using a hashset is the first instinct more like muscle memory
but when we optimise it since it is sorted it is easy to do using two pointers
3. TRAPPING RAIN WATER
keep a record of left max right max and total water becomes min of either -height[start]
Brazil's midfield is actually wasteful the fullbacks are mannequins ....casemiro needs to be subbed (jamie was kindda right) ... paqueta somehow compensated at the second quarter ....maybe danilo coming on
#FIFAWC2026#Brazil
#SDESheetChallenge@striver_79@takeUforward_
1. palindrome
since singly linkedlists dont have a prev pointer we cant use two pointers except if we reverse the second half to achieve something similar
2. find the starting of a loop
use fast and slow pointers where they meet slow goes to head and both traverse as slow to meet at the desired location
3. flatten ll
next points to the next list.
child points to a sorted linked list below.
The final flattened list should be sorted using only child pointers.
#SDESheetChallenge@striver_79@takeUforward_
1 . detect loop
loop until fast reaches null if nll doesnt exist fast and slow are bound to meet each other
2. intersection of y ll
pretty straightforward fast and slow pointers and if null shift on each others heads
3. reverse ll in k groups
exactly what the ques says.... no catch ur trick
#SDESheetChallenge@striver_79@takeUforward_
1.removing node in O(1){vague question ngl}
copy all the value the value from the next node to current and join to the next next node
2 . delete nth node from back
first appraoch would be to walk L steps count L-n and count and delete but the god of linkedlists the tortoise and hare saves us …
let the hare walk n steps and then both walk the remaining and the tortoise stands on L-n node
3.add two numbers as linekdlist
make two pointers on each linkedlist heads and keep on adding until the null value keep handiling the carryover and also make sure the end carry gets counted as i did this mistake
#SDESheetChallenge@striver_79@takeUforward_
life went from continous memory blocks to random pointers ... in short linkedlists from arrays
1. reverse a ll
recursive approach very straightforward
2. find the middle
tortoise hare method let the hare run two steps and tortoise just one at the end hare is always double of how many tortoise walked
3. merge two sorted list
kindda two pointer in linkedlists but the dummy node is the catch from where we make the chain
#SDESheetChallenge@striver_79@takeUforward_
life was quite easy no divide and conquer approaches no dp simple hashing and sliding window
Longest subarray with sum k
if there were only positives it would have been a classic sliding window problem
but since it contains negatives as well
for optimal approach use a pref sum map and find the corresponding other pref sum partner
2.longest substring without repeating chars
brute would be to use a set and two loops optimal would be to use two pointes and sliding window trim down the repeating one and create a new substring everytime we hit a repeating candidate
count subarrays with given xor
very similar to problem 1
#SDESheetChallenge@striver_79@takeUforward_
a bit late to the party due to codeforces div 2
1.two sum
O(n^2) a naive approach to solve it
or sort and use two pointer approach
optimal would be to traverse and find the two sum partner of that number in the map if exists add the indexes to answer vector if not add the number to the map
2. 4 sum
sort the array … use two loops one for left and one for right ..brute and better are basically shit approaches
3.longest consecutive sequence
a very natural approach would be to sort the array and check for potential sequences
the optimal one is quite smart .. use a set and check for potential starting points if it is check for next terms if not move on in life
#SDESheetChallenge@striver_79@takeUforward_
late to the party but todays questions felt like something an interviewer will ask just to reject you XD
anyways ....not gonna write the approach and my though process today as i had to watch the video solutions to get to the optimal answer
#SDESheetChallenge@takeUforward_@striver_79
1.majority number
simple brute force approach would be to solve this in O(n^2) a better would be to hash it but the optimal comes from voting algo where if a number appears more than n/2 times the contribution of that number to the count variable if increased when found and decreased when not would be positive
2. search in 2d matrix
brute force to search every element a better and intuitively my first approach was to determine which row the element might exist and consider it as a different array and apply binary search to that particular row
but the optimal solution considers the entire matrix as a flattened 2d array where we apply bs to get the target
3, pow(x,n)
brute force can be to multiply the number as many times as it wants
for optimal use the idea of making a sqaure recursively to get an even power and in case of odd multiply the number once again
#SDESheetChallenge@takeUforward_@striver_79
1.find the duplicate number(u cannot come up with the optimal approach if u see this ques for the first time)
one naive approach to solve this in O(nlogn) would be to sort and check every adjacent index for repeating values but the interviewer will not be happy
so the optimal approach (had to see the editorial ngl)
use the gods of linkedlist problem solving.. fast and slow pointers.... since every number lie in the range 1 to n two number would point on the same index.. and then visualise it as finding the start of loop in a linkedlist
2. finding a and b the repeating number and the missing one
first intuition is to hash this occurances can be noted down and boom interviewer will not be happy
optimal - make equations the actual sum - expected sum = X(repeating) - Y(missing) same when squared…now the x y x - y /2 gives a ; simultaneously a - val1 gives b ;
3. coutning inversions (most mindfuck question of this challenge until now)(star marked to revise again)
in case of a brute approach we just use a n^2 nested loop and count the inversions
optimal approach uses a merge sort method …basically a type of divide and conquer having two pointers and iteratively keep comparong them on the required conditions and merging halves
#SDESheetChallenge@striver_79@takeUforward_
day 3
1. merging sorted lists
a very brute approach would have been to use two pointer make a third array start iterating copying smaller or larger elements then copying it to the nums1 while we can also do the other way of specifying larger elements to end of nums1 using two pointers..
2.merge intervals
sort them and check if the extremes overlap if they do take the max
3.rotate array by 90
transpose and reverse every row
#SDESheetChallenge@striver_79@takeUforward_
DAY-2
brute force - O(n^2) check all subarrays and get rejected
optimal - whenever the sum gets less than 0 make sum 0 and also before that check max condition
Buy and sell stock
brute - O(n^2) bekaar
try to sell on minimum and sell when profit is max
Sort 0s 1s and 2s
use any sorting algo would fetch us at best O(nlog n)
so we use a three pointer method which we call dutch national flag algo