Advertisement
advictoriam

Untitled

Jan 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement