Problem Description
Given an integer array arr and a function fn, return a new array where each element at index i is the result of applying fn to arr[i] and the index i.
Key Insights
- Iterate through the array using a loop.
- For each element, apply the provided function with the element and its index as parameters.
- Append the result to a new array.
- Avoid using built-in mapping methods (like Array.map).
Space and Time Complexity
Time Complexity: O(n) - where n is the number of elements in the array, since we iterate through all elements once.
Space Complexity: O(n) - as we create a new array to store the transformed elements.
Solution
The solution involves iterating through the input array using a simple loop. For each element at index i, call the function fn with the element and its index. Store the result in a newly created array. Finally, return the new array containing all transformed elements. This approach uses basic looping and conditional execution without relying on higher-order built-in methods, making it straightforward and efficient.