[Solution] Square Sort CodeChef Solution
Problem
You are given an array of positive integers.
In one operation, you can do the following:
- Choose an index , and change the value of to either or .
Find the minimum number of operations required, such that, for the final array :
- is sorted in non-decreasing order;
- .
NOTE: The elements of the array might not fit in a 32-bit integer.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of two lines of input:
- The first line of each test case contains - the size of the array.
- The next line contains integers, - the elements of the array.
Output Format
For each test case, output on a new line, the minimum number of operations required.
Explanation:
Test case : The array is already sorted, no operation is required.
Test case : Using one operation, we can change to . The final array is which is sorted in non-decreasing order.
Test case : We make the following operations:
- Change to .
- Change to .
- Change to .
The final array is which is sorted in non-decreasing order.
No comments:
Post a Comment