I have been practicing java 8 streams and functional style for a while. Sometimes I try to solve some programming puzzles just using streams. And during this time I found a class of tasks which I don't know how to solve with streams, only with classical approach.
One example of this kind of tasks is:
Given an array of numbers find index of the element which will make sum of the left part of array below zero.
e.g. for array [1, 2, 3, -1, 3, -10, 9]
answer will be 5
My first idea was to use IntStream.generate(0, arr.length)...
but then I don't know how to accumulate values and being aware of index same time.
So questions are:
- Is it possible to somehow accumulate value over stream and then make conditional exit?
- What is then with parallel execution? it's not fitting to problem of finding indexes where we need to be aware of elements order.