Day 43 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Factorial of a Number We were given an integer n. Task was to find n! Factorial means: n! = n × (n-1) × (n-2) … × 1 Example: 5! = 5 × 4 × 3 × 2 × 1 = 120 💻 Iterative Approach 🔹️Initialize result = 1. 🔹️Run a loop from 1 to n. 🔹️Multiply each number with result. 🔹️Return final result. Simple loop. Easy to understand. Time Complexity: O(n) Space Complexity: O(1) 💻 Recursive Approach 🔹️Base case: ▪️If n == 0 or n == 1 → return 1 🔹️Recursive case: ▪️Return n × factorial(n-1) Breaks problem into smaller parts. Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. 📚 What I learned today: ▫️Recursion is based on defining smaller subproblems. ▫️Base case is necessary to stop recursion. ▫️Iterative approach is more space efficient. ▫️Same problem can be solved in multiple ways. Day 43 completed. Strengthening recursion vs iteration concepts 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Day 43: Factorial of a Number Solution
More Relevant Posts
-
Day 48 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Next Greater Element We were given an array. For each element, we had to find the next greater element on the right side. If none exists → return -1. Example: [4, 5, 2, 10] → [5, 10, 10, -1] 💻 Brute Force Approach 🔹️For each element, check all elements on the right. 🔹️Find the first greater element. 🔹️If found → store it. 🔹️Else → store -1. Time Complexity: O(n²) Space Complexity: O(1) Works. But slow. 💻 Optimal Approach (Using Stack) 🔹️Traverse from right to left. 🔹️Use a stack to store elements. 🔹️For each element: ▪️Pop smaller elements from stack ▪️Top of stack = next greater element ▪️If stack empty → answer is -1 🔹️Push current element into stack Efficient. Single pass. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Monotonic stack helps in nearest greater problems. ▫️Traversing from right simplifies logic. ▫️Popping smaller elements keeps stack useful. ▫️Stack stores only potential answers. Day 48 completed. Stack patterns getting stronger 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 94 of #365DaysOfLeetCode Challenge Today’s focus: **Mastering Greedy + Stack Patterns** After solving problems like *Remove K Digits*, one thing is becoming clear — a lot of “hard-looking” problems become easier when you recognize the **pattern behind them**. 💡 **Today’s Learning:** Instead of jumping into code, I spent time identifying: * When to use **Greedy** * When a **Stack** can simplify decisions * How to think in terms of **local vs global optimum** 📌 **Key Insight:** Many problems follow this idea: 👉 *“If the current choice is better than the previous one, fix it immediately.”* This applies to: * Removing digits (monotonic stack) * Stock span problems * Next greater/smaller elements * Histogram problems ⚡ **Mindset Upgrade:** Earlier → “Try all possibilities” Now → “What is the best decision I can make right now?” **What I learned today:** Data Structures are not just tools — they are **decision-making frameworks**. Day 94 done The journey is slowly shifting from *solving problems* → to *thinking like an engineer*. #LeetCode #DSA #CodingChallenge #Greedy #Stack #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
Day 49 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove All Adjacent Duplicates in String We were given a string s. Task was to remove all adjacent duplicates. If duplicates are removed, new duplicates may form. So we repeat until no duplicates remain. Example: "abbaca" → "ca" 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Traverse each character. 🔹️If stack is not empty and top == current character → pop 🔹️Else → push current character 🔹️At the end, build string from stack Duplicates get removed automatically. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack helps in handling adjacent comparisons. ▫️Removing elements can create new patterns. ▫️Single pass solution is possible with stack. ▫️String problems often map to stack operations. Day 49 completed. Almost at 50 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 LeetCode Day Problem Solving 🚀 Day-49 📌 Problem: Given a circular array of strings and a target string, find the minimum distance needed to reach the target starting from a given index. You can move: ✔ One step forward (right) ✔ One step backward (left) Since the array is circular, moving beyond ends will wrap around. 🧠 Example: Input: words = ["hello","i","am","leetcode","hello"] target = "hello", startIndex = 1 ✅ Output: 1 📖 Explanation: From index 1, shortest way is: ➡ Move left 1 step → reach "hello" 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - start|, n - |i - start|) ✔ Check all indices where words[i] == target ✔ Take the minimum distance among them ✔ If target not found → return -1 ⚡ Approach: Traverse the array For every match with target: Compute circular distance Return the minimum 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Handling circular arrays efficiently ✔ Optimizing distance using modular thinking ✔ Simple traversal with smart math 💡 ✅ Day 49 Completed 🚀 Sharpening skills in Arrays + Optimization Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 Day 53 of #100DaysOfCode — Mastering In-Place Problem Solving Today’s focus: LeetCode #48 — Rotate Image🧠 At first glance, this problem looks simple… until you hit the constraint: 👉 You must rotate the matrix in-place (no extra space allowed). That’s where the real learning begins. 💡 What I learned today: * Difference between brute force vs optimized approach * Why space complexity matters in real-world systems * How to break a problem into steps: 1. Transpose the matrix 2. Reverse each row * Improved understanding of 2D array manipulation 🔍 Key Insight: Most problems aren’t about coding — they’re about *thinking in transformations. Once you visualize the pattern, the solution becomes elegant. 💻 Tech I’m sharpening daily: * Data Structures & Algorithms (DSA) * Problem-solving mindset * Writing clean & optimized C++ code 📈 Consistency Update: Even on days when it feels tough or slow, I’m showing up. Because growth isn’t about speed — it’s about discipline. 🎯 Goal: Become a strong problem solver ready for real-world engineering challenges. If you're also on a coding journey, let’s connect and grow together 🤝 #Day53 #100DaysOfCode #DSA #LeetCode #CodingJourney #SoftwareEngineering #ProblemSolving #CPlusPlus #TechCareers #LearningInPublic #Consistency #FutureEngineer
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-29 📌 Problem: Given a circular array of strings and a target string, find the minimum distance needed to reach the target starting from a given index. You can move: ✔ One step forward (right) ✔ One step backward (left) Since the array is circular, moving beyond ends will wrap around. 🧠 Example: Input: words = ["hello","i","am","leetcode","hello"] target = "hello", startIndex = 1 ✅ Output: 1 📖 Explanation: From index 1, shortest way is: ➡ Move left 1 step → reach "hello" 💡 Key Insight: ✔ Since array is circular, distance = 👉 min(|i - start|, n - |i - start|) ✔ Check all indices where words[i] == target ✔ Take the minimum distance among them ✔ If target not found → return -1 ⚡ Approach: Traverse the array For every match with target: Compute circular distance Return the minimum 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Handling circular arrays efficiently ✔ Optimizing distance using modular thinking ✔ Simple traversal with smart math 💡 ✅ Day 29 Completed 🚀 Sharpening skills in Arrays + Optimization Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 Tackling Interval Intersection Problems – From My Calendar I to My Calendar III Over the past 2 days, I explored a fascinating progression of problems on LeetCode that sharpened my understanding of interval intersections and introduced me to a powerful new technique: the line sweep algorithm. LeetCode 729: My Calendar I The challenge was simple yet elegant: prevent double bookings. It taught me the basics of handling intervals and checking overlaps. LeetCode 731: My Calendar II Things got trickier—now I had to prevent triple bookings. This forced me to think about storing not just events, but also overlaps, and carefully tracking intersections. LeetCode 732: My Calendar III The real leap came here. After each booking, I had to return the maximum number of simultaneous overlaps (k-bookings). This is where the line sweep algorithm came into play. By treating each start time as +1 and each end time as -1, and sweeping through sorted events, I could efficiently track active bookings and compute the maximum overlap. I utilized TreeMap for storing the intervals in a sorted order and fast retrieval. ✨ What I learned: How interval problems scale in complexity when constraints change. Why event sorting + active count tracking is such a powerful paradigm. How algorithms like line sweep transform brute-force checks into elegant, efficient solutions. 💡 Key takeaway: Sometimes the best way to tackle a hard problem is to build up gradually—start with simpler versions, understand the core mechanics, and then scale up. My Calendar I and II laid the foundation; My Calendar III unlocked a new algorithmic perspective. 🔗 If you’re exploring interval problems, I highly recommend this sequence. It’s a great way to grasp intersections, overlaps, and the beauty of sweep line algorithms. #LeetCode #DataStructuresAndAlgorithms #CodingJourney #JavaProgramming #AlgorithmDesign #LineSweepAlgorithm #TreeMap #IntervalProblems #CompetitiveProgramming #CodingChallenge #TechTalk #InnovationThroughCode #BuildInPublic #LearnShareGrow
To view or add a comment, sign in
-
-
The Day's Particulars: #Day15 of the LeetCode Challenge 💠 Problems Conquered: Palindrome Linked List & Remove Nodes From Linked List 💠 Patterns Explored: Code Reusability, The DRY Principle, Linked List Reversal 💠 Future Notes to Self: A well-written helper method is worth its weight in gold. Writing clean, modular code means you can solve multiple complex problems with the exact same foundational logic. 𝐃𝐞𝐚𝐫𝐞𝐬𝐭 𝐆𝐞𝐧𝐭𝐥𝐞 𝐑𝐞𝐚𝐝𝐞𝐫𝐬, We have officially reached the halfway mark of this 30-day journey! Day 15 was less about learning a brand new algorithm and more about practical software engineering: the power of code reusability. Today's agenda featured two distinct challenges: 𝘗𝘢𝘭𝘪𝘯𝘥𝘳𝘰𝘮𝘦 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵 𝘢𝘯𝘥 𝘙𝘦𝘮𝘰𝘷𝘦 𝘕𝘰𝘥𝘦𝘴 𝘍𝘳𝘰𝘮 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵. For the Palindrome problem, the most optimal O(1) space approach is to find the middle of the list using slow and fast pointers, reverse the second half, and then compare the two halves. For the Remove Nodes problem, where you must delete any node that has a greater value to its right, the logic becomes trivial if you process the list backward. Since we only have forward pointers in a singly linked list, the solution was to reverse the entire list, iterate through to keep track of the maximum value (dropping any smaller nodes), and then reverse it back to its original state. The highlight of my day was not just securing the green "Accepted" checkmarks, but realizing that the exact same reverse() helper method was the MVP for both problems. Instead of rewriting logic, I simply extracted the list-reversal code into its own method and called it whenever needed. It was a great practical reminder of the DRY (Don't Repeat Yourself) principle. Build strong, modular pieces, and you can assemble them to solve almost anything. Halfway there, and the momentum is going strong! Yours in code, Saumya #LeetCode #DailyCoding #LinkedList #CodeReusability #JavaDeveloper #LadyWhistledown #SoftwareEngineering #dsa_stories #30daysOfCode #TechCommunity #DRYPrinciple #DataStructures #Algorithms
To view or add a comment, sign in
-
-
Just crossed 100 problems solved on LeetCode. Not impressive by itself—but what matters is consistency. Most of these came from sticking to fundamentals like arrays, two pointers, and basic problem patterns instead of jumping around randomly. Current focus: • Strengthening problem-solving patterns • Reducing time per problem • Moving from Easy → Medium consistently Still a long way to go. Next target: 200 with stronger Medium coverage. If you're grinding LeetCode too, focus less on quantity and more on pattern recognition—it compounds faster. #LeetCode #DSA #CodingJourney #ProblemSolving #SoftwareEngineering
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