Day 2/30 of #30DaysOfDSA 🔹 Problem: Two Sum 💡 Approach: I first thought of a brute-force approach. I checked all possible pairs using two loops and returned the indices where the sum equals the target. This works fine but takes O(n²) time. Then I optimized the solution using HashMap. Instead of checking every pair, I stored each element with its index in a HashMap. For every element, I calculated the complement (target - current value) and checked if it already exists in the map. This reduced the time complexity to O(n) and made the solution more efficient. ⚡ What I Learned: I learned how to optimize a problem by reducing unnecessary comparisons. Using HashMap helps achieve faster lookups and improves time complexity significantly. #DSA #Coding #Consistency #Java
Optimizing Two Sum Problem with HashMap in Java
More Relevant Posts
-
Day 1/30 of #30DaysOfDSA 🔹 Problem: Valid Anagram 💡 Approach: I first thought of a brute-force approach. I considered sorting both strings and then comparing them. This works, but sorting takes O(n log n) time, which is not the most optimal solution. Then I optimized using frequency array (int[26]): Instead of using HashMap, I used an array to count character frequencies. For each character, I incremented for one string and decremented for the other in a single loop. This made the solution more efficient and reduced overhead, while still achieving O(n) time complexity. ⚡ What I Learned: I learned how to move from brute force to optimization by focusing on the actual requirement — here, only frequency matters, not order. Hashing makes problems faster by enabling constant-time lookups and efficient counting. #DSA #Coding #LearningInPublic #Consistency #Java
To view or add a comment, sign in
-
-
Day 4/30 of #30DaysOfDSA 🔹 Problem: Top K Frequent Elements 💡 Approach: At first, I considered a straightforward approach — count the frequency of each element and then sort them based on frequency to pick the top k elements. While this works, the time complexity comes out to be O(n log n), which isn’t optimal for large inputs. To improve this, I explored a more efficient approach using Bucket Sort. I stored the frequency of each element using a HashMap, and instead of sorting, I grouped elements into buckets where the index represents their frequency. By iterating through these buckets from highest to lowest frequency, I was able to directly extract the top k frequent elements. This optimization reduced the time complexity to O(n), making the solution much more efficient. ⚡ What I Learned: • How to optimize solutions by eliminating unnecessary sorting • The power of HashMap for frequency counting • How Bucket Sort can be applied in real problem-solving scenarios #DSA #Coding #Consistency #Java
To view or add a comment, sign in
-
-
Day 82 – Univalued Binary Tree Solved a problem to determine whether all nodes in a binary tree have the same value using depth-first traversal. Key Learnings: Applied DFS recursion to traverse all nodes in the tree Compared each node’s value with the root value for validation #DSA #Java #BinaryTree #DFS #Recursion #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 30 Today’s focus: HashMap for frequency counting. Problem solved: • Number of Good Pairs (LeetCode 1512) Concepts used: • HashMap • Frequency counting • Incremental counting technique Key takeaway: The goal is to count pairs (i, j) such that nums[i] == nums[j] and i < j. Instead of checking all pairs (which would take O(n²)), we use a HashMap to track frequencies. As we iterate through the array: • For each number, we check how many times it has already appeared • That count directly contributes to the number of valid pairs • Then we update its frequency in the map This allows counting pairs in a single pass with O(n) time complexity. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 52 Cracked Reverse Integer today — a classic problem that looks simple but tests attention to edge cases like overflow and sign handling. 🔁 Key takeaway: It’s not just about reversing digits, it’s about doing it safely within constraints. ⚡ Runtime: 1 ms (Top 0.04%) 📊 Memory: Solid performance Small problems, deep thinking — that’s the grind. #Day52 #LeetCode #ProblemSolving #Java #Consistency #DSA #CodingJourney
To view or add a comment, sign in
-
-
Day 74 - Univalued Binary Tree Checking whether all nodes in a binary tree share the same value using recursion. Approach: • Store root value • Traverse the tree recursively • Compare each node with root value • Return false if any mismatch is found Key Insight: Recursion simplifies tree traversal and validation Time Complexity: O(n) #Day74 #LeetCode #Java #CodingPractice #TechJourney #DSA #BinaryTree
To view or add a comment, sign in
-
-
#75DaysOfDSA – Day 17 Problem: Letter Combinations of a Phone Number Approach: Implemented using recursion and backtracking Created a keypad mapping array for digit-to-character conversion Generated all possible combinations by exploring each recursive branch Key Learnings: Better understanding of recursion flow and tree structure Handling string immutability during recursive calls Importance of base case and incremental state building Performance: Runtime: 3 ms All test cases passed #DSA #Java #LeetCode #Backtracking #Recursion
To view or add a comment, sign in
-
-
Day2 - Demystifying HashMap - How it works under the Hood? 👉 HashMap works on the principle of hashing. It stores values in the form of key-value pairs and to access a value you need to provide the key. 👉 HashMap is basically a 2 dimensional Singly Linked List. It can grow in both directions. For efficient use of HashMap the 'key' element should implement equals() and hashcode() method. equals() method defines that two objects are meaningfully equal. hashcode() helps HashMap to arrange elements separately in a bucket. So elements with same hascode are kept in the same bucket together. 👉 It uses the equals() method to identify the actual object present in the bucket. #java #coding
To view or add a comment, sign in
-
-
Day 84 - Binary Tree Right Side View Computed the visible nodes when viewing a binary tree from the right side. Approach: • Use level order traversal (BFS) • Process nodes level by level • Add the last node of each level to the result Key Idea: The rightmost node at each level is the visible one Time Complexity: O(n) Space Complexity: O(n) #Day84 #LeetCode #BinaryTree #BFS #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
Day 9/30 — DSA Challenge 🚀 Problem: Construct Binary Tree from Preorder and Inorder Traversal Topic: Tree + Recursion + Hashing Difficulty: Medium Approach: Used preorder to identify root node Used inorder to divide left and right subtrees Stored inorder indices in a HashMap for O(1) lookup Recursively built left and right subtrees Mistake / Challenge: Initially struggled to understand how preorder and inorder work together Got confused in managing indices for subtree boundaries Fix: Used a pointer for preorder traversal Used HashMap to quickly find root index in inorder Carefully handled left and right subtree ranges Key Learning: Preorder → gives root Inorder → gives structure (left/right split) Combining both helps reconstruct the tree Time Taken: 1 hour Consistency check ✅ See you on Day 10. GitHub Repo: https://lnkd.in/gHW9vKUf #DSA #LeetCode #Java #Trees #Recursion #LearningInPublic
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