Given a binary array( consider it as cyclic with start and end tied together) of length N having only one’s and zero’s where
. The task is to find minimum operation needed to make the array beautiful.
An array is said to be beautiful if it has no consecutive ones or zeros. If not possible then print -1.
In one operation, you can do the following:
- Cut the array in two parts.
- Reverse one of these two parts.
- Join the corresponding endpoints of these two parts, creating one whole array again.
Find the minimum number of above operations to be performed on the array to make it beautiful such that it does not contains any consecutive one’s or zero’s.
Examples:
Input : A[] = { 1, 1, 0, 0 }
Output : 1
Explanation:
Make first cut between A[0] and A[1]
and second cut between A[2] and A[3].
Reverse array A[1] to A[2] and tie both array together.
Thus new array is A = [1, 0, 1, 0] which is beautiful.
Input : A[] = { 1, 1, 0, 0, 0 }
Output : -1
Approach: The goal is to make the array either of the form A1[] = { 1, 0, 1, …, 0, 1, 0 } or A2[] = { 0, 1, 0, … 1, 0, 1 }.
- If we have odd number of elements then it is impossible to make array beautiful as we always one binary number extra than other. Hence we return -1.
- If we have even number of elements then we can make any of array A1 or A2 only when we have binary zeros and ones equal count.
- Also, the minimum cut required is the amount of consecutive zero or ones we have and will be equal.
- So, we iterate the array and maintain the count of zeros and ones and also consecutive zeros.
- If zero = one, then return count of consecutive zeros, else return -1.
Note: One special case is when there is only one element in the array. If there is only 1 element present in the array, then the array is already beautiful so the answer is zero.
Below is the implementation of above approach:
C++
// CPP implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum operations // required to make array beautiful int minOperations(int A[], int n) { if (n & 1) return -1; int zeros = 0, consZeros = 0, ones = 0; for (int i = 0; i < n; ++i) { A[i] == 0 ? zeros++ : ones++; // counting consecutive zeros. if (i + 1 < n) { if (A[i] == 0 && A[i + 1] == 0) consZeros++; } } // check that start and end are same if (A[0] == A[n - 1] && A[0] == 0) consZeros++; // check is zero and one are equal if (zeros == ones) return consZeros; else return -1; } // Driver program int main() { int A[] = { 1, 1, 0, 0 }; int n = sizeof(A) / sizeof(A[0]); cout << minOperations(A, n); return 0; } |
Java
// Java implementation of above approach class GFG{ // Function to find minimum operations // required to make array beautiful static int minOperations(int[] A, int n) { if ((n & 1)>0) return -1; int zeros = 0, consZeros = 0, ones = 0; for (int i = 0; i < n; ++i) { if(A[i] == 0) zeros++; else ones++; // counting consecutive zeros. if (i + 1 < n) { if (A[i] == 0 && A[i + 1] == 0) consZeros++; } } // check that start and end are same if (A[0] == A[n - 1] && A[0] == 0) consZeros++; // check is zero and one are equal if (zeros == ones) return consZeros; else return -1; } // Driver program public static void main(String[] args) { int[] A =new int[] { 1, 1, 0, 0 }; int n = A.length; System.out.println(minOperations(A, n)); } } // This code is contributed by mits |
Python3
# Python 3 implementation of # above approach # Function to find minimum operations # required to make array beautiful def minOperations(A, n) : if n & 1 : return -1 zeros, consZeros, ones = 0, 0, 0 for i in range(n) : if A[i] : zeros += 1 else : ones += 1 # counting consecutive zeros. if( i + 1 < n) : if A[i] == 0 and A[i + 1] == 0 : consZeros += 1 # check that start and end are same if A[0] == A[n - 1] and A[0] == 0 : consZeros += 1 # check is zero and one are equal if zeros == ones : return consZeros else : return -1 # Driver code if __name__ == "__main__" : A = [1, 1, 0, 0] n = len(A) print(minOperations(A, n)) # This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approach class GFG{ // Function to find minimum operations // required to make array beautiful static int minOperations(int[] A, int n) { if ((n & 1)>0) return -1; int zeros = 0, consZeros = 0, ones = 0; for (int i = 0; i < n; ++i) { if(A[i] == 0) zeros++; else ones++; // counting consecutive zeros. if (i + 1 < n) { if (A[i] == 0 && A[i + 1] == 0) consZeros++; } } // check that start and end are same if (A[0] == A[n - 1] && A[0] == 0) consZeros++; // check is zero and one are equal if (zeros == ones) return consZeros; else return -1; } // Driver program static void Main() { int[] A =new int[] { 1, 1, 0, 0 }; int n = A.Length; System.Console.WriteLine(minOperations(A, n)); } } // This code is contributed by mits |
PHP
<?php // PHP implementation of above approach // Function to find minimum operations // required to make array beautiful function minOperations($A, $n) { if ($n & 1) return -1; $zeros = 0; $consZeros = 0; $ones = 0; for ($i = 0; $i < $n; ++$i) { $A[$i] == 0 ? $zeros++ : $ones++; // counting consecutive zeros. if (($i + 1) < $n) { if ($A[$i] == 0 && $A[$i + 1] == 0) $consZeros++; } } // check that start and // end are same if ($A[0] == $A[$n - 1] && $A[0] == 0) $consZeros++; // check is zero and one are equal if ($zeros == $ones) return $consZeros; else return -1; } // Driver Code $A = array( 1, 1, 0, 0 ); $n = sizeof($A); echo minOperations($A, $n); // This code is contributed // by akt_mit ?> |
1
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find minimum number of merge operations to make an array palindrome
- Find the minimum number of operations required to make all array elements equal
- Minimum operations to make XOR of array zero
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Minimum delete operations to make all elements of array same
- Make all the array elements odd with minimum operations of given type
- Minimum operations to make counts of remainders same in an array
- Minimum number of operations on an array to make all elements 0
- Minimum Increment operations to make Array unique
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations to make all elements equal using the second array
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum increment operations to make the array in increasing order
- Minimum Cost to make all array elements equal using given operations
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Minimum operations required to make all Array elements divisible by K
- Minimum Bitwise AND operations to make any two array elements equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

