Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ please There are N coins, each showing either heads or tails. We would like all the coins to form a sequence of alternating heads

c++ please
image text in transcribed
image text in transcribed
There are N coins, each showing either heads or tails. We would like all the coins to form a sequence of alternating heads and tails. What is the minimum number of coins that must be reversed to achieve this? Write a function: int solution(vector &A); that, given an array A consisting of N integers representing the coins, returns the minimum number of coins that must be reversed. Consecutive elements of array A represent consecutive coins and contain either a 0 (heads) or a 1 (tails). Examples: 1. Given array A = [1,0,1,0, 1, 1), the function should return 1. After reversing the sixth coin, we achieve an alternating sequence of coins (1,0,1,0, 1, 0). 2. Given array A = [1,1,0, 1, 1), the function should return 2. After reversing the first and fifth coins, we achieve an alternating sequence [0,1,0,1,0). 3. Given array A = [0, 1, 0], the function should return O. The sequence of coins is already alternating. 4. Given array A = [0, 1, 1, 0], the function should return 2. We can reverse the first and second coins to get the sequence: [1,0, 1,0). Assume that: N is an integer within the range (1..100); each element of array A is an integer that can have one of the following values: 0,1. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OpenStack Trove

Authors: Amrith Kumar, Douglas Shelley

1st Edition

1484212215, 9781484212219

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago