![]() |
VOOZH | about |
Given an array A[] of size N. You can replace any number in the array with the gcd of that element with any of its adjacent elements. Find the minimum number of such operation to make the element of whole array equal to 1. If its not possible print -1.
Examples:
Input : A[] = {4, 8, 9}
Output : 3
Explanation:
In the first move we choose (8, 9)
gcd(8, 9) = 1. Now the array becomes {4, 1, 9}.
After second move, the array becomes {1, 1, 9}.
After third move the array becomes {1, 1, 1}.
Input : A[] = { 5, 10, 2, 6 }
Output : 5
Explanation:
There is no pair with GCD equal one. We first
consider (5, 10) and replace 10 with 5. Now array
becomes { 5, 5, 2, 6 }. Now we consider pair (5, 2)
and replace 5 with 1, array becomes { 5, 1, 2, 6 }.
We have a 1, so further steps are simple.
Input : A[] = {8, 10, 12}
Output : -1
Explanation:
Its not possible to make all the element equal to 1.
Input : A[] = { 8, 10, 12, 6, 3 }
Output : 7 We can find all the sub array in O(N^2) and GCD can be calculated in O(Log N) using Euclidean algorithms.
The overall complexity will be O(N^2 Log N).
Here is the implementation of the above idea.
Output:
4
Time Complexity: O(n2*log(n))
Auxiliary Space: O(1)