Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider N coins aligned in a row. Each coin is showing either heads br tails. The adjacency of these coins is the number of pairs

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Consider N coins aligned in a row. Each coin is showing either heads br tails. The adjacency of these coins is the number of pairs of adjacent coins showing the same face. You are given an implementation of a function: class Solution { public int solution(int[] A); } that, given a non-empty array A consisting of N integers representing the coins, returns the maximum possible adjacency that can be bbtained by reversing exactly one coin (that is, one of the coins must be reversed). Consecutive elements of array A represent consecutive coins in the row. Array A contains only Os and/or 1s: O represents a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of six numbers, such that: 1 A[0] A[1] A[2] A[3] A[4] A[5] = 0 1 = 0 0 the function returns 4. The initial adjacency is 2, as there are two pairs of adjacent coins showing the same face, namely (0, 1) and (4, 5). After reversing the coin represented by A[2] the adjacency equals 4. A[1] = 1 A[2] = 0 A[3] A[4] = 0 A[5] 0 the function returns 4. The initial adjacency is 2, as there are two pairs of adjacent coins showing the same face, namely (0, 1) and (4, 5). After reversing the coin represented by A[2] the adjacency equals 4, as there are four pairs of adjacent coins showing the same face, hamely (0, 1), (1, 2), (2, 3) and (4, 5), and it is not possible to obtain a higher adjacency. The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for the example test cases. The goal of the exercise is to find and fix the bug(s) in the implementation. You can modify at most three lines. Assume that: N is an integer within the range [1..1,000); 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. Copyright 2009-2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited. 1 N 3 4 6 . 7 8 9 int r = 1 1 class Solution { 2 int solution(int[] A) { int n = A. length; 4. int result = 0; 5 for (int i = 0; i 0) { 16 if (A[i 11 = A[i]). count = count + 1; 18 else 19 count = count = 1; 20 } if (i 0) { if (A[i - 1] != A[i]) count = count + 1; 1 else count = count } if (i <><>

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

Students also viewed these Databases questions

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago