2626. Array Reduce Transformation
Easy
274
9

Given an integer array `nums`, a reducer function `fn`, and an initial value `init`, return a reduced array.

reduced array is created by applying the following operation: `val = fn(init, nums)`, `val = fn(val, nums)``val = fn(val, nums)``...` until every element in the array has been processed. The final value of `val` is returned.

If the length of the array is 0, it should return `init`.

Please solve it without using the built-in `Array.reduce` method.

Example 1:

```Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr; }
init = 0
Output: 10
Explanation:
initially, the value is init=0.
(0) + nums = 1
(1) + nums = 3
(3) + nums = 6
(6) + nums = 10
```

Example 2:

```Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr * curr; }
init = 100
Output: 130
Explanation:
initially, the value is init=100.
(100) + nums^2 = 101
(101) + nums^2 = 105
(105) + nums^2 = 114
(114) + nums^2 = 130
```

Example 3:

```Input:
nums = []
fn = function sum(accum, curr) { return 0; }
init = 25
Output: 25
Explanation: For empty arrays, the answer is always init.
```

Constraints:

• `0 <= nums.length <= 1000`
• `0 <= nums[i] <= 1000`
• `0 <= init <= 1000`
Accepted
26.8K
Submissions
30.6K
Acceptance Rate
87.6%

Seen this question in a real interview before?
1/4
Yes
No

Discussion (0)