A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a program to determine Minimum Number of Flips
Q:

Write a program to determine Minimum Number of Flips

0

Given a binary string (containing only 0s and 1s). We need to make this string a sequence of alternate characters (0 and 1) by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Write a program to determine the minimum number of bits to reach the goal.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <bits/stdc++.h>
using namespace std;

void print(vector<int> a,int n){
	for(int i=0;i<n;i++)
		cout<<a[i]<<" ";
	cout<<endl;
}

int minflip(string s)
{
	int flip0=0,flip1=0,flag0=0,flag1=1;
	
	//converting to string starting with 0
	for(int i=0;i<s.length();i++){
		if(flag0!=s[i]-'0')
			flip0++;
		flag0=1-flag0;
	}
	
	//converting to string starting with 1
	for(int i=0;i<s.length();i++){
		if(flag1!=s[i]-'0')
			flip1++;
		flag1=1-flag1;
	}
	
	return flip0>flip1?flip1:flip0; //returnig minimum
}

int main()
{
	string s;

	cout<<"Enter input string\n";
	cin>>s;

	cout<<"minimum no of flip required is: "<<minflip(s)<<endl;

	return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now