𝗗𝗮𝘆 𝟭𝟯/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 Solved Two Sum using HashMap for constant-time lookup. ➤ Approach (O(n), O(n) space): • Traverse the array once • For each element, calculate complement = target - nums[i] • Check if complement already exists in the map • If yes → return the stored index and current index • Otherwise, store the current number with its index ➤ Key Insight: Instead of checking every pair (O(n²)), store what you’ve seen and look up what you need. This reduces the problem to a single pass. #LeetCode #Java #DSA #HashMap #ProblemSolving #20DaysChallenge #Consistency
Solved LeetCode's Two Sum Challenge with HashMap
More Relevant Posts
-
𝗗𝗮𝘆 𝟭𝟰/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved First Unique Character in a String using HashMap (Frequency Counting). ➤ Approach (O(n), O(1) space): • First pass: Count frequency of each character using a HashMap • Second pass: Traverse the string again and return the first character with frequency = 1 • If none found → return -1 ➤ Key Insight: Instead of checking each character repeatedly (which would be O(n²)), store frequencies once and reuse them. #LeetCode #Java #DSA #HashMap #StringProblems #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
Day 20 - Find Lucky Integer in an Array Technique Used: HashMap Frequency Counting Key Idea: Constructed a frequency map to count occurrences of each element. Iterated through the map to identify elements whose value equals their frequency, and returned the maximum among them. Time Complexity: O(n) #Day20 #LeetCode #Java #DSA #HashMap #ProblemSolving
To view or add a comment, sign in
-
-
Day 10 – LeetCode question Solved LeetCode 560 – Subarray Sum Equals K (Medium) Approach: Prefix Sum + HashMap Maintained running cumulative sum Checked if (sum − k) existed in the map Used HashMap to store frequency of prefix sums Counted all valid subarrays efficiently Time Complexity: O(n) Space Complexity: O(n) Strengthened understanding of prefix sum patterns and hashmap-based optimizations for subarray problems. #LeetCode #Day10 #PrefixSum #HashMap #DSA #Java #ProblemSolving #Consistency #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 60 — HashMap & Frequency Counting 🧩 3659. Partition Array Into K-Distinct Groups Today I solved a problem using HashMap and frequency counting in Java. 🧠 Approach: 🔹 First, count the frequency of each number using a HashMap. 🔹 Since every group must contain k distinct elements, the maximum number of groups possible is: groups=nums.lengthk\text{groups} = \frac{nums.length}{k}groups=knums.length🔹 If any element appears more than the number of groups, it cannot be distributed without repeating in a group. 🔹 Also check if the array length is divisible by k. 💡 Key Insight: If frequency of any element > total groups, forming valid groups with distinct elements becomes impossible. ⏱️ Complexity: Time: O(n) Space: O(n) #Day60 #LeetCode #Java #HashMap #DSA #ProblemSolving #Consistency 💪🚀
To view or add a comment, sign in
-
-
🚀 DSA Day 11 | HashMap in Action Today I worked on a classic DSA problem: finding the occurrence of elements in an array using HashMap. ✨ Learned how to: Count frequencies efficiently Update values while traversing the array Use HashMap to optimize time complexity 📌 Key takeaway: HashMap makes frequency-based problems simple and efficient. #DSA #Day11 #Java #HashMap #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
-
Move All Zeroes to End | #geekstreak2026 Day 17 Question: Given an array of non-negative integers, move all zeroes to the end while maintaining the relative order of non-zero elements. The operation must be performed in-place. Approach (Two-Pointer – Java): • Use two pointers: count → position for next non-zero element i → traverses the array • Traverse the array: If arr[i] != 0 → swap arr[i] with arr[count] Increment count • After one pass: All non-zero elements remain in order at the front All zeroes shift to the end automatically Time Complexity: O(N) Space Complexity: O(1) #geekstreak2026 #GeeksforGeeks #POTD #Java #TwoPointers #Arrays #Algorithms #CodingChallenge #npci
To view or add a comment, sign in
-
-
🌳 Day 59/100: Preorder Traversal - Root, Left, Right Day 59. Learning tree traversals. Preorder: Visit root first, then explore. 🎯 📌 Problem: Return preorder traversal of binary tree. 🎯 Recursion: Base case: If node is null, return. Recursive case: Add value, go left, go right. Simple and clean. 📊 Complexity: O(n) time, O(h) space (recursion stack) 🌱 Tree Traversals: Preorder: Root → Left → Right Inorder: Left → Root → Right Postorder: Left → Right → Root Day 59. Traversal patterns building. 🌳 #100DaysOfCode #DSA #LeetCode #Day59 #Java #PreorderTraversal #BinaryTree #TreeTraversal
To view or add a comment, sign in
-
-
Day 40/100 | #100DaysOfDSA 💻🔥 Today’s problem: Subarray Sum Equals K Goal: Find the total number of subarrays whose sum equals k. Approach: Prefix Sum + HashMap Idea: • Keep track of the running sum while traversing the array • If (currentSum - k) was seen before, a subarray with sum k exists • Use a HashMap to store prefix sums and their frequencies Key insight: If prefixSum[j] - prefixSum[i] = k, then the subarray between them sums to k. Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Prefix sums combined with hashing can turn many subarray problems from O(n²) to O(n). Day 40 — still stacking patterns. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #PrefixSum #HashMap #Java #CodingJourney #InterviewPrep #ProblemSolving
To view or add a comment, sign in
-
-
Day 40 — Maximum Depth of Binary Tree Solved LeetCode 104: Maximum Depth of Binary Tree. Used recursion: Base case → return 0 if node is null Recursively compute left and right depth Return 1 + max(left, right) Simple problem, but important for understanding tree height, recursion flow, and divide-and-conquer thinking. Building strong tree foundations step by step. #Day40 #LeetCode #BinaryTree #Recursion #DSA #Java #Consistency
To view or add a comment, sign in
-
-
🚀 Day 26 of #100DaysOfCode 🌱 Topic: Arrays / Hashing ✅ Problem Solved: LeetCode 128 – Longest Consecutive Sequence 🛠 Approach: Stored all elements in a HashSet for O(1) lookup. For each number, checked if it is the start of a sequence (i.e., num - 1 is not present in the set). If yes, kept expanding forward (num + 1) to count length. Updated maximum length accordingly. Avoided sorting to maintain O(n) time complexity. #100DaysOfCode #Day26 #DSA #Arrays #Hashing #Java #LeetCode #ProblemSolving #CodingJourney #Consistency #LearningInPublic #SoftwareEngineering
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