Skip to content

Latest commit

 

History

History
78 lines (47 loc) · 2.52 KB

File metadata and controls

78 lines (47 loc) · 2.52 KB

Java HackerRank

Difficulty Max Score Success Ratio
Easy 10 96.96%

We define the following:

  • A subarray of an [SVG image] -element array is an array composed from a contiguous block of the original array's elements. For example, if [SVG image] , then the subarrays are [SVG image] , [SVG image] , [SVG image] , [SVG image] , [SVG image] , and [SVG image] . Something like [SVG image] 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 [SVG image] integers, find and print its number of negative subarrays on a new line.

Input Format

The first line contains a single integer, [SVG image] , denoting the length of array [SVG image] .

The second line contains [SVG image] space-separated integers describing each respective element, [SVG image] , in array [SVG image] .

Constraints

  • [SVG image]
  • [SVG image] Output Format

Print the number of subarrays of [SVG image] having negative sums.

Sample Input

5
1 -2 4 -5 1

Sample Output

9

Explanation

There are nine negative subarrays of [SVG image] :

  1. [SVG image]
  2. [SVG image]
  3. [SVG image]
  4. [SVG image]
  5. [SVG image]
  6. [SVG image]
  7. [SVG image]
  8. [SVG image]
  9. [SVG image]

Thus, we print [SVG image] on a new line.

💡 Hints

➡️ Approach

✅ Detailed Solution

View Solution : Java Subarray

Submissions Leaderboard Discussions Editorial
📝 My Submission 🏆 Track our position 🤔 Help from Community ✍️ Editorial