𝗗𝗮𝘆 𝟱/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 Solved Continuous Subarray Sum using Prefix Sum & HashMap. ➤ Approach (O(n), O(n) space): — Maintain a running totalSum — Compute remainder = totalSum % k — Store the first index where each remainder appears — If the same remainder appears again and the distance between indices ≥ 2, a valid subarray exists ➤ Key Insight: If two prefix sums have the same remainder when divided by k, their difference is a multiple of k. #LeetCode #Java #DSA #PrefixSum #HashMap #ProblemSolving #20DaysChallenge #Consistency
LeetCode Challenge: Solved Continuous Subarray Sum with Prefix Sum & HashMap
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
-
-
𝗗𝗮𝘆 𝟭𝟯/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 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
To view or add a comment, sign in
-
-
🔢 Day 58/100: Intersection of Two Arrays II Day 58. Back to arrays. HashMap for frequency tracking. Simple and effective. ✅ 📌 Problem: Find intersection of two arrays, including duplicates. Example: nums1 = [1,2,2,1], nums2 = [2,2] → [2,2] 💡 Approach: 1. Count frequency of nums1 in HashMap 2. For each num in nums2: - If exists and count > 0 → Add to result - Decrease count 3. Return result 🎯 Why HashMap: Tracks how many times each number appears. Handles duplicates correctly. 📊 Complexity: O(n + m) time, O(n) space 🌱 Pattern: Frequency problems → Think HashMap. Day 58 done. 💪 #100DaysOfCode #DSA #LeetCode #Day58 #Java #HashMap #ArrayIntersection #FrequencyCount
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 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
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟰/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 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
-
-
𝗗𝗮𝘆 𝟵/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solve Merge Sorted Array using the Two Pointer (from end) technique. ➤ Approach (O(m + n), O(1) space): Set three pointers: —> i = m - 1 (end of nums1 valid elements) —> j = n - 1 (end of nums2) —> k = m + n - 1 (end of nums1 total capacity) Compare elements from the back and place the larger one at index k Move pointers accordingly until one array is exhausted ➤ Key Insight: Merging from the front would overwrite values. Merging from the back avoids extra space and keeps everything in-place. #LeetCode #Java #DSA #TwoPointers #ArrayProblems #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
📝 Day 17/30 – LeetCode #238 (Product of Array Except Self) | Java The straightforward solution using division fails due to edge cases involving zeros and is explicitly disallowed. A brute-force approach would also be inefficient with O(n²) time complexity. By computing prefix products and suffix products, I was able to calculate the result for each index without using division. Each element in the output array represents the product of all numbers before and after it, resulting in an O(n) time solution with constant extra space. This problem highlighted how breaking a problem into directional passes can simplify complex constraints. #LeetCode #Java #DSA #Arrays #PrefixSum #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
Day 28 — LeetCode Progress (Java) Problem: Sort Array By Parity Required: Given an integer array, rearrange it so that all even numbers come first, followed by all odd numbers. Return the modified array. Idea This is a simple partitioning problem. Even and odd separation can be done by: Collecting evens first Then placing odds No sorting required. Just grouping. Approach Create a result array of same length. Traverse once: If number is even → place in result. Traverse again: If number is odd → place in result. Return result. Time Complexity: O(n) Space Complexity: O(n) (extra array used) #LeetCode #DSA #Java #Arrays #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Roman to Integer using HashMap + Single Traversal. ➤ Approach (O(n), O(1) space): • Store Roman symbols and their values in a HashMap • Traverse the string from left to right • If the current value is less than the next value → subtract • Otherwise → add ➤ Key Insight: Roman numerals mostly add up left to right, but when a smaller value appears before a larger one, it indicates subtraction (like IV, IX, XL). By checking the next character during traversal, we can decide whether to add or subtract — all in one pass. #LeetCode #Java #DSA #HashMap #StringManipulation #ProblemSolving #20DaysChallenge #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