🚀 Day 16 – DSA Daily Series Today’s Problem: Two Sum II – Input Array Is Sorted (LeetCode 167) Today’s problem was simple yet really interesting — especially because of how efficiently it can be solved using the two-pointer technique. 🧠 Problem Given a sorted array, find two numbers such that they add up to a target value and return their indices (1-based). Example: Input: [2,7,11,15], target = 9 Output: [1,2] 💡 Approach Instead of using extra space like a hashmap, I used: • Two pointers — one at the start and one at the end • If sum is too large → move right pointer left • If sum is too small → move left pointer right • Stop when target is found Clean and efficient ⚡ ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🔎 Key Learning When the array is already sorted, always think of two pointers before anything else — it saves both time and space. Solved it today and it felt really smooth to implement! 💯 Continuing to stay consistent and improve step by step 🚀 #DSA #LeetCode #Python #TwoPointers #CodingJourney #ProblemSolving
Two Sum II - Sorted Array Solution
More Relevant Posts
-
🚀 Day 11 of Consistency | #75DaysLeetCodeChallenge 🧠 Today’s Problem : Two Sum II – Input Array Is Sorted (#167) 💡 Key Learning: This problem highlights the power of the two-pointer technique on a sorted array, helping reduce time complexity efficiently compared to brute force. ⚡ Approach: Use two pointers → left (l) at start & right (r) at end → If sum == target → return indices If sum < target → move l++ If sum > target → move r-- 🧠 Why this works: Takes advantage of sorted array Reduces complexity → O(n) No extra space required → O(1) 🔥 Result : ✔️ Runtime: 0 ms (Beats 100%) 📈 Mastering patterns like two pointers is key to cracking medium & hard problems. Consistency is compounding. Keep going. 💪 #Day11 #LeetCode #DSA #CodingJourney #100DaysOfCode #Python #TwoPointers #Consistency
To view or add a comment, sign in
-
-
🚀 Day 36 – LeetCode Journey Today’s problem: String to Integer (atoi) ✔️ Handled leading spaces and signs (+/-) ✔️ Processed numeric characters step by step ✔️ Managed overflow conditions within 32-bit integer range 💡 Key Insight: Carefully handling edge cases (like spaces, signs, and overflow) is just as important as the core logic. Small conditions can make a big difference in correctness. This problem strengthened my understanding of string parsing, edge cases, and boundary conditions. Learning to write robust and reliable code every day 💪🔥 #LeetCode #Day36 #Strings #EdgeCases #Python #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 39/100 – LeetCode Challenge Today’s problem: 406. Queue Reconstruction by Height This problem focuses on applying a Greedy approach with sorting to reconstruct a queue based on height and positional constraints. 🔍 Key Insight: Sort people by height in descending order and k value in ascending order, then insert each person at their respective index. 💡 What I learned: Importance of sorting strategy in greedy problems How insertion at a specific index can maintain constraints Thinking from the perspective of "who affects whom" (taller vs shorter) 🧠 Approach: Sort the array → (-height, k) Insert each person at index k 💻 Code (Python): class Solution: def reconstructQueue(self, people): people.sort(key=lambda x: (-x[0], x[1])) queue = [] for p in people: queue.insert(p[1], p) return queue ⏱️ Time Complexity: O(n²) Consistency is the key — showing up every day and improving step by step. #Day39 #LeetCode #100DaysOfCode #DSA #Python #CodingChallenge #GreedyAlgorithm #SoftwareDevelopment
To view or add a comment, sign in
-
-
LeetCode 23: Merge k sorted list: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Approach: Simple and straightforward, iterate through each of the linked-list in the given list, push each node's value into a priority queue using heapq module, create a new linked-list using the elements of priority queue. Time complexity: O(n logn) where n is the total number of values. Space complexity: O(n) #Python #LeetCode #DSA #CompetitiveProgramming #DataStructures #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
Topic 10/100 🚀 🧠 Topic 10 — Partial Functions What if you could pre-fill some arguments of a function and reuse it later? 🤯 👉 What is it? Partial functions allow you to fix a few arguments of a function and generate a new function with fewer parameters. 👉 Use Case: Used in real-world applications for: Pre-configuring functions Simplifying repeated function calls Building reusable utilities 👉 Why it’s Helpful: Reduces repetition Makes code cleaner Improves readability 💻 Example: from functools import partial def multiply(x, y): return x * y double = partial(multiply, 2) print(double(5)) # Output: 10 🧠 What’s happening here? We fixed the value of x = 2, creating a new function (double) that only needs one argument. ⚡ Pro Tip: Use partial functions when you find yourself passing the same arguments repeatedly. 💬 Follow this series for more Topics #Python #BackendDevelopment #100TopicOfCode #SoftwareEngineering #LearnInPublic
To view or add a comment, sign in
-
-
Day 3 / 100 🚀 Solved “Reverse Integer” — a problem that looks simple but actually tests how carefully you handle edge cases. At first, reversing digits feels straightforward. But the real challenge is handling 32-bit overflow without using extra space. 💡 Key learning: Before updating the result, always check if multiplying by 10 will exceed the allowed range. Core idea: rev * 10 + digit must stay within [-2³¹, 2³¹ - 1] Highlights: • Time Complexity: O(log n) • Space Complexity: O(1) • Correctly handles negative numbers and overflow This problem reinforced a critical habit: Don’t just make the logic work — validate boundary conditions. #100DaysOfCode #LeetCode #DSA #Python #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
Most FastAPI codebases look clean at first glance. Until you try to change something. I’ve noticed a pattern — a lot of complexity doesn’t come from the problem itself, but from where the logic lives. When routes start handling more than just request/response, things get harder to reason about. Lately, I’ve been keeping one constraint: Routes should stay thin. They handle the HTTP layer. All business logic moves to services. It’s a small shift, but it changes a lot: 1) Clearer separation of concerns 2) Easier testing 3) Fewer side effects when making changes Also started appreciating dependency injection more. Not as a framework feature, but as a way to keep things decoupled and predictable. Nothing groundbreaking here. But in a time where a lot of code is being generated faster than it’s being designed, maintainability comes down to how consistently we apply these basics — not whether we know them. Curious how others approach structuring FastAPI projects at scale. #FastAPI #BackendDevelopment #CleanCode #SoftwareEngineering #Python
To view or add a comment, sign in
-
📌 Problem: Increasing Triplet Subsequence 💡 Approach: Traverse the array while maintaining two variables: first and second, representing the smallest and second smallest values found so far. If the current number is smaller than first, update first Else if it’s smaller than second, update second If a number is greater than both, we’ve found an increasing triplet This ensures an optimal single-pass solution. ⚙️ Key Insight: Track only two values instead of checking all triplets Greedy + optimization approach reduces complexity significantly ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 📚 What I learned: Greedy strategy for subsequence problems Reducing brute-force O(n³) to optimal linear solution #LeetCode #DSA #Algorithms #Coding #ProblemSolving #Python #Greedy #InterviewPreparation #CodingJourney
To view or add a comment, sign in
-
𝗧𝗵𝗿𝗲𝗲 𝗻𝘂𝗺𝗯𝗲𝗿𝘀. 𝗢𝗻𝗲 𝗽𝗮𝘀𝘀. 𝗢𝗻𝗲 𝗴𝗵𝗼𝘀𝘁 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲. Day 28 of #1000DaysOfLearning Today's problem: 𝗦𝗼𝗿𝘁𝗲𝗱 𝗦𝘂𝗯𝘀𝗲𝗾𝘂𝗲𝗻𝗰𝗲 𝗼𝗳 𝗦𝗶𝘇𝗲 𝟯 (GFG Medium) Find arr[i] < arr[j] < arr[k] where i < j < k — in a single pass. Track the smallest first, then the smallest second after it. The moment you see x > second, you've got your triplet. The tricky part: if first gets updated 𝗮𝗳𝘁𝗲𝗿 second is already set, it's no longer positionally before second. So you keep a prevFirst — 𝘁𝗵𝗲 𝘃𝗮𝗹𝘂𝗲 first 𝗵𝗲𝗹𝗱 𝘄𝗵𝗲𝗻 second 𝘄𝗮𝘀 𝗹𝗮𝘀𝘁 𝘂𝗽𝗱𝗮𝘁𝗲𝗱. Even if first moves to something smaller later, prevFirst still holds the element that actually appeared before second in the array. That's what makes the returned triplet 𝗽𝗼𝘀𝗶𝘁𝗶𝗼𝗻𝗮𝗹𝗹𝘆 𝘃𝗮𝗹𝗶𝗱, not just value-valid. 𝗢(𝗻) time. 𝗢(𝟭) space. #DSA #Python #DataScience #GFG #1000DaysOfLearning #LearningInPublic
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development