🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
Solved LeetCode #219 with Java and HashMap
More Relevant Posts
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Solved Base 7 in Java 🔢 Approach The challenge was to convert a given integer (num) to its base 7 string representation. Conversion Method The core of the solution lies in the standard algorithm for base conversion: repeated division and remainder collection. Handle Zero and Negatives: If the input num is 0, the result is immediately "0". I determined if the number is negative and stored this in a boolean flag, then proceeded with the absolute value of the number (num = Math.abs(num)) for the conversion logic. Conversion Loop: I used a while loop that continues as long as num > 0. In each iteration: The remainder when num is divided by 7 (num % 7) gives the next digit in base 7. This digit is appended to a StringBuilder. num is then updated by integer division by 7 (num /= 7). Final Result: Since the remainders are collected in reverse order, I called sb.reverse(). If the original number was negative, I prepended a hyphen (-) to the reversed string. Finally, I returned the result as a string. This simple and efficient implementation had a very fast runtime, beating 77.39% of submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #BaseConversion #ProblemSolving
To view or add a comment, sign in
-
-
🌟 Day 73 of 100 Days of Challenge 🌟 📌 LeetCode 220 — Contains Duplicate III Difficulty: 🟥 Hard | Language: Java ☕ Today’s problem was a tricky combination of index range and value range checks. The task was to determine if there exists a pair (i, j) such that: i ≠ j |i - j| ≤ indexDiff |nums[i] - nums[j]| ≤ valueDiff 🧠 Key Insight Instead of brute force (which is O(n²) ❌), we can use a sliding window + TreeSet approach: Maintain a window of size indexDiff. For each nums[i], find if there’s a number within [nums[i] - valueDiff, nums[i] + valueDiff]. TreeSet’s ceiling() function helps in efficient range searching. Slide the window forward by removing older elements. ⏱ Time Complexity: O(n log k) 💾 Space Complexity: O(k) where k = indexDiff ✨ Takeaway: This problem beautifully combines data structures (TreeSet) with sliding window, making it a great practice for hard-level problems. It sharpened my ability to handle both value-based and index-based constraints efficiently. #100DaysOfCode #Day71 #DSA #LeetCode #Java #ProblemSolving #TreeSet #SlidingWindow #CodingChallenge #HardProblem #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 18/100 of #100DaysOfCode ✅ Solved “Isomorphic Strings” (LeetCode #205) using Java! 🧩 Problem: Given two strings s and t, determine if they are isomorphic. 👉 Two strings are isomorphic if characters in one string can be replaced to get the other — while keeping order and unique mapping intact. 🧠 Approach: Used a HashMap to store the mapping of characters from s → t. Checked if each character in s has a consistent mapping in t. Also ensured that no two characters in s map to the same character in t. ✨ Example: s = "egg", t = "add" → true (e→a, g→d) s = "foo", t = "bar" → false 📈 Complexity: Time: O(n) Space: O(n) 💡 Key Learnings: Understood how to maintain one-to-one character mapping using a HashMap. Reinforced logic building for pattern matching between strings. 💻 Tech Used: Java | HashMap | Problem Solving #LeetCode #100DaysOfCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing #DevCommunity
To view or add a comment, sign in
-
-
🔥 #100DaysOfDSA — Day 30/100 Topic: Reverse an Array in Java 🔁 💡 What I Did Today: Today, I explored how to reverse an array manually using the two-pointer approach. Instead of relying on built-in methods, I learned how to swap elements from both ends until the array is completely reversed. 🧠 Logic Used: Initialize two pointers: start = 0 (beginning of array) end = numbers.length - 1 (end of array) While start < end: Swap numbers[start] and numbers[end] Move pointers inward → start++ and end-- 📊 Example: Input → {2, 4, 6, 8, 10} Output → {10, 8, 6, 4, 2} ⚙️ Time Complexity: O(n) — each element is swapped once. O(1) space — done in-place without using extra arrays. ✨ Takeaway: Learning to reverse an array manually helps strengthen the concept of pointers, loops, and in-place operations. Sometimes the simplest logic gives the biggest "aha!" moment 💡 #100DaysOfCode #Day30 #Java #DSA #Arrays #ProblemSolving #CodingJourney #LearnInPublic #DeveloperLife #CodeNewbie #LogicBuilding
To view or add a comment, sign in
-
-
✨ Day 74 of #100DaysOfCode Today, I solved LeetCode 22. Generate Parentheses using Backtracking in Java 🧠💻 📝 Problem Summary: Given n pairs of parentheses, generate all combinations of well-formed parentheses. ✅ Example: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] 💡 Key Idea: Use backtracking to build all valid strings. Keep track of the number of opening and closing brackets. Add '(' if open < n. Add ')' if close < open. When the string reaches length 2 * n, it's a valid combination. 🚀 Learning: Backtracking is a powerful technique for exploring all valid combinations. Keeping proper state (open, close) helps prune invalid paths early. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Backtracking #CodingChallenge #SowmiyaCodes #PlacementPrep
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