Table of Contents
algorithm
Example
Output
Code description
Test case example
in conclusion
Home Backend Development C++ Minimum number of non-adjacent pair flips required to remove all zeros in a binary string

Minimum number of non-adjacent pair flips required to remove all zeros in a binary string

Sep 04, 2023 pm 01:09 PM
binary flip non-adjacent

Minimum number of non-adjacent pair flips required to remove all zeros in a binary string

In a binary string, flipping a pair of adjacent bits can easily remove a single 0 from the string. However, when we need to remove all 0's from a binary string, we may also need to flip non-adjacent bit pairs. In this article, we will discuss how to determine the minimum number of non-adjacent pair flips required to remove all 0's from a binary string.

algorithm

To solve this problem, we will use a simple greedy algorithm. The idea is to always choose the pair of bits that are furthest away from each other and have at least one 0 in between. We can then flip these two bits, effectively removing a 0 from the string. We repeat this process until all 0's have been removed.

Now let us implement this algorithm in C.

Example

#include <iostream>
#include <cstring>

using namespace std;

int main() {
   string s;
   s="100101000";
   int n = s.size();
   
   int cnt = 0;
   for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
         cnt++;
         if (i+2 < n && s[i+2] == '0') {
            i += 2;
         }
         else {
            i++;
         }
      }
   }
   
   cout << cnt << endl;
   return 0;
}
Copy after login

Output

3
Copy after login

Code description

The code above takes a binary string as input and calculates the minimum number of non-adjacent pair flips required to remove all 0's from the string. Now let's look at the code in detail.

First, we take a binary string as input and store it in the string variable "s". We also store the size of the string in an integer variable "n".

string s;
cin >> s;
int n = s.size();
Copy after login

Next, we initialize the variable "cnt" to store the number of 0's in the string. We then iterate over the string using a for loop. For each 0 encountered, we increment the count of 0s and check if the next two bits are also 0s. If so, we flip the pair of bits by increasing the index by 2. Otherwise, we flip only adjacent bit pairs by increasing the index by 1.

int cnt = 0;
for (int i = 0; i < n; i++) {
   if (s[i] == '0') {
      cnt++;
      if (i+2 < n && s[i+2] == '0') {
         i += 2;
      }
      else {
         i++;
      }
   }
}
Copy after login

Finally, we output the count of non-adjacent pair flips required to remove all 0's from the string.

cout << cnt << endl;
Copy after login

Test case example

Let us consider the binary string "100101000". The minimum number of non-adjacent pair flips required to remove all 0's from this string can be calculated using the algorithm above.

First, we encounter 0 at position 2. We flip the (1,3) pair to get the string "110101000". Then we encounter the next 0 at position 5. We flip the (1,7) pair to get the string "111101000". Then we encounter the next 0 at position 8. We flip the (1,9) pair to get the string "111111000". Now all 0's have been removed from the string.

The number of non-adjacent pair flips required to remove all 0's from a string is 3. We can verify this by running the above C code on the input string "100101000".

in conclusion

In this article, we discussed how to determine the minimum number of non-adjacent pair flips required to remove all 0's from a binary string. We use a simple greedy algorithm to solve this problem and implement it in C code. We also provide an example test case to illustrate how the algorithm works.

The above is the detailed content of Minimum number of non-adjacent pair flips required to remove all zeros in a binary string. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to flip the screen in Eye of Deep Space How to flip the screen in Eye of Deep Space Mar 22, 2024 pm 10:41 PM

Players can flip the screen when playing the game in Eye of Deep Space. Many users do not know how to flip the screen in Eye of Deep Space. Players need to turn on the option to support screen rotation in the control center and then return to the game. How to flip the screen of Deep Space Eye 1. Open the screen of your phone and slide up from the bottom of the screen with your finger. 2. Then you can open the control center. In the upper right corner of the control center is the switch to turn off screen rotation. 3. Click it to turn on screen rotation. At this time, you will find that the icon controlling rotation is highlighted in the control center. 4. When you open an application that supports screen rotation, it will rotate as the orientation of the phone changes.

How to calculate binary arithmetic How to calculate binary arithmetic Jan 19, 2024 pm 04:38 PM

Binary arithmetic is an operation method based on binary numbers. Its basic operations include addition, subtraction, multiplication and division. In addition to basic operations, binary arithmetic also includes logical operations, displacement operations and other operations. Logical operations include AND, OR, NOT and other operations, and displacement operations include left shift and right shift operations. These operations have corresponding rules and operand requirements.

Methods and techniques on how to achieve image flipping effect through pure CSS Methods and techniques on how to achieve image flipping effect through pure CSS Oct 20, 2023 am 10:57 AM

Methods and techniques on how to achieve image flipping effects through pure CSS Preface: In web development, we often need to add some animation effects to web pages to increase user experience. The flipping effect of pictures is one of the common effects. It is not only simple and convenient to realize the flipping of images through pure CSS, but also avoids the additional overhead caused by using other languages ​​​​such as JavaScript. This article will introduce how to achieve the flip effect of images through pure CSS, and provide specific code examples. 1. Using CSS3 transfo

What are the two major improvements of EDVAC? What are the two major improvements of EDVAC? Mar 02, 2023 pm 02:58 PM

EDVAC has two major improvements: one is the use of binary, and the other is the completion of stored programs, which can automatically advance from one program instruction to the next, and its operations can be automatically completed through instructions. "Instructions" include data and programs, which are input into the memory device of the machine in the form of codes, that is, the same memory device that stores data is used to store instructions for performing operations. This is the new concept of so-called stored programs.

HTML, CSS, and jQuery: Build a beautiful card flip effect HTML, CSS, and jQuery: Build a beautiful card flip effect Oct 27, 2023 pm 01:43 PM

HTML, CSS and jQuery: Build a beautiful card flip effect In web design, the application of special effects can increase the interactivity and visual effects of the page. The card flipping effect is a common special effect that can bring users a more vivid and interesting browsing experience. This article will introduce how to use HTML, CSS and jQuery to build a beautiful card flip effect, and provide specific code examples. First, we need to prepare the basic structure of HTML. We will use two div elements to represent the front of the card

How to convert binary to hexadecimal using C language? How to convert binary to hexadecimal using C language? Sep 01, 2023 pm 06:57 PM

Binary numbers are represented by 1s and 0s. The 16-bit hexadecimal number system is {0,1,2,3…..9,A(10),B(11),…F(15)} in order to convert from binary representation to hexadecimal Represents that the bit string ID is grouped into 4-bit chunks, called nibbles starting from the least significant side. Each block is replaced with the corresponding hexadecimal number. Let us see an example to get a clear understanding of hexadecimal and binary number representation. 001111100101101100011101 3 E 5 B&nb

How to read binary files in Golang? How to read binary files in Golang? Mar 21, 2024 am 08:27 AM

How to read binary files in Golang? Binary files are files stored in binary form that contain data that a computer can recognize and process. In Golang, we can use some methods to read binary files and parse them into the data format we want. The following will introduce how to read binary files in Golang and give specific code examples. First, we need to open a binary file using the Open function from the os package, which will return a file object. Then we can make

What is the main reason for using binary within computers? What is the main reason for using binary within computers? Apr 04, 2019 pm 02:25 PM

The main reasons why computers use binary systems: 1. Computers are composed of logic circuits. Logic circuits usually only have two states, the switch is on and off, and these two states can be represented by "1" and "0"; 2. Only two numbers, 0 and 1, are used in the binary system, which is less error-prone during transmission and processing, thus ensuring high reliability of the computer.

See all articles