Day 46 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Geometric Sum using Recursion We were given an integer n. Task was to find the sum: 1 + 1/3 + 1/3² + ... + 1/3ⁿ Using recursion. 💻 Approach 🔹️Define a recursive function sum(n). 🔹️Base case: ▪️If n == 0 → return 1 🔹️Recursive case: ▪️Return sum(n-1) + 1/(3ⁿ) Each call adds one term. And moves toward smaller n. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. 📚 What I learned today: ▫️Recursion can be used for summation of series. ▫️Each recursive call adds one term to the result. ▫️Understanding base case is important to stop recursion. ▫️Power calculations are common in series problems. Day 46 completed. Getting more comfortable with recursive formulas 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Geometric Sum using Recursion in 50 Days of Code Challenge
More Relevant Posts
-
-- Solved LeetCode 100: Same Tree -- Today I worked on the classic binary tree problem “Same Tree”, and it turned out to be a great exercise in strengthening recursion fundamentals -- Problem Summary: -- Given two binary trees, determine whether they are identical in both structure and node values. -- Approach (Recursive DFS): At each node, I focused on 3 key checks: • If both nodes are NULL → trees match at this point • If one is NULL → structure mismatch • If values differ → not the same If all checks pass, recursively compare left and right subtrees. -- Complexity: Time: O(n) – visiting every node once Space: O(h) – recursion stack (depends on tree height) Consistency > Complexity. #LeetCode #DSA #Recursion #BinaryTree #CodingJourney #SoftwareEngineering #algorithm
To view or add a comment, sign in
-
-
🌲 Day 50/90: Cracking the Code on Binary Tree Views! We are officially past the halfway mark! 🚀 Today was all about changing perspectives—literally. Dealing with Binary Trees can get dizzying, but once you master the "views," the logic starts to feel like second nature. Here’s a breakdown of the logic I tackled today: 🔍 Problems Solved 1. Right Side View: Imagine standing to the right of the tree. Which nodes can you see? I used a modified DFS (Recursive) approach to prioritize the right child, ensuring I only grabbed the first node visible at each level. 2. Bottom-Left Most Element: This one is a classic. It’s not just the leftmost node; it’s the leftmost node on the very last level. A simple BFS (Level Order Traversal) made this a breeze. 3. Top View: This was the boss battle of the day! I used Vertical Level Order Traversal with a coordinate system (hd, level). By using a Map to store the first node encountered at each horizontal distance, I captured exactly what’s visible from the "sky." 💡 Key Takeaway: For tree "view" problems, Level Order Traversal is your best friend. If you can visualize the tree on a 2D coordinate plane, you can solve almost anything. Current Status: 50 days down, 40 to go. The momentum is real. #100DaysOfCode #DataStructures #Algorithms #BinaryTree #SoftwareEngineer #Vlog #CodingLife #LeetCode #TechJourney
To view or add a comment, sign in
-
Day 36 – #100DaysOfLeetCode Challenge Today I worked on the problem “Subarray Sum Equals K.” Problem Overview: Given an array of integers and an integer k, the task is to find the total number of continuous subarrays whose sum equals k. Example: Input: nums = [1, 1, 1], k = 2 Output: 2 Explanation: The subarrays [1,1] (from index 0→1 and 1→2) both sum to 2. Approach – Prefix Sum + HashMap To solve this efficiently: 🔹 Use a running sum (prefix sum) while iterating through the array 🔹 Store the frequency of prefix sums in a HashMap 🔹 At each step, check if (current_sum - k) exists in the map 🔹 If it exists, add its frequency to the count Time Complexity: O(n) Space Complexity: O(n) Key Learning: This problem highlights how prefix sums combined with hashing can drastically optimize problems involving subarrays and cumulative sums. Day 36 of my #100DaysOfLeetCode journey — staying consistent and improving problem-solving skills one day at a time!
To view or add a comment, sign in
-
-
Day#2 🚀 Solved: LeetCode 344 – Reverse String | Clean In-Place Solution Revisited one of the simplest yet most fundamental problems in string manipulation. 🔹 Problem: Reverse a character array in-place without using extra memory. 💡 Key Idea – Two Pointers • Start with one pointer at the beginning and one at the end • Swap elements and move inward • Continue until both pointers meet ⏱️ Complexity • Time: O(n) • Space: O(1) 🧠 What I learned : 🔹 Simplicity scales No need for extra data structures—clean in-place swapping does the job efficiently. 🔹 Patterns repeat Two-pointer technique shows up everywhere: strings, arrays, linked lists. 🔹 In-place thinking matters Optimizing space is often as important as optimizing time in real-world systems. 🔁 Sometimes revisiting “easy” problems strengthens the foundation for solving complex ones. #LeetCode #SoftwareEngineering #CodingInterview #Algorithms #DataStructures #ProblemSolving
To view or add a comment, sign in
-
🚀 Cracked one of the toughest 🔥 problems on LeetCode — Median of Two Sorted Arrays." 💡 Problem: Find the median of two sorted arrays in O(log (m+n)) time. 🧠 Key Insight: Instead of merging arrays (O(n)), I used Binary Search + Partitioning to split both arrays such that: ✔ Left half contains equal elements ✔ All left elements ≤ right elements ⚙️ Approach Highlights: ✅ Binary search on smaller array ✅ Handle edge cases using ±∞ ✅ Partition validation logic ✅ Works for both even & odd lengths 🔥 Journey: After 3 attempts, finally nailed this problem 💪 Each attempt taught something new — edge cases, partition logic, and precision. ⚡ Result: ✔ Accepted ✅ ⚡ Runtime: 0 ms (Beats 100%) 💻 Clean & optimal solution 🎯 What I learned: Thinking beyond brute force is key Binary search isn’t just for searching Edge cases define correctness 💬 Challenge for you: Can you solve this without merging arrays? 😉 #LeetCode #DataStructures #Algorithms #BinarySearch #CodingInterview #SoftwareEngineer #ProblemSolving #TechInterview #100DaysOfCode
To view or add a comment, sign in
-
-
Getting data out of a domain model or event stream into useful query models often becomes complicated. It's a common challenge to project domain state effectively for different query needs without overcomplicating things. A VirtualDDD session with Mathias Verraes and Eric Evans from back in 2020 explored this topic during a live coding talk. It demonstrated practical approaches to creating projections. The session focused on making these projections more expressive. The key idea was simplifying the process of transforming domain events or state into optimized read models, aiming for clarity and maintainability in your system's data representation. Watch the session: https://buff.ly/fTX4V1I #DomainDrivenDesign #EventSourcing #ReadModels #SoftwareArchitecture #LiveCoding
To view or add a comment, sign in
-
LeetCode Daily | Day 84 🔥 LeetCode POTD – 1559. Detect Cycles in 2D Grid (Medium) ✨ 📌 Problem Insight Given a 2D grid of characters: ✔ Move in 4 directions (up, down, left, right) ✔ Only move to cells with same value ✔ Detect a cycle of length ≥ 4 ✔ Cannot go back to immediate previous cell 🔍 Initial Thinking – Brute DFS ⚙️ 💡 Idea: ✔ Start DFS from every cell ✔ Track visited path ⚠️ Concern: ✔ Might revisit parent → false cycle ✔ Need a way to avoid backtracking confusion 💡 Key Observation 🔥 ✔ This is graph cycle detection in a grid ✔ While moving, track parent cell ✔ If we reach a visited cell ≠ parent → cycle found ✅ 🚀 Optimized Approach ✔ Use DFS with: → visited matrix → parent tracking (px, py) ✔ For each neighbor: → If not visited → continue DFS → If visited and not parent → cycle exists 🔧 Core Idea ✔ Grid = graph ✔ Same values = connected component ✔ Cycle detection = revisit node (not parent) ⏱ Complexity ✔ Time: O(m × n) ✔ Space: O(m × n) 🧠 Key Learning ✔ Always track parent in DFS cycle problems ✔ Grid problems often map to graph concepts ✔ Avoid false cycles by ignoring immediate back edge 🚀 Takeaway When dealing with grids, think like a graph — a small tweak like tracking parent turns a tricky problem into a standard cycle detection pattern ⚡ #LeetCode #DSA #Algorithms #GraphTheory #CPlusPlus #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 25/60 of Daily LeetCode Challenge Today I solved Recover a Tree From Preorder Traversal — a really interesting problem combining strings + recursion + trees. 💡 My Approach: At first, the traversal string looked confusing, but I broke it down step by step. I noticed that: The number of dashes (-) represents the depth of the node The actual number after dashes is the node value So I decided to simulate the tree construction using recursion. 🔹 Step 1: Parse Depth and Value I wrote a helper to count dashes → gives me the depth Then extracted the full number (node value) from the string 🔹 Step 2: Recursive Tree Building I used recursion where I pass the expected depth If the current node’s depth doesn’t match, I return NULL Otherwise: Create the node Move index forward Recursively build left child first, then right child 🔹 Step 3: Maintain Index I kept a global/current index so I always know where I am in the string After processing a node, I update index accordingly 🧠 Key Insight: The important idea is: -> Depth (number of dashes) controls where the node should be placed in the tree Also, preorder means: -> Node → Left → Right, so I followed the same order in recursion ⏱️ Complexity: Time: O(n) (each character processed once) Space: O(h) (recursion stack) #Day25 #LeetCode #BinaryTree #Recursion #DSA #CodingJourney
To view or add a comment, sign in
-
-
After recently using the reversal trick to rotate arrays in O(1) space, I realized the exact same logic applies beautifully to strings. Today I tackled LeetCode 151: Reverse Words in a String in C++. Instead of taking the easy route by allocating extra memory to split the string into a new array of words, I solved it entirely in-place. The strategy: 1️⃣ Reverse the entire string. 2️⃣ Reverse each individual word back to its correct orientation. 3️⃣ Use a two-pointer pass to clean up any leading, trailing, or multiple spaces. Connecting the dots between different data structures and seeing how array pointer techniques translate perfectly to strings is incredibly rewarding. #LeetCode #Cpp #DataStructures #Algorithms #TwoPointers
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