📘 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
HashMap Frequency Counting for LeetCode 1512
More Relevant Posts
-
--------LeetCode Progress Update Solved: Convert a Number to Hexadecimal (405) * Problem Insight: Convert a 32-bit integer into its hexadecimal representation without using built-in conversion methods. * Approach: • Used bit manipulation (num & 15) to extract last 4 bits • Mapped values to hexadecimal characters (0-9, a-f) • Right shift (>>> 4) to process next chunk • Handled negative numbers using 2’s complement logic automatically * Key Learning: Bit manipulation makes low-level operations both efficient and elegant—especially when dealing with number systems. #LeetCode #DSA #Java #BitManipulation #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 47of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Mirror Distance of an Integer Problem Insight: Given a number, the task is to find the absolute difference between the original number and its reversed form. Approach: • Stored the original number in a temporary variable • Reversed the number using digit extraction (modulo and division) • Calculated the absolute difference between the original and reversed number Time Complexity: O(d), where d = number of digits Space Complexity: O(1) Key Learnings: • Digit manipulation using modulo and division is a powerful technique • Always store the original value before modifying the input • Reversing numbers is a fundamental pattern in many DSA problems Takeaway: Breaking the problem into simple steps makes even tricky-looking logic easy to solve. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
💻 Day 80 – DSA Practice (Binary Trees) Today’s challenge: 👉 Find all root-to-leaf paths where the sum equals a given target. 🔹 Approach: Use Depth-First Search (DFS) Track the current path and remaining sum When reaching a leaf node, check if the sum matches 💡 Key Concepts: Recursion + Backtracking Tree traversal (DFS) Efficient sum handling ⚡ Insight: Reducing the target sum at each step makes the solution cleaner and avoids recalculating sums. #DSA #BinaryTree #Java #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Today I solved Two Sum (LeetCode #1 - Easy) 💡 🔍 Problem: Find two indices such that their values add up to the target. 🧠 Approach I used: HashMap Instead of checking every pair (O(n²)), I used a HashMap to optimize the solution. 👉 Steps: Calculate complement = target - current element Check if complement exists in the map If yes → return indices ✅ If no → store the element in the map ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(n) 💡 This problem helped me understand how using extra space can reduce time complexity. 📌 Key Learning: “Optimize brute force by using HashMap for faster lookups.” #DSA #Java #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode
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
-
-
𝐃𝐚𝐲 𝟕𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding all elements that appear more than n/3 times in an array. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Majority Element II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐇𝐚𝐬𝐡𝐌𝐚𝐩 • Counted frequency of each element using a map • Calculated threshold = n / 3 • Collected elements whose frequency exceeded the threshold 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • At most 2 elements can appear more than n/3 times • HashMap is straightforward for frequency counting • Understanding constraints helps reduce possibilities • This problem has an optimized Boyer-Moore Voting (extended) solution 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Constraints often reveal hidden patterns — understanding them leads to better optimizations. 77 days consistent 🚀 On to Day 78. 🔗 Problem Link: https://lnkd.in/dDwdWYJs #DSA #Arrays #HashMap #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
=>Day 17/90 DSA Journey Solved: Continuous Subarray Sum (LeetCode) Today I worked on an interesting problem involving Prefix Sum + HashMap, which helped me optimize from a brute-force O(n²) solution to an efficient O(n) approach. If the same remainder (sum % k) appears again, it means the subarray between those indices has a sum divisible by k. -> What I learned: How to use prefix sum effectively Importance of storing remainders in HashMap Handling edge cases like subarray length ≥ 2 ->Optimization: Brute Force → O(n²) ❌ Optimized Approach → O(n) ✅ #LeetCode #DSA #Java #Coding #ProblemSolving #100DaysOfCode
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 47/75: Optimizing with Brian Kernighan’s Algorithm! Today’s challenge was counting the number of "1" bits in an integer. While the naive approach is to check all 32 bits one by one, I implemented a much more elegant solution known as Brian Kernighan’s Algorithm. The Logic: The expression n = n & (n - 1) has a fascinating property: it always flips the least significant set bit (the rightmost 1) to 0. By running this in a loop until $n$ becomes 0, the number of iterations equals exactly the number of set bits. If a number has only three "1" bits, the loop runs 3 times, not 32. The Result: ✅ Runtime: 0 ms (Beats 100.00% of Java users) ✅ Efficiency: $O(k)$ time complexity, where $k$ is the number of set bits. Mastering these bitwise shortcuts is what separates a good solution from a great one! 🚀 #75DaysOfCode #LeetCode #Java #BitManipulation #Algorithms #ComputerScience #Optimization #SoftwareEngineering
To view or add a comment, sign in
-
-
Day: 97/365 📌 LeetCode POTD: Minimum Distance to the Target Element Easy Key takeaways/Learnings from this problem: 1. This one shows that sometimes the simplest approach—just scanning the array—is more than enough. 2. Key learning: track the minimum absolute distance on the go instead of storing unnecessary data. 3. Good reminder that not every problem needs fancy algorithms, clarity beats complexity. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
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