id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

norm.krumpe@muohio.edu homework4 > maxRun
prev  |  next  |  chance

In an array, a "run" occurs when 2 or more consecutive elements have the same sign (positive, negative, or zero). For example, in the array {2, -3, -19, -8, -2, 5, 25, 0}, there is a run of 4 negative numbers, and a run of 2 positive numbers. The "length" of the run of negatives is 4 (because there are 4 negatives in a row), and the length of the run of positives is 2. Write a method that returns the length of the longest run. If no run occurs, return 0. Note that a run of 1 is not possible. Math.signum() can be useful for determining the sign of a number. Math.signum(num) returns -1.0 if num is negative, 0.0 if num is 0, and 1.0 if num is positive. This can be a quick way to determine whether two numbers have the same sign.


maxRun([2, -3, -19, -8, -2, 5, 25, 0]) → 4
maxRun([1, 0, 0, 0, -2, -2]) → 3
maxRun([4, -5]) → 0

...Save, Compile, Run (ctrl-enter)

int maxRun(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 300

Copyright Nick Parlante 2017 - privacy