🌸 Day 58 of #100DaysOfCode 🌸 💡 Problem: Alice and Bob Playing Flower Game ✨ Approach: Used a mathematical logic approach instead of simulation — counting even–odd combinations between Alice and Bob’s flowers 🌼 If one has an even number and the other an odd, the total number of valid pairs determines the winning outcomes. No loops. No extra space. Just pure math elegance! 🧮 ⚡ Complexity Analysis: Time Complexity: O(1) → direct formula-based computation Space Complexity: O(1) → constant space 📊 Performance: ✅ Runtime: 0 ms (Beats 💯%) ✅ Memory: 40.48 MB (Beats 93.79%) 🔑 Key Insight: Sometimes the smartest solutions don’t need iterations — just mathematical reasoning. Optimizing logic can turn complex problems into one-liners! ⚙️💫 #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #MathLogic #CodingChallenge #Algorithm #CodeOptimization #ProgrammingJourney
"100DaysOfCode: Solving Flower Game with Math Logic"
More Relevant Posts
-
🧮 Day 27 – 90 Days of Code Problem: Count Operations to Obtain Zero 🧠 Intuition This problem is conceptually similar to the Euclidean Algorithm for finding the GCD. At each step, we repeatedly subtract the smaller number from the larger one until one becomes zero. Instead of doing subtraction one by one, we can optimize using integer division — num1 / num2 tells us how many subtractions can be done in one go. So effectively, we’re simulating the Euclidean process while counting how many subtraction steps occur in total. ⚙️ Approach Initialize a counter count = 0. While both numbers are positive: Add num1 / num2 to the counter (number of subtractions possible in this step). Update num1 = num1 % num2 (reduce the larger number). Swap num1 and num2 to continue with the next iteration. Once either number becomes 0, stop and return the count. ⏱️ Complexity Time Complexity: O(log(min(num1, num2))) — similar to the Euclidean algorithm. Space Complexity: O(1) 💡 Key Learning Optimized repetitive subtraction problems using division-based reasoning — an elegant example of mathematical insight improving algorithmic efficiency. 🔖 Tags #Day27Of90 #90DaysOfCode #LeetCode #LeetCodeDaily #DSA #Maths #EuclideanAlgorithm #ProblemSolving #CodingJourney #KeepLearning #ConsistencyIsKey #Java #Programming
To view or add a comment, sign in
-
-
🚀 Day 413 of #500DaysOfCode 💡 Problem: 2654. Minimum Number of Operations to Make All Array Elements Equal to 1 🧩 Difficulty: Medium Today’s challenge was an interesting mix of GCD logic and array transformation! We’re given an array of positive integers and can repeatedly pick adjacent elements — replacing one of them with their GCD. Our goal? Make all elements equal to 1 in the minimum number of operations. 🧠 Key Idea: If there’s already a 1 in the array → just need (n - countOf1s) operations. Otherwise, find the shortest subarray whose GCD equals 1. It takes (len - 1) steps to make the first 1. Then (n - 1) steps to spread that 1 across the array. If no subarray has GCD = 1 → return -1. 📘 Concepts Used: Greatest Common Divisor (GCD) Sliding subarray search Mathematical optimization ✨ This problem reinforced how number theory meets logic in coding — sometimes the simplest mathematical idea (like GCD) leads to elegant solutions! #Day413 #LeetCode #Java #ProblemSolving #CodingChallenge #LearnEveryday #500DaysOfCode #MathInCoding
To view or add a comment, sign in
-
-
🚀 Day 61 of My LeetCode Journer 🚀 🚀 Problem: Transpose Matrix 🧠 Concept: Matrix Manipulation 🔹 Approach: We are given a 2D matrix and need to return its transpose — where rows become columns. It’s a straightforward problem that strengthens our understanding of nested loops and 2D array traversal. 💡 Key Takeaways: Practice of nested loops and array indices Useful concept in image processing, data transformations, and linear algebra #Day61 #LeetCode #Java #DSA #Matrix #CodingChallenge #100DaysOfCode #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🔥 Day 113 of My DSA Challenge – Word Search 🔷 Problem : 79. Word Search 🔷 Goal : Check if a given word exists in a 2D grid by connecting sequentially adjacent letters (horizontally or vertically). Same cell cannot be used more than once. 🔷 Key Insight : This is a DFS + backtracking problem. Start from every cell matching the first character. Explore all 4 directions recursively. Track visited cells to avoid reusing letters. Backtrack when a path fails. This teaches how to explore grids efficiently using recursion and state tracking. 🔷 Approach : 1️⃣ Iterate over all cells to find starting points 2️⃣ DFS recursively for adjacent letters 3️⃣ Use a visited matrix to avoid reusing cells 4️⃣ Backtrack if the path does not lead to the word Time Complexity: O(m × n × 4^k) where k = length of word Space Complexity: O(m × n) for visited + recursion stack Word Search reinforces grid DFS + backtracking patterns. Explore all possibilities, track state carefully, backtrack on failure. This pattern is useful for : ✅ Maze solving ✅ Island counting problems ✅ Pathfinding & puzzle problems Every day, a new pattern becomes familiar. Step by step, the mind gets sharper. 🚀 #Day113 #DSA #100DaysOfCode #DFS #Backtracking #LeetCode #ProblemSolving #Java #CodingChallenge #Algorithms #DataStructures #DeveloperJourney
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 1513. 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐒𝐮𝐛𝐬𝐭𝐫𝐢𝐧𝐠𝐬 𝐖𝐢𝐭𝐡 𝐎𝐧𝐥𝐲 1𝐬 Today’s problem was a clean exercise in mathematical pattern recognition within strings — proving that sometimes, counting smartly beats looping endlessly. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given a binary string and need to count the number of substrings consisting entirely of '1's. Since the count can be large, the result is returned modulo 10⁹ + 7. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: The core insight is recognizing how consecutive '1's form substrings: For a group of k consecutive '1's, the number of valid substrings = k * (k + 1) / 2. Instead of explicitly using this formula, I used a simple iterative approach: Keep a running counter for consecutive '1's. Add that counter to the result whenever we encounter another '1'. Reset the counter on a '0'. This yields an elegant O(n) single-pass solution with constant space. 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n) Space Complexity: O(1) 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Mathematical series reasoning helps simplify substring problems. Consecutive counting avoids redundant substring generation. Simplicity and pattern recognition often lead to the most optimal logic. A great reminder that not every problem needs complexity — sometimes, it’s all about spotting the sequence. #LeetCode #DSA #Java #ProblemSolving #Algorithms #CodingPractice #LearningJourney #100DaysOfCode #Consistency #BinaryStrings
To view or add a comment, sign in
-
-
Day 71/160 — Detect Loop in Linked List 🔁 Today’s challenge was about finding cycles in a linked list — a classic problem that teaches how to spot infinite loops in data structures. 💡 Approach: Used Floyd’s Cycle Detection Algorithm (Tortoise & Hare) — Move one pointer one step at a time and another two steps. If they ever meet, there’s a loop! ⚙️ Complexity: O(n) time | O(1) space Slow and steady meets fast and furious 🐢🐇 — and the loop is caught! #Day71 #160DaysOfChallenge #DSA #Coding #LinkedList #Algorithms #Java #FloydCycleDetection #LearnToCode
To view or add a comment, sign in
-
-
🔥 Day 108 of My DSA Challenge – Merge Sorted Array 🔷 Problem : 88. Merge Sorted Array 🔷 Goal : Merge two sorted arrays into one sorted array in-place inside nums1. The tricky part? nums1 already has extra space at the end — and we need to fill it without using extra arrays. 🔷 Key Insight : Use the two-pointer technique starting from the end — this avoids overwriting values in nums1. Pointer p1 → end of valid elements in nums1 Pointer p2 → end of nums2 Pointer idx → last index in final merged array Place the largest element first by comparing from the end. 🔷 Approach : 1️⃣ Start from the last elements of both arrays 2️⃣ Compare and place the greater one at the end 3️⃣ Move pointers backwards 4️⃣ If nums2 still has elements, copy them This gives us O(m + n) time and O(1) extra space ✅ Sometimes the most elegant solutions are not about building new structures but smartly reusing what you already have. Two-pointer technique continues to be a powerful tool in array problems 💪 #Day108 #100DaysOfCode #LeetCode #DSA #Java #TwoPointers #Arrays #CodingChallenge #Algorithm #InPlaceAlgorithms #ProblemSolving #Programming #SoftwareEngineering #TechJourney #DeveloperJourney #CodeEveryday #LearnDSA #GrowthMindset #EngineerMindset #LogicBuilding
To view or add a comment, sign in
-
-
🔥 Day 109 of My DSA Challenge – Intersection of Two Arrays 🔷 Problem : 349. Intersection of Two Arrays 🔷 Goal : Return the unique intersection of two integer arrays. Order doesn’t matter, but duplicates should not appear in the result. 🔷 Key Insight : This is a set / hashmap based lookup problem. We store elements from the first array, then check if elements from the second array exist in it. To ensure uniqueness, we remove the element once counted, so duplicates don't appear. 🔷 Approach : 1️⃣ Add all elements of nums1 to a HashMap 2️⃣ Traverse nums2 3️⃣ If the element exists in the map → add to result & remove from map 4️⃣ Convert list to array and return Time Complexity: O(n + m) Space Complexity: O(n) Sometimes the simplest tools — like HashMaps/Sets — give the cleanest solutions. Learning to choose the right data structure = faster logic + cleaner code 💪 #Day109 #100DaysOfCode #LeetCode #DSA #Java #HashMap #DataStructures #CodingChallenge #Algorithm #ProblemSolving #Programming #SoftwareEngineering #TechJourney #DeveloperJourney #CodeEveryday #LearnDSA #LogicBuilding #GrowthMindset #EngineerMindset
To view or add a comment, sign in
-
✨ Day 101 of My DSA Challenge – Magnetic Force Between Two Balls 🔷 Problem: 1552. Magnetic Force Between Two Balls 🔷 Goal: Place m balls into sorted basket positions such that the minimum magnetic force (i.e., minimum distance between any two balls) is as large as possible. 🔷 Key Insight: This is another brilliant Binary Search on the Answer problem. Instead of directly computing distances, we binary search for the maximum possible minimum distance that still allows placing all balls. Here’s how the logic works: 1️⃣ Sort all basket positions. 2️⃣ Use binary search on the range of possible distances (0 to max(position) - min(position)). 3️⃣ For each middle distance mid, check feasibility using a greedy approach — place balls while maintaining at least mid distance apart. 4️⃣ If it’s possible → try a larger distance. If not → reduce the distance. 🔷 My Java Approach: Implemented a helper function isPossible() to check if we can place all balls for a given minimum distance. Used Binary Search to maximize that minimum distance. 🔷 Complexity: Time → O(n × log(maxDist)) Space → O(1) This problem beautifully blends sorting, binary search, and greedy placement — showing how abstract concepts like “searching on the answer” translate into real algorithmic reasoning. Every new day strengthens my foundation in Binary Search patterns, sharpening both logic and implementation clarity. 🚀 #Day101 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #BinarySearch #GreedyAlgorithm #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #LearnToCode #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
To view or add a comment, sign in
-
-
💡 (14th Nov 2025) Arrays — Part 6: Trapping Rain Water (Code) & Best Time to Buy & Sell Stock Today I worked on two classical array problems that test both implementation skills and algorithmic thinking: 1️⃣ Trapping Rain Water — Code • Implemented the two-pointer / precomputation approach to compute trapped water between bars. • Important ideas: left-max & right-max, or optimized two-pointer scanning to achieve O(n) time and O(1) extra space. • Why it matters: forces you to think about how to derive global results from local information. 2️⃣ Best Time to Buy & Sell Stock • Solved using a single-pass approach that tracks the minimum price so far and the maximum profit achievable. • Time Complexity: O(n), Space: O(1). • Why it matters: demonstrates how simple greedy logic can replace brute-force checks. Takeaway: Both problems highlight the same principle: identify the right state to track (left-max, right-max, min-price, current-profit) and update it efficiently. These patterns reappear across many array problems and interview questions. Apna College Shradha Khapra #DSA #Arrays #Coding #Algorithms #Java #ProblemSolving #Kadane #TrappingRainwater #StockProblem #LearningInPublic
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