My Day Learning Arrays with Recursion

ยท

2 min read

Today was a productive day of learning how to tackle array problems using recursion!

I started with a warm-up by revisiting linear search and binary search using recursion. These concepts were familiar from previous explorations, so they served as a good foundation.

Next, I tackled a couple of easier problems: determining whether an array is sorted and finding duplicate numbers within an array. For the duplicates, I initially used an ArrayList to store them. However, I soon realized the power of problem-solving flexibility.

Instead of creating the list outside the function, I learned to receive the list as an argument. This might sound simple, but it made me think about the thought process of how to handle and return the values within the function.

However, I also stumbled upon a less efficient approach. I created a new list within the function itself, meaning a new list would be created with every function call. This approach, while seemingly straightforward, had a significant drawback: poor space complexity.

By visualizing the process, I realized that passing the list as an argument is the most effective solution. Each function call builds upon the existing list, avoiding unnecessary memory allocation for new lists with each call.

Overall, this was a valuable experience in exploring different approaches to solving the same problem. It highlighted the importance of considering space complexity alongside logic. Additionally, visualizing the flow of data helped me understand how to effectively return and add values within the recursive structure.

This journey not only expanded my knowledge of recursion but also encouraged me to approach problems from multiple angles, leading to more efficient and elegant solutions.

Did you find this article valuable?

Support Pratik Kale by becoming a sponsor. Any amount is appreciated!

ย