📌 Day 26/100 - Maximum Subarray (LeetCode 53) 🔹 Problem: Given an integer array, find the contiguous subarray that has the largest sum and return it. 🔹 Approach: This problem is a classic example of Kadane’s Algorithm — a dynamic programming approach that efficiently finds the maximum sum subarray in linear time. Start with the first element as both current and maximum sum. Traverse through the array, at each step decide whether to add the current number to the previous sum or start fresh. Keep track of the global maximum as you go. 🔹 Complexity: ⏱ Time: O(n) 💾 Space: O(1) 🔹 Key Learning: Sometimes the optimal path starts fresh — like resetting a sum when it goes negative. Dynamic programming isn’t always about big tables — it can be as elegant as tracking a few variables. A simple algorithm can solve what appears to be a complex problem efficiently. #100DaysOfCode #LeetCode #Java #ProblemSolving #KadaneAlgorithm #DSA #CodingJourney
"Kadane's Algorithm: Finding Maximum Subarray Sum"
More Relevant Posts
-
🌟 Day 81 of #100DaysOfCoding 🌟 🔹 Problem: 1186. Maximum Subarray Sum with One Deletion 🔹 Platform: LeetCode 🔹 Language: Java 💡 Concept: This problem is an advanced variation of Kadane’s Algorithm. The goal is to find the maximum subarray sum where we are allowed to delete at most one element. To solve this, I used Dynamic Programming with two arrays: noDelete[i] → max subarray sum ending at index i without deleting any element. oneDelete[i] → max subarray sum ending at index i with one deletion allowed. The key idea is to track both possibilities at each step to ensure we consider both — keeping or deleting an element — while maximizing the result. 🧠 Fixed Error: Initially, I faced an ArrayIndexOutOfBoundsException because I started the loop from i = 0. After adjusting the loop to start from i = 1, the issue was resolved successfully. ✨ Learning: Always check loop boundaries carefully in DP problems. Maintaining two states helps handle “delete one element” type conditions efficiently. Small indexing mistakes can lead to runtime errors — debugging them strengthens logical precision. #Day81 #LeetCode #DynamicProgramming #KadaneAlgorithm #100DaysOfCode #CodingChallenge #Java #ProblemSolving
To view or add a comment, sign in
-
-
💡 LeetCode 3354 — Make Array Elements Equal to Zero Today I solved one of the most conceptually beautiful array problems I’ve come across. At first glance, it’s labeled as Easy, but it actually blends multiple concepts — making it feel more like a Medium problem. What makes it special: It involves simulation — tracking how a pointer moves across the array. It demonstrates call by reference for array objects in Java. The pointer movement feels like simple harmonic motion (SHM) in physics — oscillating back and forth after each decrement! Here, every time the pointer hits a non-zero value, it decrements it and reverses its direction — just like a particle turning around at the ends of SHM. A perfect example of how programming logic and physics intuition can align beautifully. 🧠 Concepts Involved: Array simulation Recursion and cloning (for preserving states) Java’s reference behavior with arrays LeetCode may call it Easy, but conceptually it deserves a Medium tag for its depth and insight. #LeetCode #Java #DSA #ProblemSolving #Simulation #LearningEveryday
To view or add a comment, sign in
-
-
💻 Day 69 of #LeetCode100DaysChallenge Solved LeetCode 264: Ugly Number II a dynamic programming problem that improves sequence generation and pointer-based optimization skills. 🧩 Problem: An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer n, return the n-th ugly number. 💡 Approach — Dynamic Programming with Three Pointers: 1️⃣ Maintain an array dp where dp[i] stores the i-th ugly number. 2️⃣ Use three pointers p2, p3, and p5 to track multiples of 2, 3, and 5 respectively. 3️⃣ At each step, choose the smallest of dp[p2]*2, dp[p3]*3, and dp[p5]*5 as the next ugly number. 4️⃣ Increment the corresponding pointer(s) to avoid duplicates. 5️⃣ Continue until the nth ugly number is found. ⚙️ Complexity: Time: O(N) — one pass to generate all ugly numbers Space: O(N) — for storing the sequence ✨ Key Takeaways: ✅ Strengthened understanding of pointer-based DP techniques. ✅ Learned how to efficiently generate sorted multiplicative sequences. ✅ Practiced optimization over brute-force factor checking. #LeetCode #100DaysOfCode #Java #DynamicProgramming #TwoPointers #Math #DSA #ProblemSolving #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
🚀 Day 47 of My LeetCode Journey 🚀 Problem: Sum of Square Numbers Topic: Math / Two Pointers 🧠 Approach: To check if a number c can be expressed as the sum of squares of two integers (a² + b² = c), we use the two-pointer technique: Start a = 0 and b = √c Calculate sum = a² + b² If sum == c → ✅ return true If sum < c → increase a If sum > c → decrease b Repeat until a <= b. ⏱️ Complexity: Time: O(√c) Space: O(1) This problem beautifully combines mathematical logic with an efficient two-pointer approach — clean and elegant! 💡 #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #DSA
To view or add a comment, sign in
-
-
📌 Day 159 of Coding - Make Array Elements Equal to Zero (LeetCode - Easy) 🎯 Goal: Given an integer array nums, find the number of valid starting positions and directions such that by repeatedly decrementing and reversing direction (based on the rules), all elements eventually become 0. 💡Approach & Debugging: First computed total sum using Arrays.stream(nums).sum(). Maintained two prefix sums : left and right. For each index i where nums[i] == 0: Incremented left and decremented right progressively. Checked whether the left and right sums balance according to the movement rules. Counted valid selections when the balance was perfect or off by just one. ✔️ Time Complexity: O(N) - single pass through the array. ✔️ Space Complexity: O(1) - constant extra space. #Day159 #LeetCode #Simulation #Arrays #PrefixSum #Java #ProblemSolving #DSA #CodingChallenge #100DaysOfCode #Algorithms #InterviewPrep #SoftwareEngineering #DataStructures #CodeNewbie #ProgrammersLife
To view or add a comment, sign in
-
-
Day 25 of #LeetCode Challenge 🔹 Problem: 3354. Make Array Elements Equal to Zero 🔹 Difficulty: Easy 🔹 Topic: Array | Simulation | Prefix Sum Today’s problem was a fun logic-based simulation involving movement and balance between left and right sums. The goal was to find how many starting positions (and directions) could lead to every element becoming zero. The trick here is realizing that you don’t actually need to simulate every move — just track prefix sums and compare left and right totals. If they’re equal (or differ by 1), it’s a valid starting point. A neat mix of math and logic! #LeetCode #100DaysOfCode #Day25 #Java #CodingChallenge #ProblemSolving #LearnToCode #LeetCodeDaily #ProgrammingJourney #DSA #Algorithms
To view or add a comment, sign in
-
-
📌 Day 153 of Coding - Maximum Frequency of an Element After Operations I (LeetCode - Medium) 🎯 Goal: Given an integer array nums, an integer range k, and a number of operations, determine the maximum possible frequency of any number after performing up to numOperations modifications - where each operation can change a number by at most k. 💡Approach & Debugging: Found the maximum value in the array and created a frequency prefix array. For each possible target value i, calculated the total numbers within the [i - k, i + k] range. The frequency of i could be increased using available operations on nearby elements. Kept track of the best achievable frequency. ✔️ Time Complexity: O(N + M) - where M is the value range up to max(nums) + k. ✔️ Space Complexity: O(M) 🧠Key Takeaways: Prefix sums + frequency arrays are powerful for range-based computations. Always optimize loops around range-based frequency calculations. #Day153 #LeetCode #Arrays #PrefixSum #Frequency #Java #ProblemSolving #DSA #CodingChallenge #Algorithms #100DaysOfCode #InterviewPrep #SoftwareEngineering #DataStructures #CodeNewbie #ProgrammersLife
To view or add a comment, sign in
-
-
🔹 Day 40 – LeetCode Practice Problem: Find Greatest Common Divisor of Array (LeetCode #1979) 📌 Problem Statement: Given an integer array nums, find the greatest common divisor (GCD) of the smallest and largest numbers in the array. ✅ My Approach (Java): 1. Find the minimum and maximum elements in the array. 2. Starting from the smaller number and going downwards, check for the highest integer that divides both min and max. 3. Return that integer as the GCD. 📊 Complexity: Time Complexity: O(n + min(a, b)) Space Complexity: O(1) ⚡ Submission Results: Accepted ✅ Runtime: 0 ms (Beats 100%) 🚀 Memory: 43.41 MB (Beats 41.55%) 💡 Reflection: This problem shows how basic math logic and loop optimization can lead to extremely efficient solutions. A simple and powerful way to practice number theory in coding! #LeetCode #ProblemSolving #Java #DSA #CodingPractice #Learning
To view or add a comment, sign in
-
-
🚀 Day 414 of #500DaysOfCode 🔢 LeetCode 2536: Increment Submatrices by One (Medium) Today I worked on an interesting matrix manipulation problem that teaches an important optimization idea — 2D Difference Arrays. 🧠 Problem Summary We are given an n x n matrix initialized with zeros and a list of queries. Each query represents a submatrix, and we need to increment every element inside that submatrix by 1. A brute-force solution would update each cell for every query, which becomes inefficient when the matrix and number of queries grow. ⚡ Key Insight Instead of updating all cells directly, we use a 2D prefix / difference matrix technique: Update only the corners of each submatrix Convert the difference matrix into the final matrix using prefix sums Achieve O(n² + q) performance instead of O(q · n²) A neat trick that’s extremely useful for range update problems! ✅ Takeaways Difference arrays are not just for 1D — they scale beautifully to 2D Matrix prefix sums simplify many complex update operations Thinking in terms of range operations often leads to faster algorithms 💻 Tech Used Java 2D prefix sum optimization Matrix manipulation Another day, another concept mastered. On to the next challenge! 💪🔥 #coding #leetcode #java #programming #dsa #matrix #problemsolving #500DaysOfCode #Day414
To view or add a comment, sign in
-
-
✅ Day 63 of LeetCode Medium/Hard Edition Today’s challenge was “Count Vowel Permutation” — a fascinating Dynamic Programming problem that explores state transitions across vowels 🔤✨ 📦 Problem: Given an integer n, we need to count how many strings of length n can be formed using only vowels (a, e, i, o, u), following these adjacency rules: 'a' → may only be followed by 'e' 'e' → may only be followed by 'a' or 'i' 'i' → may not be followed by another 'i' 'o' → may only be followed by 'i' or 'u' 'u' → may only be followed by 'a' Since the result can be very large, return it modulo 10⁹ + 7. 🔗 Problem Link: https://lnkd.in/gW7GMVUh ✅ My Submission: https://lnkd.in/g8t7mddT 💡 Thought Process: The problem models a finite-state machine, where each vowel transitions only to specific next vowels. We can use Dynamic Programming with memoization to count all possible sequences of length n, given a last-used vowel. Steps: 1️⃣ Define a recursive DP: dp[n][vowel] = number of valid strings of length n ending with that vowel. 2️⃣ Use transition rules to decide valid next vowels for each step. 3️⃣ Memoize results to avoid recomputation and take modulo 10⁹ + 7. 4️⃣ The total number of strings = sum of all dp[n][vowel] values. ⚙️ Complexity: ⏱ Time: O(n × 5) — one state per vowel per length 💾 Space: O(n × 5) — or O(1) if optimized iteratively #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #ProblemSolving #Java #CodingChallenge #LeetCodeDay63
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