Advertisement
uopspop

Untitled

Nov 2nd, 2020 (edited)
2,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package other;
  2.  
  3. public class interview_arrayshift {
  4.     /**
  5.      * n = 5
  6.      * [1,2,3,4,5] or [5,4,3,2,1]
  7.      * [1,2,3,4,5]: true
  8.      * [3,4,5,1,2]: true
  9.      * *[4,5,1,2,3]: true
  10.      * [5,1,2,3,4]: true
  11.      * [5,4,1,2,3]: false
  12.      * *[3,2,1,5,4]: true
  13.      *
  14.      */
  15.  
  16.     public static void main (String[] args) {
  17.  
  18.  
  19. //        int[] ary = {1,2,3,4,5};
  20. //        int[] ary = {4,5,1,2,3};
  21. //        int[] ary = {4,5,6,7,8,1,2,3};
  22. //        int[] ary = {7,8,4,5,6,1,2,3}; // wrong: double downs
  23.  
  24. //        int[] ary = {5,4,3,2,1};
  25. //        int[] ary = {3,2,1,5,4};
  26.         int[] ary = {7,6,5,4,3,2,1,9,8};
  27. //        int[] ary = {3,2,1,7,6,5,9,8}; // wrong: double ups
  28.  
  29.         // append the exact same array to the end
  30.         int[] ary_2 = new int[ary.length * 2];
  31.         for (int i = 0; i < ary.length; i++) {
  32.             ary_2[i] = ary[i];
  33.             ary_2[i + ary.length] = ary[i];
  34.         }
  35.  
  36.         // ascending
  37.         boolean is_asce_ok = check_asec(ary, ary_2);
  38.         System.out.println("is_asce_ok: " + is_asce_ok);
  39.  
  40.         // descending
  41.         boolean is_desc_ok = check_desc(ary, ary_2);
  42.         System.out.println("is_desc_ok: " + is_desc_ok);
  43.  
  44.     }
  45.  
  46.     private static boolean check_asec(int[] ary, int[] ary_2) {
  47.         int num_start_asec = 1;
  48.         boolean isAsecOK = false;
  49.         for (int i = 0; i < ary_2.length; i++) {
  50.             // found starting number
  51.             if (ary_2[i] == num_start_asec) {
  52.                 int i_ary = 0;
  53.                 int i_ary_2_tmp = i;
  54.                 while (true) {
  55.                     if (i_ary >= ary.length) {
  56.                         isAsecOK = true;
  57.                         break;
  58.                     }
  59.                     if (i_ary_2_tmp >= ary_2.length) break;
  60.  
  61.                     int val = i_ary + 1;
  62.                     if (ary_2[i_ary_2_tmp] != val) {
  63.                         isAsecOK = false;
  64.                         break;
  65.                     }
  66.  
  67.                     i_ary++;
  68.                     i_ary_2_tmp++;
  69.  
  70.                 }
  71.  
  72.             }
  73.             if (isAsecOK) break;
  74.         }
  75.         return isAsecOK;
  76.     }
  77.  
  78.     private static boolean check_desc(int[] ary, int[] ary_2) {
  79.         int num_start_desc = ary.length;
  80.         boolean isDescOK = false;
  81.         for (int i = 0; i < ary_2.length; i++) {
  82.             if (ary_2[i] == num_start_desc) {
  83.  
  84.                 int i_ary = 0;
  85.                 int i_ary_2_tmp = i;
  86.                 while (true) {
  87.                     if (i_ary >= ary.length) {
  88.                         isDescOK = true;
  89.                         break;
  90.                     }
  91.                     if (i_ary_2_tmp >= ary_2.length) break;
  92.  
  93.                     int val = num_start_desc - i_ary;
  94.                     if (ary_2[i_ary_2_tmp] != val) {
  95.                         isDescOK = false;
  96.                         break;
  97.                     }
  98.  
  99.                     i_ary++;
  100.                     i_ary_2_tmp++;
  101.  
  102.                 }
  103.  
  104.             }
  105.             if (isDescOK) break;
  106.         }
  107.         return isDescOK;
  108.     }
  109.  
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement