Last Updated: 7 Dec, 2025

Mike's Beautiful Sequence

Easy
Asked in company
HashedIn by Deloitte

Problem statement

You are a data analyst working with a numerical sequence A of length n. A sequence is considered "beautiful" if the greatest common divisor (GCD) of all its elements is greater than 1.


To transform a sequence, you can perform a special operation:


- Choose an index i (where 1 <= i < n).
- Replace the two adjacent elements A[i] and A[i+1] with A[i] - A[i+1] and A[i] + A[i+1], respectively.


Your task is to find the minimum number of operations required to make the sequence A beautiful. If the sequence is already beautiful, no operations are needed.


Input Format:
The first line of input contains a single integer n, the length of the sequence.

The second line contains n space-separated integers, the elements of the sequence A.


Output Format:
The first line must be either "YES" or "NO". Since it is always possible to make the sequence beautiful, the answer will always be "YES".

If the answer is "YES", the second line must contain a single integer: the minimum number of operations required.


Note:
The most straightforward way to make the GCD of a sequence greater than 1 is to make all its elements even (so their GCD is at least 2).

Let's analyze the operation's effect on the parity (odd/even) of numbers:
- odd, odd -> (odd-odd), (odd+odd) -> even, even. This takes 1 operation to fix two adjacent odd numbers.

- odd, even -> (odd-even), (odd+even) -> odd, odd. This doesn't solve the problem directly, but it creates two adjacent odd numbers. We can then apply the first rule to this new pair. 
So, odd, even -> odd, odd -> even, even. This takes a total of 2 operations to fix a single odd number that is next to an even number.

The strategy is to iterate through the sequence and clear all odd numbers.