Mean of Array using Recursion in 50 Days of Code Challenge

Day 40 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Mean of Array using Recursion We were given an array. Task was to find the mean (average). But using recursion. This one was a bit different. Not direct sum. It builds from smaller parts. 💻 Approach (Using Recursion) 🔹️Define a function mean(arr, n). 🔹️Base case → if n == 1, return arr[0]. 🔹️Recursively find mean of first n-1 elements. 🔹️Convert that mean back to sum → mean × (n-1). 🔹️Add current element arr[n-1]. 🔹️Divide total by n. Formula used: mean(arr, n) = (mean(arr, n-1) × (n-1) + arr[n-1]) / n This builds the answer step by step. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. 📚 What I learned today: ▫️Recursion can be used to build aggregate values step by step. ▫️We can convert mean back to sum using (mean × count). ▫️Understanding recurrence relation is important. ▫️Not all recursive problems are about printing or traversal. Day 40 completed. Getting deeper into recursion concepts 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic

To view or add a comment, sign in

Explore content categories