Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class ZigZagSequence {
- static int najdiNajdolgaCikCak(int a[]) {
- /* Non-zero elements only! */
- int len = 0;
- int max = 0;
- if (a[0] != 0) {
- len = 1;
- max = 1;
- }
- for (int i = 1; i < a.length; i++) {
- /* Product of two consecutive elements needs to be less than zero or greater than zero */
- if (a[i - 1] < 0 && a[i] > 0) {
- if (len > max) {
- max = len;
- }
- } else if (a[i - 1] > 0 && a[i] < 0) {
- if (len > max) {
- max = len;
- }
- } else {
- len = 0;
- }
- len++;
- }
- /* Add one for the last element */
- return max + 1;
- }
- public static void main(String[] args) throws Exception {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- int a[] = new int[N];
- for (int i = 0; i < N; i++) {
- a[i] = Integer.parseInt(br.readLine());
- }
- int rez = najdiNajdolgaCikCak(a);
- System.out.println(rez);
- br.close();
- }
- }
Add Comment
Please, Sign In to add comment