🚀Day 2/100 – LeetCode Journey Today’s problem: Two Sum II (Sorted Array) 💡Approach (Two Pointer Method) Since the array is sorted, I used two pointers: One at the start (i) One at the end (j) 👉Logic: If nums[i] + nums[j] == target → return answer ✅ If sum is greater → move j-- If sum is smaller → move i++ In this problem, we return 1-based index → so answer is (i + 1, j + 1) ⚡Time Complexity: O(n) → single pass using two pointers 🧠 Space Complexity: O(1) → no extra space used 🙌 Thanks to RAVI KUMAR Sir for guidance! . . #100DaysOfCode #LeetCode #DSA #Java
Two Sum II (Sorted Array) Solution
More Relevant Posts
-
Day 64 — LeetCode Progress (Java) Problem: Remove Element Required: Given an array nums and a value val, remove all occurrences of val in-place and return the new length of the array. Idea: Use a two-pointer approach to overwrite unwanted elements while maintaining the order of remaining elements. Approach: Maintain a pointer k to track the position of valid elements. Traverse the array: If the current element is not equal to val, place it at index k Increment k All valid elements are moved to the front of the array. Return k as the new length. Time Complexity: O(n) Space Complexity: O(1) #LeetCode #DSA #Java #TwoPointers #Arrays #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 65 — LeetCode Progress (Java) Problem: Find All Numbers Disappeared in an Array Required: Given an array of size n containing numbers in the range [1, n], return all the numbers that are missing from the array. Idea: Compare the expected range [1…n] with the actual elements to identify missing values. Approach: Initialize a set containing all numbers from 1 to n. Traverse the array: Remove each element from the set The remaining elements in the set are the missing numbers. Time Complexity: O(n) Space Complexity: O(n) #LeetCode #DSA #Java #HashSet #Arrays #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 91/100 – 𝐌𝐚𝐱 𝐂𝐨𝐧𝐬𝐞𝐜𝐮𝐭𝐢𝐯𝐞 𝐎𝐧𝐞𝐬 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Keeping track of a running count helps efficiently solve problems involving continuous sequences. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Traverse the array Count consecutive 1s Reset count when 0 appears Track maximum count 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? We only care about continuous 1s, so resetting on 0 ensures we start counting a new sequence. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Initialize count = 0 and max = 0 Loop through array: If 1 → increment count Else → reset count Update max at each step ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #Day91 #100DaysOfCode #Java #DSA #LeetCode #Arrays #CodingJourney
To view or add a comment, sign in
-
-
Day 66 — LeetCode Progress (Java) Problem: Find the Difference of Two Arrays Required: Given two integer arrays, return: Elements present in nums1 but not in nums2 Elements present in nums2 but not in nums1 Idea: Use sets to remove duplicates and quickly check membership. Approach: Convert both arrays into sets Iterate over nums1 set: Add elements not present in nums2 set to result1 Iterate over nums2 set: Add elements not present in nums1 set to result2 Return both lists Time Complexity: O(n + m) Space Complexity: O(n + m) #LeetCode #DSA #Java #HashSet #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀𝐃𝐚𝐲 88/100 – 𝐀𝐝𝐝 𝐁𝐢𝐧𝐚𝐫𝐲 Today’s problem was Add Binary — a great exercise to understand how addition works at the binary level. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Just like decimal addition, binary addition also uses a carry, but with base 2. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Traverse both strings from right to left Add digits along with carry Append result and update carry 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? Binary addition rules: 0 + 0 = 0 1 + 0 = 1 1 + 1 = 0 (carry 1) ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Use two pointers (end of both strings) Add digits + carry Append (sum % 2) Update carry (sum / 2) Reverse the result ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦𝐚𝐱(𝐧, 𝐦)) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) #Day88 #100DaysOfCode #Java #DSA #LeetCode #Strings #CodingJourney
To view or add a comment, sign in
-
-
Many linked list problems are about pointer manipulation and maintaining order. 🚀 Day 113/365 — DSA Challenge Solved: Merge Two Sorted Lists Problem idea: We are given two sorted linked lists, and we need to merge them into a single sorted list. Efficient approach: Use a two-pointer technique to compare elements and build the merged list. Steps: 1. Create a dummy node to simplify result construction 2. Compare current nodes of both lists 3. Attach the smaller node to the result 4. Move the corresponding pointer forward 5. Once one list ends, attach the remaining part of the other list This ensures the final list remains sorted. ⏱ Time: O(n + m) 📦 Space: O(1) Day 113/365 complete. 💻 252 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LinkedList #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Day 75 - Count Complete Tree Nodes Finding the total number of nodes in a complete binary tree using recursion. Approach: • Traverse the tree recursively • Count left subtree • Count right subtree • Add 1 for current node Time Complexity: O(n) Space Complexity: O(h) #Day75 #LeetCode #Java #CodingPractice #TechJourney #DSA #BinaryTree
To view or add a comment, sign in
-
-
#Day85 of #100DaysOfCode Focused on practicing array-based problems with an emphasis on logic and efficiency. Worked on: * Moving zeros to the end of an array * Checking if an array is a palindrome * Calculating the sum of even elements #Java #Arrays #100DaysOfCode
To view or add a comment, sign in
-
-
Day 65 - DSA Practice Solved LeetCode 237 – Delete Node in a Linked List. The catch is you don’t get the head or the previous node, so the usual deletion approach doesn’t work. Instead, copy the value from the next node and link to the node after it. This effectively removes the given node. Simple problem, but a good reminder to think differently when constraints block the obvious approach. #DSA #LeetCode #Java
To view or add a comment, sign in
-
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