You are viewing a single comment's thread. Return to all comments →
Not even using arrays at all. Java 8 makes it just too easy...
public static void main(String[] args) { try (Scanner input = new Scanner(System.in)) { input.nextInt(); List<Integer> numbers = new ArrayList<>(); while (input.hasNextInt()) { numbers.add(input.nextInt()); } System.out.println(getAllContiguousSubarrays(numbers) .stream() .filter(list -> list.stream() .mapToInt(e -> e) .sum() < 0) .count()); } } private static List<List<Integer>> getAllContiguousSubarrays(List<Integer> list) { List<List<Integer>> result = new ArrayList<>(); for (List<Integer> frontTruncatedSubList : IntStream.range(0, list.size()) .mapToObj(i -> list.subList(i, list.size())) .collect(Collectors.toList())) { result.addAll(IntStream.range(0, frontTruncatedSubList.size()) .mapToObj(i -> frontTruncatedSubList.subList(0, frontTruncatedSubList.size() - i)) .collect(Collectors.toList())); } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Subarray
You are viewing a single comment's thread. Return to all comments →
Not even using arrays at all. Java 8 makes it just too easy...