×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: Dec 13, 2020 8:31 PM
Views: 4641
Tags: twodimarray
  1. public static boolean isSequence(int[][] a){
  2.         int n = a.length;
  3.         /* 1) Get the minimum element in array */
  4.         int min = getMinValue(a);
  5.  
  6.         /* 2) Get the maximum element in array */
  7.         int max = getMaxValue(a);
  8.  
  9.             if (n < 1)
  10.                 return false;
  11.  
  12.  
  13.  
  14.             /* 3) max - min + 1 is equal to n,  then only check all elements */
  15.             if (max - min + 1 == n)
  16.             {
  17.             /* Create a temp array to hold visited flag of all elements.
  18.                Note that, calloc is used here so that all values are initialized
  19.                as false */
  20.                 boolean visited[] = new boolean[n];
  21.                 int i;
  22.                 for (i = 0; i < n; i++)
  23.                 {
  24.                     for (int j = 0; j < a[i].length; j++) {
  25.                         if (visited[a[i][j] - min] != false)
  26.                             return false;
  27.  
  28.  
  29.                         visited[a[i][j] - min] = true;
  30.                     }
  31.                 }
  32.                     /* If we see an element again, then return false */
  33.  
  34.  
  35.                     /* If visited first time, then mark the element as visited */
  36.  
  37.  
  38.  
  39.                 /* If all elements occur once, then return true */
  40.                 return true;
  41.             }
  42.             return false; // if (max - min  + 1 != n)
  43.         }