Day 99/100 – #100DaysOfCode 🚀 | #Java #PrefixSum #HashMap ✅ Problem Solved: Continuous Subarray Sum (LeetCode 523) 🧩 Problem Summary: Given an integer array and an integer k, determine if the array has a continuous subarray of size at least 2 whose sum is a multiple of k. 💡 Approach Used: ✔ Used Prefix Sum + HashMap ✔ Key observation: If two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Steps: Maintain running sum Store (prefixSum % k) → earliest index in a HashMap If the same remainder appears again and the subarray length ≥ 2 → return true Initialize map with (0 → -1) to handle edge cases ⚙ Time Complexity: O(N) 📦 Space Complexity: O(K) (where K = number of unique remainders) ✨ Takeaway: Modular arithmetic paired with prefix sums is a powerful pattern for detecting divisible subarrays in linear time. #Java #LeetCode #PrefixSum #HashMap #100DaysOfCode #CodingChallenge
Java Solution: Continuous Subarray Sum with Prefix Sum and HashMap
More Relevant Posts
-
Day 95/100 – #100DaysOfCode 🚀 | #Java #HashMap #Randomization ✅ Problem Solved: Random Flip Matrix (LeetCode 519) 🧩 Problem Summary: You are given a binary matrix initialized with all 0s. Each call to flip() should randomly return the position of a 0 and change it to 1. No position should be flipped more than once until reset() is called. 💡 Approach Used: ✔ Used HashMap + Randomization to simulate shuffling ✔ Treat the matrix as a flattened 1D array Steps: Map each index to its actual value using a HashMap Randomly pick an index from the remaining unflipped range Swap it with the last available index Decrease the available range On reset(), clear the map and restore the range ⚙ Time Complexity: O(1) per flip() 📦 Space Complexity: O(K) (where K = number of flipped cells) ✨ Takeaway: This problem is a great example of achieving uniform randomness without storing the entire matrix — smart indexing beats brute force. #Java #LeetCode #HashMap #Randomization #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 92/100 – #100DaysOfCode 🚀 | #Java #DynamicProgramming #Strings ✅ Problem Solved: Longest Palindromic Subsequence (LeetCode) 🧩 Problem Summary: Given a string, find the length of the longest subsequence that is also a palindrome. (A subsequence does not need to be contiguous.) 💡 Approach Used: ✔ Used Dynamic Programming (2D DP) ✔ Define dp[i][j] as the length of the longest palindromic subsequence in substring s[i…j] Logic: If s[i] == s[j] → dp[i][j] = 2 + dp[i+1][j-1] Else → dp[i][j] = max(dp[i+1][j], dp[i][j-1]) Fill the table bottom-up by increasing substring length ⚙ Time Complexity: O(N²) 📦 Space Complexity: O(N²) ✨ Takeaway: Problems involving subsequences often hint toward DP. Breaking the string into overlapping subproblems makes complex patterns manageable. #Java #LeetCode #DynamicProgramming #Strings #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 97/100 – #100DaysOfCode 🚀 | #Java #Strings #Logic ✅ Problem Solved: Longest Uncommon Subsequence I (LeetCode) 🧩 Problem Summary: Given two strings, find the length of the longest uncommon subsequence — a subsequence that appears in one string but not in the other. 💡 Approach Used: ✔ Observational / logical approach ✔ If both strings are equal, no uncommon subsequence exists ✔ If they are different, the longer string itself is the answer Why it works: A string is always a subsequence of itself If strings differ, the longer one cannot be a subsequence of the shorter ⚙ Time Complexity: O(1) 📦 Space Complexity: O(1) ✨ Takeaway: Some problems look like DP but are solved with pure logic. Always check for simple observations before overengineering. #Java #LeetCode #Strings #Logic #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 105 Backtracking & Recursion 🔹 Problem:- Combination Sum (LeetCode 39) Task: Given an array of distinct integers and a target value, find all unique combinations where the chosen numbers sum to the target. Each number can be used multiple times. Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3], [7]] My Approach: Used Backtracking to explore all possible combinations. At each step, decided to pick or skip the current element. Allowed reuse of elements by staying on the same index. Backtracked once the target became negative or a valid combination was found. Time Complexity: Exponential (based on number of combinations) Space Complexity: O(Target) (recursion stack + current path) Key Takeaway: Backtracking is all about choices and reversals. Once the pick / not-pick pattern becomes clear, complex problems start feeling much simpler. #takeUforward #200DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #LeetCode #DSA #CodeNewbie #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 29/100 ✅ LeetCode 138: Copy List with Random Pointer Solved this problem by creating a deep copy of a linked list where each node contains an additional random pointer. The approach ensures that both next and random pointers are copied correctly without modifying the original list. Key steps involved: Create copied nodes Link copied nodes with original nodes Assign random pointers efficiently Separate the original and cloned lists This problem helped me strengthen my understanding of linked list manipulation and pointer mapping. Solution:-https://lnkd.in/gD9TWWsg #LeetCode #LeetCode138 #CopyListWithRandomPointer #LinkedList #DSA #Java #ProblemSolving #CodingPractice #DeepCopy #DataStructures #Day29
To view or add a comment, sign in
-
-
Java☕ — HashMap vs ConcurrentHashMap clarified 🔥 Earlier, I thought: “HashMap works fine… why another map?” Then I learned about multi-threading. #Java_Code Map<String, Integer> map = new HashMap<>(); ❌ Not thread-safe #Java_Code Map<String, Integer> map = new ConcurrentHashMap<>(); ✅ Thread-safe The difference is not syntax — it’s behavior. 📌HashMap 📝Faster 📝Unsafe in multi-threading 📌ConcurrentHashMap 📝Thread-safe 📝Uses bucket-level locking 📝No ConcurrentModificationException Big realization for me: Thread safety is not optional in real applications. If multiple threads touch shared data — design matters. #Java #ConcurrentHashMap #Multithreading #BackendDevelopment #LearningJava
To view or add a comment, sign in
-
Java☕ — HashMap vs ConcurrentHashMap clarified 🔥 Earlier, I thought: “HashMap works fine… why another map?” Then I learned about multi-threading. #Java_Code Map<String, Integer> map = new HashMap<>(); ❌ Not thread-safe #Java_Code Map<String, Integer> map = new ConcurrentHashMap<>(); ✅ Thread-safe The difference is not syntax — it’s behavior. 📌HashMap 📝Faster 📝Unsafe in multi-threading 📌ConcurrentHashMap 📝Thread-safe 📝Uses bucket-level locking 📝No ConcurrentModificationException Big realization for me: Thread safety is not optional in real applications. If multiple threads touch shared data — design matters. #Java #ConcurrentHashMap #Multithreading #BackendDevelopment #LearningJava
To view or add a comment, sign in
-
Day 45 — Top K Frequent Elements (Java) Solved the classic Top K Frequent Elements problem using HashMap + Min Heap, focusing on correctness and interview-safety over gimmicks. What the code does (clearly and correctly): Reads n and builds the input array Uses a HashMap to count frequency of each element Maintains a min-heap of size k based on frequency Removes lower-frequency elements to keep only top k Prints the final result in correct order Complexity (no fluff): Time: O(n log k) Space: O(n) (frequency map + heap) This is not the brute-force sorting approach. This is the expected, scalable solution that handles hidden test cases properly. Optimizations like bucket sort can be explored later—this one is clean and reliable. Verified with input: 1 1 1 2 2 3, k = 2 → output: 1 2 #Java #DSA #DataStructureAndAlgorithm #HashMap #PriorityQueue #Heap #LeetCode #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 108 Combination Sum III Problem:- Combination Sum III Task:- Find all valid combinations of k numbers that sum up to n, using only numbers from 1 to 9, where: Each number is used at most once No duplicate combinations are allowed Example: Input: k = 3, n = 7 Output: [[1, 2, 4]] My Approach:- Used Backtracking (DFS) to explore all possible combinations. Started from a given number to avoid duplicates. Stopped recursion when: The combination size exceeded k The sum became negative Added the combination to the result only when: Exactly k numbers were chosen The sum became 0 Time Complexity:- Exponential (bounded due to range 1–9) Space Complexity:- O(k) (recursive stack + temporary list) Backtracking may look complex at first, but with clear base conditions and pruning, it becomes a powerful and elegant tool for solving combination problems efficiently. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 106 Backtracking & Recursion | Combination Sum II Problem: Combination Sum II Task: Given an array of candidate numbers and a target, find all unique combinations where the numbers sum up to the target. Each number can be used only once, and duplicate combinations are not allowed. Example: Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [1,1,6], [1,2,5], [1,7], [2,6] My Approach: Sorted the array to handle duplicates efficiently. Used backtracking to explore all valid combinations. Skipped duplicate elements to avoid repeated combinations. Pruned recursion when the current value exceeded the target. Time Complexity: Exponential (Backtracking) Space Complexity: O(N) due to recursion stack Backtracking becomes much cleaner and more efficient when combined with sorting and smart pruning. Understanding when to skip elements is just as important as choosing them. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie #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