Open In App

Find minimum operations needed to make an Array beautiful

Improve
Improve
Like Article
Like
Save
Share
Report

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 2\leq N \leq 10^{6}    . 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[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
?>

                    

Javascript

<script>
    // Javascript implementation of above approach
     
    // Function to find minimum operations
    // required to make array beautiful
    function minOperations(A, n)
    {
        if ((n & 1)>0)
            return -1;
 
        let zeros = 0, consZeros = 0, ones = 0;
 
        for (let 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;
    }
     
    let A = [ 1, 1, 0, 0 ];
    let n = A.length;
   
    document.write(minOperations(A, n));
         
</script>

                    

Output
1

Complexity Analysis:

  • Time Complexity: O(N)
  • Auxiliary Space: O(1)


Last Updated : 08 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads