We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Anyone knows why using a parallel stream results in incorrect results for some tests, even if I use a synchronized list? Shouldn't the list prevent this?
publicstaticlongarrayManipulation(intn,List<List<Integer>>queries){// Write your code here List<Long>sums=Collections.synchronizedList(Arrays.asList(newLong[n+1]));Collections.fill(sums,0L);queries.stream().parallel().forEach(query->{inta=query.get(0);intb=query.get(1);intk=query.get(2);sums.set(a-1,sums.get(a-1)+k);sums.set(b,sums.get(b)-k);});longmax=0;longsum=0;for(longi:sums.subList(0,n)){sum+=i;max=Math.max(max,sum);}returnmax;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Anyone knows why using a parallel stream results in incorrect results for some tests, even if I use a synchronized list? Shouldn't the list prevent this?