uopspop

Untitled

Nov 2nd, 2020
786
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 : [0, +1, +1, +1, +1]
  8.      * [3,4,5,1,2]: true : [0, +1, +1, -4, +1, +1]
  9.      * *[4,5,1,2,3]: true : [0, +1, -4, +1, +1]
  10.      * [5,1,2,3,4]: true : [0, -4, +1, +1, +1]
  11.      * [5,4,1,2,3]: false
  12.      * *[3,2,1,5,4]: true : [0, -1, -1, +4, -1]
  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.  
  30.         boolean result_asce = check_asec(ary);
  31.         if (result_asce == true)
  32.             System.out.println("result_asce: " + result_asce);
  33.  
  34.         boolean result_desc = check_desc(ary);
  35.         if (result_desc == true)
  36.             System.out.println("result_desc: " + result_desc);
  37.  
  38.     }
  39.  
  40.     private static boolean check_asec(int[] ary) {
  41.         // O: ups
  42.         // O: ups down ups
  43.         // X: ups down down
  44.         int correct_sum_1 = ary.length - 1;
  45.         int correct_sum_2 = -1;
  46.  
  47.         int diff_sum;
  48.         int sum = 0;
  49.         int i_prev = 0;
  50.  
  51.         int count_go_down = 0;
  52.  
  53.         for (int i = 1; i < ary.length; i++) {
  54.             int diff = ary[i] - ary[i_prev];
  55.             if (diff < 0) count_go_down++;
  56.             if (count_go_down > 1) return false;
  57.  
  58.             sum += diff;
  59.             i_prev = i;
  60.         }
  61.  
  62.         if (sum == correct_sum_1) {
  63.             return true;
  64.         }else if (sum == correct_sum_2) {
  65.             return true;
  66.         }
  67.  
  68.         return false;
  69.     }
  70.  
  71.  
  72.     private static boolean check_desc(int[] ary) {
  73.         // O: downs
  74.         // O: downs up downs
  75.         // X: downs up downs
  76.         int correct_sum_1 = -(ary.length - 1);
  77.         int correct_sum_2 = +1;
  78.  
  79.         int diff_sum;
  80.         int sum = 0;
  81.         int i_prev = 0;
  82.  
  83.         int count_go_up = 0;
  84.  
  85.         for (int i = 1; i < ary.length; i++) {
  86.             int diff = ary[i] - ary[i_prev];
  87.             if (diff > 0) count_go_up++;
  88.             if (count_go_up > 1) return false;
  89.  
  90.             sum += diff;
  91.             i_prev = i;
  92.         }
  93.  
  94.         if (sum == correct_sum_1) {
  95.             return true;
  96.         }else if (sum == correct_sum_2) {
  97.             return true;
  98.         }
  99.  
  100.         return false;
  101.     }
  102.  
  103. }
  104.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×