Erlang & Rust | 0ms / Optimal
IntuitionThe problem's simplicity makes it a good choice for practicing with a more exotic language like Erlang. I started by solving it in Rust, then translate
# Intuition
The problem's simplicity makes it a good choice for practicing with a more exotic language like Erlang. I started by solving it in Rust, then translated the gist of my rust solution into Erlang. 

# Complexity
- Time complexity: $O(N)$ (both solutions)

# Performance 
| **Language** | **Runtime (Beats %)** | **Memory (Beats %)** | 
|:---|---:|---:|
| **Rust** | `0ms` (100%) | `2.3 MB` (68.12%) | 
| **Erlang** | `9ms` (100%)| `59.70 MB` (100%) | 

# Code
```erlang []
-spec sum_of_good_numbers(Nums :: [integer()], K :: integer()) -> integer().

sum_of_good_numbers(Nums, K) ->
    % FoldInit is the initial values for the {Lmap, Rmap, Idx} vars
    FoldInit = {maps:new(), maps:new(), 1},
    
    % We map each element in Nums to its left and right pairs
    {Lmap, Rmap, _} = lists:foldl(
        fun(Num, {Lmap, Rmap, Idx}) ->

            % We'll map the IDX-K'th index to Num in Lmap
            Left = Idx - K,

            % And the Idx+K'th index to Num in Rmap
            Right = Idx + K,

            % And keep doing that until all the numbers are mapped
            { maps:put(Right, Num, Lmap), maps:put(Left, Num, Rmap), Idx + 1 }
        end, FoldInit, Nums
    ),

    % Next, we iterate again, using the Lmap and Rmap values 
    % just created. We start the sum at 0 and the index at 1 as before.
    FoldNext = {0, 1, Lmap, Rmap},
    {Result, _, _, _ } = lists:foldl(
        fun(Num, {Sum, Idx, Lmap, Rmap}) ->

            % The left side is good if it doesn't exist ("not maps...")
            % or if it's greater than the mapped left side value.
            % 
            % The default value "1001" is based on the problem's 
            % value range (1 to 1000) and prevents false positives
            % and bad-key exceptions.
            LeftGood  = (not maps:is_key(Idx, Lmap)) or (Num > maps:get(Idx, Lmap, 1001)),

            % The right side is good if it doesn't exist ("not maps...")
            % or if it's greater than the mapped right side value.
            RightGood = (not maps:is_key(Idx, Rmap)) or (Num > maps:get(Idx, Rmap, 1001)),

            % If both left and good and right are good (true case),
            % we add Num to Sum. Otherwise, we keep just Sum. 
            case LeftGood and RightGood of 
                true -> 
                    { Sum + Num, Idx + 1, Lmap, Rmap };
                false -> 
                    { Sum,       Idx + 1, Lmap, Rmap }
            end 
        end, FoldNext, Nums
    ),

    % Finally, we return the result of the sum operation.
    Result.

```

```rust []
impl Solution {
    pub fn sum_of_good_numbers(nums: Vec<i32>, k: i32) -> i32 {
        
        let n = nums.len();

        // convert k to usize for correct comparisons
        let k = k as usize;

        // we'll track the sum as we iterate
        // over the array
        let mut sum = 0;

        'sum_good_nums:
        for i in 0..n {

            // left is good if: 
            //      i - k does not exist
            //      nums[i] is strictly greater than nums[i-k] 
            let lg = (k   >  i) || nums[i] > nums[i-k];

            // right is good if: 
            //      i + k does not exist
            //      nums[i] is strictly greater than nums[i+k] 
            let rg = (k+i >= n) || nums[i] > nums[i+k];

            // If nums[i] looks good from both the left and 
            // right side, add it to the sum.
            if lg && rg {
                sum += nums[i];
            }
        }

        sum
    }
}
```
['Array', 'Rust', 'Erlang']