I need your help. I have the task :
- Given a List of Integer numbers, return the average of all odd numbers from the list or throw NoSuchElementException. But before that subtract 1 from each element on an odd position (having the odd index). This is my solution:
return numbers.stream().map(i -> numbers.indexOf(i) % 2 != 0 ? i - 1 : i).filter(i -> i % 2 != 0)
.mapToDouble(Integer::doubleValue).average().orElseThrow(NoSuchElementException::new);
Why do I subtract 1 from the element on an even position? I need only odd positions (odd indexes). This is the picture