Advertisement
advictoriam

Untitled

Jan 23rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Numbers
  2. {
  3.    /**
  4.       Computes the length of the longest sequence that occurs in the
  5.       start of the left half and the end of the right half of an
  6.       array.
  7.       @param values an array of integer values
  8.       @return the length of the longest end sequence
  9.    */
  10.    public int sameEnds(int[] values)
  11.    {
  12.       int length = 0;
  13.       int firstIndex = 0;
  14.       for(int i = (int)((double)values.length / 2 +.5); i < values.length; i++)
  15.       {
  16.          for(int j = i; j < values.length; j++)
  17.          {
  18.             if(values[firstIndex] == values[j])
  19.             {
  20.                length++;
  21.                if(j + 1 == values.length){return length;}
  22.                firstIndex++;
  23.             }
  24.             else{break;}
  25.          }
  26.          length = 0;
  27.          firstIndex = 0;
  28.       }
  29.       return 0;
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement