Advertisement
uopspop

Untitled

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