[Solution] Good Arrays CodeChef Solution
Problem
Chef considers an array good if it has no subarray of size less than such that the GCD of all the elements of the subarray is equal to .
Chef has an array of size with him. He wants to convert it into a good array by applying a specific operation.
In one operation, Chef can choose any subarray and reverse it.
Find the minimum number of operations required to convert the array into a good array and the operations themselves. If there are multiple ways to achieve the result, print any.
If it is not possible to convert the array into a good array, print instead.
Note: A subarray of an array is formed by deletion of several (possibly zero) elements from the beginning and several (possibly zero) elements from the end of the array.
Input Format
- First line will contain , number of test cases. Then the test cases follow.
- The first line of each test case contains of single integer denoting the length of the array.
- The second line contains space-separated integers representing the initial array.
Output Format
- If there is a way to convert the given array into a good array, then:
- In a single line, output denoting the minimum number of operations required.
- In the following lines, output integers - and denoting the left and right index of the subarray you want to reverse. Note that, .
- If there is no way to convert, print in a single line.
Explanation:
Test case : We need only one operation to convert the array into a good array. Choose and reverse the subarray to . Thus, the final array is . Note that in the final array, no subarray of length less than has gcd equal to .
Test case : It is not possible to convert the given array to a good array using any number of operations.
Test case : The given array is already good. That is, no subarray of length less than has gcd equal to . Thus, we need operations.
No comments:
Post a Comment