
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum number of operations on an array to make all elements 0 using C++.
Problem statement
Given an array of size N and each element is either 1 or 0. The task is to calculated the minimum number of operations to be performed to convert all elements to zero. One can perform below operations −
If an element is 1, You can change its value equal to 0 then −
If the next consecutive element is 1, it will automatically get converted to 0
If the next consecutive element is already 0, nothing will happen.
If arr[] = {1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1} then 4 operation are required to make all elements zero
Algorithm
1.If the current element is 1 then increment the count and search for the next 0 as all consecutive 1’s will be automatically converted to 0. 2. Return final count
Example
#include <iostream> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int performMinOperation(int *arr, int n){ int i, cnt = 0; for (i = 0; i < n; ++i) { if (arr[i] == 1) { int j; for (j = i + 1; j < n; ++j) { if (arr[j] == 0) { break; } } i = j - 1; ++cnt; } } return cnt; } int main(){ int arr[] = {1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1}; cout << "Minimum required operations = " << performMinOperation(arr, SIZE(arr)) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output −
Minimum required operations = 4
- Related Articles
- Minimum delete operations to make all elements of array same in C++.
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum operations required to make all the array elements equal in C++
- Number of operations required to make all array elements Equal in Python
- Minimum number of moves to make all elements equal using C++.
- Find minimum number of merge operations to make an array palindrome in C++
- Find the number of operations required to make all array elements Equal in C++
- C++ program to find minimum how many operations needed to make number 0
- Program to find minimum operations to make array equal using Python
- Minimum operations of given type to make all elements of a matrix equal in C++
- Find minimum operations needed to make an Array beautiful in C++
- Program to find minimum operations to make the array increasing using Python
- Minimum number of given operations required to make two strings equal using C++.
- Minimum operations to make XOR of array zero in C++
- Minimum number of elements that should be removed to make the array good using C++.

Advertisements