SHARE
TWEET

Untitled

advictoriam Jan 23rd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Numbers
  2. {
  3.    /**
  4.       Computes the length of the longest run (sequence of
  5.       adjacent repeated values) in an array.
  6.       @param values an array of integer values
  7.       @return the length of the longest run in values
  8.    */
  9.    public int lengthOfLongestRun(int[] values)
  10.    {
  11.       int currentNum = 0;
  12.       int currentLength = 0;
  13.       int longestLength = 0;
  14.      
  15.       for(int a : values)
  16.       {
  17.          if(a == currentNum)
  18.          {
  19.            currentLength++;
  20.          }
  21.          else
  22.          {
  23.            currentNum = a;
  24.            if(longestLength < currentLength){longestLength = currentLength;}
  25.            currentLength = 1;
  26.          }
  27.       }
  28.       if(longestLength < currentLength){longestLength = currentLength;}
  29.       return longestLength;
  30.    }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top