Filip_Markoski

2.Lab2.2 Zig - zag sequence (Solved)

Oct 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. public class ZigZagSequence {
  5.  
  6.     static int najdiNajdolgaCikCak(int a[]) {
  7.         /* Non-zero elements only! */
  8.         int len = 0;
  9.         int max = 0;
  10.        
  11.         if (a[0] != 0) {
  12.             len = 1;
  13.             max = 1;
  14.         }
  15.         for (int i = 1; i < a.length; i++) {
  16.             /* Product of two consecutive elements needs to be less than zero or greater than zero */
  17.             if (a[i - 1] < 0 && a[i] > 0) {
  18.                 if (len > max) {
  19.                     max = len;
  20.                 }
  21.             } else if (a[i - 1] > 0 && a[i] < 0) {
  22.                 if (len > max) {
  23.                     max = len;
  24.                 }
  25.             } else {
  26.                 len = 0;
  27.             }
  28.             len++;
  29.         }
  30.         /* Add one for the last element */
  31.         return max + 1;
  32.     }
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36.  
  37.         int N = Integer.parseInt(br.readLine());
  38.         int a[] = new int[N];
  39.         for (int i = 0; i < N; i++) {
  40.             a[i] = Integer.parseInt(br.readLine());
  41.         }  
  42.  
  43.         int rez = najdiNajdolgaCikCak(a);
  44.         System.out.println(rez);
  45.  
  46.         br.close();
  47.  
  48.     }
  49.  
  50. }
Add Comment
Please, Sign In to add comment