- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find minimum operations needed to make an Array beautiful in C++

In this problem, we are given a binary array bin[] consisting n binary values which happen to be 0 and 1. Our task is to find minimum operations needed to make an Array beautiful.

Beautiful array is a special type of binary array which consists of a pattern of alternate 0’s and 1’s.

**Problem Description** − We need to find the number operations that are required to make the array beautiful. An operations consists of these steps −

**Step 1** − Cut the array into two halves.

**Step 2** − Reverse any one of the two halves.

**Step 3** − Join then halves back.

We will be counting the number of operations that are required to make sure that the array becomes a beautiful array.

**Let’s take an example to understand the problem,**

## Input

bin[] = {1, 0, 1, 0, 0, 1}

## Output

1

## Explanation

We will cut the array, make a subarray bin[4, 5], reverse it and join it back.

## Solution Approach

The solution to the problem is based on finding the minimum number of switch operations which is equal to the number of consecutive zeros. The base cases are −

If the size of the array is 1, it is a beautiful array. If the size of the array is an odd number then it can never be a beautiful array.

For all even lengths, we will check the total number of consecutive zeros or ones which will be the number of operations to be performed.

## Algorithm

Initialise − zeroCount , oneCount, consZero = 0

**Step 1** − if ( n = 1), return 0

**Step 2** − if (n%2 != 0), return -1.

**Step 3** − Loop for i -> 0 to n-1.

**Step 3.1** − if bin[i] == bin[i+1] == 0, consZero++.

**Step 4** − if bin[n-1] == bin[0] == 0, consZero++.

**Step 5** − return consZero.

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int minOperations(int bin[], int n) { if(n == 1) return 0; if(n%2 != 0) return -1; int consZero = 0; for (int i = 0; i < n; ++i) { if (i + 1 < n) { if (bin[i] == 0 && bin[i + 1] == 0) consZero++; } } if (bin[0] == bin[n - 1] && bin[0] == 0) consZero++; return consZero; } int main() { int bin[] = { 1, 0, 1, 0, 0, 1}; int n = sizeof(bin) / sizeof(bin[0]); cout<<"The minimum operations needed to make an Array beautiful is "<<minOperations(bin, n); return 0; }

## Output

The minimum operations needed to make an Array beautiful is 1

- Related Articles
- C++ program to find minimum how many operations needed to make number 0
- Find minimum number of merge operations to make an array palindrome in C++
- Program to find minimum operations needed to make two arrays sum equal in Python
- Minimum operations to make XOR of array zero in C++
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- Program to find minimum operations to make array equal using Python
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum operations required to remove an array in C++
- Program to find minimum operations to make the array increasing using Python
- Minimum delete operations to make all elements of array same in C++.
- Minimum operations required to make all the array elements equal in C++
- C++ code to find minimum operations to make numbers c and d
- Minimum operations to make GCD of array a multiple of k in C++
- Minimum number of Appends needed to make a string palindrome in C++