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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Java
  3. Data Structures
  4. Java Subarray

Java Subarray

Problem
Submissions
Leaderboard
Discussions
Editorial

We define the following:

  • A subarray of an -element array is an array composed from a contiguous block of the original array's elements. For example, if , then the subarrays are , , , , , and . Something like would not be a subarray as it's not a contiguous subsection of the original array.
  • The sum of an array is the total sum of its elements.
    • An array's sum is negative if the total sum of its elements is negative.
    • An array's sum is positive if the total sum of its elements is positive.

Given an array of integers, find and print its number of negative subarrays on a new line.

Input Format

The first line contains a single integer, , denoting the length of array .
The second line contains space-separated integers describing each respective element, , in array .

Constraints

Output Format

Print the number of subarrays of having negative sums.

Sample Input

5
1 -2 4 -5 1

Sample Output

9

Explanation

There are nine negative subarrays of :

Thus, we print on a new line.

Author

Shafaet

Difficulty

Easy

Max Score

10

Submitted By

108874

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy