🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
Two Sum II - Sorted Array Solution on LeetCode
More Relevant Posts
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
𝗬𝗼𝘂 𝗵𝗮𝘃𝗲 𝗯𝗲𝗲𝗻 𝘂𝘀𝗶𝗻𝗴 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗿𝗼𝗻𝗴. It is not just for sorted arrays. The real definition — "Eliminate HALF the search space with each decision." That one shift in thinking unlocks 20+ LeetCode problems. Swipe to see the template, live code, and 7 problems you can now solve with one pattern. 👇 Save this. 🔖 #DSA #BinarySearch #Java #LeetCode #CodingInterview #DebugWithPurpose
To view or add a comment, sign in
-
#day332 of #1001daysofcode problem statement (0226): Invert Binary Tree The idea is to recursively swap the left and right child of every node in the tree. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of tree) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 | Median of Two Sorted Arrays Intuition: We are given two sorted arrays and need to find the median of the combined numbers. Since both arrays are already sorted, we can merge them in sorted order. Once we have the merged array, finding the median becomes simple. If the total number of elements is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. Approach: Use two pointers to traverse both arrays. Compare the elements and insert the smaller one into a new array. Continue this process until all elements are merged. Finally, calculate the median based on the length of the merged array. Complexity: Time Complexity: O(n + m) Space Complexity: O(n + m) #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 35 – Plus One Worked on a problem where a large integer is represented as an array of digits, and the task was to increment the number by one. Key Learnings: Traversing arrays from right to left to simulate digit addition Handling carry when a digit becomes 10 Managing edge cases where all digits are 9 by creating a new array #DSA #Java #Arrays #ProblemSolving #CodingPractice #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 25 of #100DaysOfCode Solved 24. Swap Nodes in Pairs on LeetCode 🔗🔄 🧠 Key insight: Swapping nodes in a linked list doesn’t require extra memory—careful pointer updates are enough to reverse every adjacent pair. ⚙️ Approach: 🔹Traverse the list two nodes at a time 🔹Reverse links between each adjacent pair 🔹Maintain connections with the previous pair to keep the list intact 🔹Handle edge cases for odd-length lists ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
#day333 of #1001daysofcode problem statement (0257): Binary Tree Paths 💡 Approach: Used DFS recursion to explore all root-to-leaf paths in the binary tree. While traversing, I kept building the path string. When a leaf node is reached, the complete path is added to the result list. Example path format: 1->2->5 ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of the tree) Consistency in solving one problem every day 📈 #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 41 - Find Pivot Index Solved using a prefix sum approach. First computed the total sum of the array, then traversed once while maintaining a running left sum. For each index, the right sum is calculated using total - left - nums[i]. If both sums match, that index is the pivot. Time Complexity: O(n) #Day41 #LeetCode #Java #PrefixSum #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 36/100 – LeetCode Challenge 🚀 Problem: 3Sum Closest Approach: Sorted the array Fixed one element and applied the two-pointer technique Tracked the closest sum by comparing absolute differences Returned immediately if an exact match was found Time Complexity: O(n²) Space Complexity: O(1) Key takeaway: Many optimization problems are variations of classic patterns. Understanding 3Sum deeply makes solving 3Sum Closest straightforward. #LeetCode #100DaysOfCode #DSA #Java #TwoPointers #ProblemSolving #InterviewPrep
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