Shubham's corner

Software, AI, Optimisation, Learnings, Cooking and Traveling.

I use this space to share my learnings, experiments, technical and non-technical desicisions and everything else going with my life.

[Neetcode] Two Integer Sum II

[Neetcode] Two Integer Sum II

Basically you have a sorted array of integers and you need to find the index i, j which will sum up to a target. Approach 1 - Bruteforce The simplest solution is loop through all the integers combinations and find the indices. This is basically looping twice each number -> O(n^2), not very good.We can make the above one more optimal by starting the second loop after the current index. This will reduce our iterations by half, making it n(n-1)/2. This is also O(n^2).We can add one more optimization by pruning as soon as second loop sums to a number greater than target. As the array is sorted, any number after the number which resulted in sum greater than target will also result in a sum greater than the target. Worst case this will also be O(n^2).But we got a clue here, the clue is traversing in reverse order if we get a sum less than target then we no longer need to search a number before the index. Basically, if sum of 1st and last is less than the target, then good guess would be check the 2nd number and last number. And if 1st and lat is greater than the target, then a good guess would be check 1st and 2nd last. And so on. Approach 2 - 2 pointers Take 2 pointers, l at the start of the array and r at the last of the array. If the sum of numbers at the pointers is greater than target then check r-1 and if it is less than the target then check l+1. This will give us a solution which is O(n) as none of the numbers are being passed more than once.

Chicken without oil

Chicken without oil

Eating healthy food is a good habit. I live in a PG, and the food is quite good here, but I think it lacks fiber and protein. So, I decided to start cooking some food myself. I’m not a good cook, but I’m trying to learn better things. So I thought, maybe I can start documenting them here. And today, I cooked chicken without any oil. I’m trying to eat healthy, and that’s why I’m preventing oil from touching my food. Anyway, let’s get to the recipe.Recipe Marinate the chicken First, I bought 450 grams of chicken from Licious. These were chicken thighs. Just after bringing them to my room, I started the process of marinating the chicken. For marination, I used 100 grams of dahi, around 20 grams of ginger-garlic paste, half a tablespoon of salt, one tablespoon of red chilli powder, half a tablespoon of turmeric, and two tablespoons of chicken masala. Mix everything together, cover it, and leave it for around an hour. Then, I took one whole onion and sliced it vertically.Prepare the onions Generally, we use oil to fry onions, but that’s where my recipe is different. What I do is take 200 ml of water, heat my electric skillet at the highest setting, and put the onions into the boiling water. Leave them there and give them a quick stir every 3–4 minutes. Do this until the water has completely evaporated. The onions will become soft and slightly brownish.Cook chicken till they turn white Once the onions are a little brown and ready, add the marinated chicken. Start stirring the chicken for around 4–5 minutes. The indication that the chicken is ready to be covered is that it will become white on the outside.Cover the lid and firmly cook the chicken Now the process becomes easy. Bring the heat down to medium or low, cover it with the lid, and let the chicken cook for 30 minutes. Just make sure the chicken is not getting stuck at the bottom, so give it a quick stir every 4–5 minutes. After this 30-minute period, open the lid. If there is some liquid or gravy left and it is too watery, bring the skillet back to the highest temperature and boil down the extra gravy.You can eat the chicken as it is or have it with rotis or rice. I generally squeeze half a lemon over the chicken to make it taste fresh.

Vibe coded my blogging app

Vibe coded my blogging app

Just another late-nighter and the power of AI. I was looking for a VPS to host my blog, basically my admin page, so that I can access it anywhere and start writing my blogs wherever I can, mainly from my mobile. Then, an idea struck my head: why not get an app? Basically, I have the Git token, so I thought of getting an app that can access my GitHub and get this exact repo cloned on my system. And, like I always write blogs on my laptop, I can just use the same technologies to write blogs on my mobile. Then, I sat on my laptop, discussed things with Codex, and we came up with a plan to write an app that can connect to my GitHub, understand my blogging codebase, and create an app around that, matching my workflow. I can see my old posts, update, add, create whatever I want, and just write my blogs from mobile. The GitHub connector page This is where you just create a GitHub app and connect it to your app. Once connected, you will never need to do it again.The old blog posts View/edit your old blog posts and keep things updated.The writer page Write new articles/blogs just from the app and push it to GitHub. Once pushed the same CICD pipeline will make it go live. Easy Peasy.

Just started neetcode blind 150

Just started neetcode blind 150

Just started Neetcode blind 150. This time I'm using java as my tool. Navigating through same challenges and learning more. To be honest, coding on job and coding on coding platform are very different. On job, you use HashMaps and you are mostly done. And there are exceptions when you use DP or Trees. My case, I think I have used mostly all the general data structures and some advanced ones too. But anyways, back to coding and building hand memory again.

charAt vs toCharArray

charAt vs toCharArray

String as you know is immutable in java. When you try to manipulate a String object, Java creates a new String out of it. And object creation is expensive. There is a general rule, when you just want to read a character at any index use charAt and when you need to modify the string itself, like rearranging, sorting, creating sub string, breaking it into an array of characters using toCharArray is very cost efficient. toCharArray takes O(n) time for construction of array and O(n) space is also occupied.