𝗧𝗵𝗶𝗻𝗸𝗶𝗻𝗴 𝗕𝗲𝘆𝗼𝗻𝗱 𝘁𝗵𝗲 𝗗𝗶𝘃𝗶𝘀𝗶𝗼𝗻 𝗢𝗽𝗲𝗿𝗮𝘁𝗼𝗿 I recently tackled the "Product of Array Except Self" problem. My first instinct? Use the division operator. It’s quick, but it's brittle, especially if your data contains a zero. I challenged myself to find a more robust way and discovered the Prefix & Suffix product pattern. Instead of looking at the whole array at once, you calculate the product of everything to the left, then everything to the right. The Results: Logic: O(n) time complexity. Efficiency: O(1) extra space (excluding the output array). Reliability: Handles zeros perfectly without crashing. It’s a powerful reminder that sometimes the most obvious tool isn't the most effective one. I'm learning how to write code that doesn't just work, but scales. #Java #LeetCode #SoftwareEngineering #ProblemSolving #Algorithms #CleanCode #GrowthMindset
Product of Array Except Self Solution with Prefix Suffix Pattern
More Relevant Posts
-
🎯 #LeetcodePotd | Problem Solved with 99%+ Runtime Performance Sometimes the smartest solution isn’t fancy — it’s structured thinking: Just solved LeetCode 1984 – Minimum Difference Between Highest and Lowest of K Scores ✅ And honestly? This is a perfect example of how sorting + sliding window = instant clarity. 🧠 Problem in Simple Words You’re given student scores. Pick k students such that the difference between the highest and lowest score is as small as possible. 👉 Goal: Minimize (max − min) among any group of size k. 💡 Key Insight (Beginner Friendly) If the array is sorted, then: Any group of k students will be contiguous The difference becomes: nums[i + k − 1] − nums[i] So instead of brute force chaos, we: Sort the array Slide a window of size k Track the minimum difference Clean. Efficient. Scalable. ⏱️ Complexity Analysis Time Complexity: O(n log n) (sorting dominates) Space Complexity: O(1) (ignoring sorting internals) 📊 Submission Stats ✔ Accepted: 118 / 118 test cases ⚡ Runtime: 7 ms 🔥 Beats 99.17% of submissions 💾 Memory: 47.15 MB 🧠 Takeaway Sort first. Think visually. Then optimize. On to the next one. Consistency > motivation. 💪 #LeetCode #DSA #Java #ProblemSolving #Algorithms #SlidingWindow #Sorting #CodingJourney #BeginnersInTech #100DaysOfCode #SoftwareEngineering #InterviewPrep
To view or add a comment, sign in
-
-
0ms. 100% Beats. The "Aha!" Moment. There’s nothing quite like the feeling of solving a problem so efficiently that the runtime hits 0ms. I just tackled Linked List Cycle Detection. It’s a classic, but every time I implement it, I’m reminded of how elegant a simple idea can be. The Strategy: Instead of using extra memory to "remember" where I've been, I used Floyd’s Cycle-Finding Algorithm (the "Tortoise and the Hare"). * How it works: You run two pointers at different speeds. If there's a loop, they are guaranteed to meet. If not, the "Hare" hits the end. * The Result: O(1) Space Complexity and a 100.00% Runtime beat. In engineering, we often try to solve problems by adding more "stuff"—more memory, more variables, more complexity. This problem is a great reminder that sometimes, the most performant solution comes from just changing your perspective. Consistency isn't just about showing up; it's about refining the logic until it's seamless. Are you a fan of pointer-based solutions, or do you prefer the readability of a Hash Set? Let’s talk shop in the comments! 👇 #LeetCode #Java #SoftwareEngineering #CodingLife #DataStructures #Optimization #JavaDeveloper
To view or add a comment, sign in
-
-
🚀 Day 503 of #750DaysOfCode 🔥 LeetCode 3714 — Longest Balanced Substring II (Medium) Today’s challenge was all about prefix differences, hashing, and smart optimization. We’re given a string containing only 'a', 'b', and 'c'. A substring is called balanced if all distinct characters in that substring appear the same number of times. 🧠 Key Insight Since we only have 3 characters, instead of brute-forcing every substring (which would be O(n²) ❌), we can: Track prefix counts of a, b, c Store frequency differences Use a HashMap to detect when the same difference pair appears again If two prefixes have the same difference values: (countA - countB) (countB - countC) Then the substring between them is balanced ✅ 💡 Optimization Strategy This solution smartly breaks the problem into 3 cases: 1️⃣ Only one distinct character 2️⃣ Exactly two distinct characters 3️⃣ All three characters Each case is handled efficiently using prefix differences and HashMaps. Time Complexity: O(n) Space Complexity: O(n) No brute force. No nested loops. Just pure prefix logic. 🔥 What I Learned Today ✔ Prefix difference technique is powerful ✔ Hashing states helps avoid O(n²) brute force ✔ Breaking complex problems into structured cases simplifies thinking ✔ Medium problems often test observation, not complexity “The difference between good and great coders is not syntax — it’s pattern recognition.” Onward to Day 504 💪🔥 #LeetCode #DataStructures #Algorithms #Java #CodingJourney #750DaysOfCode #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problems pushed me to think more about optimization, pruning, and duplicate handling. ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬 𝐒𝐨𝐥𝐯𝐞𝐝 📌 4Sum 📌 Remove Duplicates from Sorted Array 🔹 𝟒𝐒𝐮𝐦 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Sorted the array Fixed two indices and used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 for the remaining pair Applied 𝐞𝐚𝐫𝐥𝐲 𝐩𝐫𝐮𝐧𝐢𝐧𝐠 using the minimum and maximum possible sums Carefully skipped duplicates at every level 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Pruning reduces unnecessary iterations ✅ Using long avoids integer overflow ✅ Duplicate handling is the hardest (and most important) part 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n³) 📦 Space: O(1) (excluding output) 🔹 𝐑𝐞𝐦𝐨𝐯𝐞 𝐃𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 𝐟𝐫𝐨𝐦 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 One pointer tracks the last unique element Overwrote duplicates in-place 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Sorted arrays enable in-place solutions ✅ Simple logic can be extremely efficient 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n) 📦 Space: O(1) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Optimization is not about writing complex code — it’s about 𝐚𝐯𝐨𝐢𝐝𝐢𝐧𝐠 𝐮𝐧𝐧𝐞𝐜𝐞𝐬𝐬𝐚𝐫𝐲 𝐰𝐨𝐫𝐤. On to 𝐃𝐚𝐲 𝟓 🔁🚀 #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 | 𝗠𝗲𝗿𝗴𝗲 𝗧𝘄𝗼 𝗦𝗼𝗿𝘁𝗲𝗱 𝗟𝗶𝘀𝘁𝘀 Day 36 ✅ — Recursion over iteration. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟭: Merge Two Sorted Lists (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Merge two sorted linked lists. Instead of the iterative dummy node approach, I used 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻. Compare the heads. Attach the smaller one, then recursively merge the rest. Base case: if one list is empty, return the other. Elegant. Three lines of logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Base cases: if l1 is null, return l2 (and vice versa) 👉 Compare l1.val and l2.val 👉 Attach smaller node and recurse with remaining lists Time: O(n + m), Space: O(n + m) for recursion stack 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Seven linked list problems, and recursion just made this one cleaner than iteration. Sometimes the simplest code is the most powerful. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g4f4_B69 𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 ✅ | 𝟲𝟰 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Recursion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #Programming
To view or add a comment, sign in
-
🚀 Day 5/30 – Understanding Stack-Based Validation Today I worked on validating balanced parentheses in a string — a classic problem that tests understanding of stacks and order of operations. The challenge was to ensure that every opening bracket has a corresponding closing bracket in the correct order. Instead of relying on pattern matching, I implemented a stack-based approach, which naturally fits this type of problem. 💡 Approach Traverse the string character by character Push opening brackets onto the stack When encountering a closing bracket, check whether it matches the most recent opening bracket If at any point the structure breaks, return false This approach ensures: O(n) time complexity O(n) space complexity (in worst case) 📊 Result ✅ Accepted (All test cases passed) ⚡ Efficient runtime performance 📚 Key Takeaway This problem reinforced an important concept: Stacks are extremely powerful when dealing with nested structures, syntax validation, and expression parsing. Small problems like this build strong fundamentals for larger system-level logic. Day 5 complete. Consistency continues. #Day5 #30DaysOfCode #LeetCode #Java #Stacks #Algorithms #DataStructures #ProblemSolving #SoftwareEngineering #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 2/30 – LeetCode Challenge Solved “Majority Element” today using the Boyer–Moore Voting Algorithm. 🔹 Problem: Find the element that appears more than ⌊n/2⌋ times in an array. 🔹 Requirements: ✔ O(n) Time Complexity ✔ O(1) Space Complexity 💡 Approach Used – Boyer–Moore Voting Algorithm Instead of using a HashMap (which takes extra space), I used an optimized approach: Maintain a candidate and a count If count becomes 0 → update candidate If number equals candidate → increment count Else → decrement count This works because the majority element will always survive the cancellation process. ⚡ Result: ✅ Accepted (53/53 test cases) ⚡ Runtime: 1 ms (Beats 99.82%) 💾 Memory: 55.75 MB 📚 Learning: Today’s problem taught me how powerful algorithmic thinking can replace brute force solutions. Space optimization makes a big difference in interviews. On to Day 3 💪 #Day2 #30DaysOfCode #LeetCode #Java #Algorithms #DataStructures #BoyerMoore #ProblemSolving #CodingJourney #SoftwareEngineeringStudent #Consistency #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 12/30 – Mastering Binary Search Patterns Today’s problem required finding the position of a target value in a sorted array — or determining where it should be inserted if it doesn’t exist. Rather than scanning linearly, I implemented a Binary Search approach to maintain optimal performance. 💡 Approach Maintain two pointers: left and right Compute mid carefully to avoid overflow Compare the target with the middle element Narrow the search space accordingly If the element is not found, return the left pointer as the insertion index This pattern ensures: ⏱ O(log n) time complexity 📦 O(1) space complexity 📊 Performance ✅ All test cases passed ⚡ 0 ms runtime (100% performance) 💾 Strong memory efficiency 📚 Key Takeaway Binary Search is more than just finding elements — it’s about identifying boundaries and understanding how search space evolves. Recognizing insertion logic as a natural extension of search strengthens problem-solving intuition. Day 12 complete. The fundamentals are getting sharper every day. #Day12 #30DaysOfCode #LeetCode #Java #BinarySearch #Algorithms #ProblemSolving #SoftwareEngineering #CodingJourney #Consistency #TechGrowth
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 | 𝗗𝗲𝗹𝗲𝘁𝗲 𝗡𝗼𝗱𝗲 & 𝗥𝗲𝗺𝗼𝘃𝗲 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 Day 44 ✅ — Two deletion techniques, one day. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟯𝟳: Delete Node in a Linked List (Medium) ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟬𝟯: Remove Linked List Elements (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: 𝕯𝖊𝖑𝖊𝖙𝖊 𝕹𝖔𝖉𝖊 (#237): The twist? You're only given the node to delete—not the head. Can't access previous node. Solution: Copy next node's value to current, then skip next. Clever trick that feels like breaking the rules. 𝕽𝖊𝖒𝖔𝖛𝖊 𝕰𝖑𝖊𝖒𝖊𝖓𝖙𝖘 (#203): Remove all nodes with a specific value. The standard deletion problem—handle head separately, then traverse and skip matching nodes. Two problems, two deletion strategies. Both are in-place, O(1) space. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵𝗲𝘀: 𝑫𝒆𝒍𝒆𝒕𝒆 𝑵𝒐𝒅𝒆: 👉 Copy next node's value to current 👉 Skip next node: node.next = node.next.next 𝑹𝒆𝒎𝒐𝒗𝒆 𝑬𝒍𝒆𝒎𝒆𝒏𝒕𝒔: 👉 Handle head nodes with target value first 👉 Traverse and skip nodes matching value 👉 Return potentially new head Time: O(n), Space: O(1) for both 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Fifteen linked list problems (Day 30-44). Deletion patterns are now second nature. The "Delete Node" trick shows that sometimes the clever solution isn't what you'd expect. Challenge your assumptions. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gMb9bPNy 𝗗𝗮𝘆 𝟰𝟰/𝟭𝟬𝟬 ✅ | 𝟱𝟲 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #NodeDeletion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #InPlaceAlgorithm #Programming #ProblemSolving
To view or add a comment, sign in
Explore related topics
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