Find minimum operations needed to make an Array beautiful

• Difficulty Level : Hard
• Last Updated : 10 May, 2021

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:

1. Cut the array in two parts.
2. Reverse one of these two parts.
3. 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 and A
and second cut between A and A.
Reverse array A to A 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 using namespace std; // Function to find minimum operations// required to make array beautifulint 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 == A[n - 1] && A == 0)        consZeros++;     // check is zero and one are equal    if (zeros == ones)        return consZeros;    else        return -1;} // Driver programint main(){    int A[] = { 1, 1, 0, 0 };    int n = sizeof(A) / sizeof(A);     cout << minOperations(A, n);     return 0;}

Java

 // Java implementation of above approach class GFG{// Function to find minimum operations// required to make array beautifulstatic 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 == A[n - 1] && A == 0)        consZeros++;     // check is zero and one are equal    if (zeros == ones)        return consZeros;    else        return -1;} // Driver programpublic 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 beautifuldef 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 == A[n - 1] and A == 0 :        consZeros += 1     # check is zero and one are equal    if zeros == ones :        return consZeros    else :        return -1 # Driver codeif __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 beautifulstatic 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 == A[n - 1] && A == 0)        consZeros++;     // check is zero and one are equal    if (zeros == ones)        return consZeros;    else        return -1;} // Driver programstatic 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



Javascript


Output:
1

Time Complexity: O(N)

My Personal Notes arrow_drop_up