package ua.nure.jfn.task1;

public class Part5 {

	public static void main(String[] args) {
		int[] test = { 1, 2, 3, 2, 1, 1, 4, 3, 2, 2, 1 };
		for (int item : sequence(test)) {
			System.out.printf("%d ", item);
		}

		System.out.println();
	}

	public static int[] sequence(int[] ar) {
		if (ar == null)
			return null;

		int n = ar.length;

		int[] seq = new int[n];
		int count = 0;
		boolean inSeq = false;

		for (int i = 1; i < n; i++) {
			int a = ar[i - 1];
			int b = ar[i];

			if (a > b) {
				seq[count++] = a;
				inSeq = true;
			} else if (inSeq) {
				seq[count++] = a;
				inSeq = false;
			}

			if (i == n - 1 && inSeq)
				seq[count++] = b;
		}

		if (count <= 1)
			return new int[0];

		int[] result = new int[count];
		for (int i = 0; i < count; i++)
			result[i] = seq[i];

		return result;
	}
}
