Advertisement
qasimovey

Longest-Part

Apr 4th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1.  
  2. public class Demo {
  3.  
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         int[] longest_run;
  8.         longest_run = new int[]{3, 2, -1, 2, 7};
  9.  
  10.         foo(longest_run);
  11.     }
  12.  
  13.     public static int foo(int[] arr) {
  14.         int longest_asc__ = 1;
  15.         int start_index_asc = 0;
  16.         int end_index_asc = 0;
  17.  
  18.         int longest_desc__ = 1;
  19.         int start_index_desc = 0;
  20.         int end_index_desc = 0;
  21.  
  22.         boolean flag;
  23.  
  24.         int longest_asc = 1;
  25.         int longest_desc = 1;
  26.         int sum = 0;
  27.         //for asc
  28.  
  29.         for (int start = 0, left = 0; start < arr.length && left < arr.length - 1; ) {
  30.  
  31.             if (arr[left + 1] >= arr[left]) {
  32.                 left++;
  33.                 longest_asc++;
  34.             } else {
  35.                 if (longest_asc > longest_asc__) {
  36.                     longest_asc__ = longest_asc;
  37.                     start_index_asc = start; //inclusive
  38.                     end_index_asc = left;    //inclusive
  39.                 }
  40.  
  41.                 start = left + 1;
  42.                 left = start;
  43.                 longest_asc = 1;
  44.             }
  45.  
  46.             //reached to end
  47.             if (left == arr.length - 1) {
  48.                 if (longest_asc > longest_asc__) {
  49.                     longest_asc__ = longest_asc;
  50.                     start_index_asc = start; //inclusive
  51.                     end_index_asc = left;    //inclusive
  52.                 }
  53.             }
  54.  
  55.         }
  56.  
  57.         //for desc
  58.         for (int start = 0, left = 0; start < arr.length && left < arr.length - 1; ) {
  59.  
  60.             if (arr[left + 1] <= arr[left]) {
  61.                 left++;
  62.                 longest_desc++;
  63.             } else {
  64.                 if (longest_desc > longest_desc__) {
  65.                     longest_desc__ = longest_desc;
  66.                     start_index_desc = start; //inclusive
  67.                     end_index_desc = left;    //inclusive
  68.                 }
  69.  
  70.                 start = left + 1;
  71.                 left = start;
  72.                 longest_desc = 1;
  73.             }
  74.  
  75.             //reached to end
  76.             if (left == arr.length - 1) {
  77.                 if (longest_desc > longest_desc__) {
  78.                     longest_desc__ = longest_desc;
  79.                     start_index_desc = start; //inclusive
  80.                     end_index_desc = left;    //inclusive
  81.                 }
  82.             }
  83.  
  84.         }
  85.  
  86.  
  87.         if (longest_asc__ == longest_desc__) {
  88.             if (start_index_asc < start_index_desc) {
  89.                 flag = true; //asc
  90.             } else flag = false;
  91.         } else if (longest_asc__ > longest_desc__) {
  92.             flag = true;
  93.         } else {
  94.             flag = false;
  95.         }
  96.  
  97.         if (flag)
  98.             for (int i = start_index_asc; i <= end_index_asc; i++) {
  99.                 System.out.print(arr[i] + " ");
  100.                 sum += arr[i];
  101.             }
  102.         else {
  103.             for (int i = start_index_desc; i <= end_index_desc; i++) {
  104.                 System.out.print(arr[i] + " ");
  105.                 sum += arr[i];
  106.             }
  107.         }
  108.         // System.out.println();
  109.         // System.out.println("sum " + sum);
  110.         return sum;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement