Other 2018 FR Questions FR other years Be Prepared Home
A-4
Part (a)
  public static int[] getColumn(int[][] arr2D, int c)
  {
    int[] column = new int[arr2D.length];

    for (int r = 0; r < column.length; r++)
      column[r] = arr2D[r][c];

    return column;
  }


Part (b)
  public static boolean isLatin(int[][] square)
  {
    if (containsDuplicates(square[0]))
      return false;

    for (int r = 1; r < square.length; r++)
      if (!hasAllValues(square[0], square[r]))
        return false;

    for (int c = 0; c < square[0].length; c++) 1
      if (!hasAllValues(square[0], getColumn(square, c)))
        return false;

     return true;
  }  2
Notes:
  1. Or
       for (int c = 0; c < square.length; c++)
    since square is a square array.

  2. Or a little shorter, combining the two loops into one (as proposed by Doug Vermes):
      public static boolean isLatin(int[][] square)
      {
        if (containsDuplicates(square[0]))
          return false;
    
        for (int i = 0; i < square.length; i++)
          if (!hasAllValues(square[0], square[i]) || !hasAllValues(square[0], getColumn(square, i)))
            return false;
    
         return true;
      }


Other 2018 FR Questions | Back to Contents

Copyright © 2018 by Skylight Publishing
support@skylit.com