𝗗𝗮𝘆 𝟭𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Remove Element using the Two Pointer technique (in-place modification). ➤ Approach (O(n), O(1) space): • Use pointer i to track position of valid elements • Traverse array with pointer j • If nums[j] != val, copy it to nums[i] and increment i • Return i as the count of remaining elements No extra array. No shifting multiple times. Just overwrite unwanted values. ➤ Key Insight: Since order doesn’t matter after index k, we only care about keeping valid elements in the first part of the array. #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #Consistency
Remove Element with Two Pointer Technique
More Relevant Posts
-
🚀 Day 10 – Find the Closest Pair from Two Arrays Today’s problem: Find a pair (one element from each sorted array) whose sum is closest to x. 🧠 Key Idea Since both arrays are sorted, we can use the Two Pointer Technique instead of brute force. 🔹 Approach Start i = 0 (beginning of first array) Start j = m - 1 (end of second array) Compute sum Track minimum difference |sum - x| If sum > x → move j-- Else → move i++ #geekstreak60 #npci #Java #geekforgeeks
To view or add a comment, sign in
-
-
Day 30/75 — Remove Nth Node From End of List Today's problem focused on linked list manipulation using the two-pointer technique. Goal: Remove the nth node from the end of a singly linked list. Approach: • Use a dummy node to simplify edge cases • Move a fast pointer n + 1 steps ahead • Move both pointers together until fast reaches the end • The slow pointer will be just before the node to remove Then update: slow.next = slow.next.next Time Complexity: O(n) Space Complexity: O(1) A classic example of how the two-pointer technique simplifies linked list problems. 30/75 🚀 #Day30 #DSA #LinkedList #TwoPointers #Java #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 23 of #100DaysOfCode 🌱 Topic: Strings / Hashing / Sliding Window ✅ Problem Solved: LeetCode 1461 – Check If a String Contains All Binary Codes of Size K 🛠 Approach: Generated all substrings of length k using sliding window. Stored each substring inside a HashSet to track uniqueness. Compared set size with 2^k to check if all possible binary codes exist. Used bit manipulation concept (1 << k) to calculate total combinations. ⏱ Time Complexity: O(n * k) 📦 Space Complexity: O(2^k) #100DaysOfCode #Day23 #DSA #Strings #Hashing #BitManipulation #Java #LeetCode #ProblemSolving #CodingJourney #Consistency #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 31/75 — Sum of Two Integers (Without + or -) Today's problem was about performing addition using bit manipulation. Key idea: • XOR (^) gives sum without carry • AND (&) + left shift gives carry Algorithm: while (b != 0): carry = (a & b) << 1 a = a ^ b b = carry Repeat until carry becomes zero. Time Complexity: O(1) Space Complexity: O(1) This problem gave a deeper understanding of how addition works at the binary level. 31/75 🚀 #Day31 #DSA #BitManipulation #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
🔲 Day 69/100: Set Matrix Zeroes - Mark First, Set Later Day 69. Matrix problem. Can't set zeros while finding them. Two-pass solution. ✅ 📌 Problem: If element is 0, set its entire row and column to 0. 💡 Solution: Pass 1: Mark which rows/cols have 0s Pass 2: Set marked rows to 0 Pass 3: Set marked cols to 0 🎯 The Trap: If you set 0s immediately, you'll create new 0s and mess up the logic! Track first, modify later. 📊 Complexity: O(m×n) time, O(m+n) space Day 69. Matrix manipulation. 🔲 #100DaysOfCode #DSA #LeetCode #Day69 #Java #MatrixZeroes #TwoPass
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟮𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved 3Sum using Sorting + Two Pointer technique. ➤ Approach (O(n²), O(1) extra space apart from result): • First, sort the array • Fix one element i • Use two pointers (left, right) to find pairs such that --> nums[i] + nums[left] + nums[right] == 0 • Move pointers based on whether the sum is smaller or larger than zero • Store unique triplets ➤ Key Insight: Sorting simplifies the problem. Once sorted, the two-pointer technique efficiently avoids checking every combination. Brute force would be O(n³). Optimized approach reduces it to O(n²). #LeetCode #Java #DSA #TwoPointers #Sorting #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🚀 Day 30 / 100 | First Missing Positive -Intuition: The smallest missing positive will always be in range 1 to n+1. Place each number at its correct index (1 at index 0, 2 at index 1 so on). First index where value ≠ index+1 gives the missing number. -Approach : O(n) Find length of array n Traverse array and place each element at correct position -> index = value − 1 Ignore negative numbers, zero, and numbers > n After placement, traverse array again If arr[i] ≠ i+1 -> return i+1 If all elements are correct -> return n+1 -Complexity: Time Complexity : O(n) Space Complexity : O(1) #100DaysOfCode #Java #DSA #Array #LeetCode
To view or add a comment, sign in
-
-
🚀 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
-
-
🚀 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
To view or add a comment, sign in
-
-
🔢 Day 67/100: Convert to Hex - Bit Masking Day 67. Convert integer to hex. Labeled "EASY." Actually easy this time. ✅ 📌 Problem: Convert number to hexadecimal string. 💡 Solution: & 15 → Get last 4 bits (0-15) Map to hex char (0-9, a-f) >>> 4 → Shift right 4 bits Repeat, reverse result 🎯 The Key: num & 15 extracts last hex digit. num >>>= 4 moves to next digit. 📊 Complexity: O(1) - at most 8 hex digits Day 67. Bit manipulation ftw. 💪 #100DaysOfCode #DSA #LeetCode #Day67 #Java #HexConversion #BitManipulation
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