Java || Conditional Statements || Beats 100%
Complexity
Time complexity: O(n)

Space complexity: O(1)
Code


# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int sumOfGoodNumbers(int[] nums, int k) {
        int sum = 0;
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            boolean leftExists = (i - k) >= 0;
            boolean rightExists = (i + k) < n;

            if (!leftExists && !rightExists) {
                sum += nums[i];
            } 
            else if (!leftExists) {
                if (nums[i] > nums[i + k]) sum += nums[i];
            } 
            else if (!rightExists) {
                if (nums[i] > nums[i - k]) sum += nums[i];
            } 
            else {
                if (nums[i] > nums[i - k] && nums[i] > nums[i + k]) sum += nums[i];
            }
        }
        return sum;
    }
}

```
['Java']