1

Topic: Problem of the adjacent coins (The Adjacent Coins Problem)

The description and the correct implementation here. There is a task, to correct the variant offered more low changing no more than three lines. On a test dial-up where for example A [] = {0, 0}, the correct implementation produces 0, that that more low - 1. Help to rectify an error. public int solution (int [] A) {//n length of the array int n = A.Length; int result = 0;//Calculate the number of consecutive identical ones, for both sides for (int i = 0; i <n - 1; i ++)//why n - 1 {if (A [i] == A [i + 1]) result = result + 1;}//Calculate max delta int r = 0; for (int i = 0; i <n; i ++) {//count: current delta int count = 0; if (i> 0) {if (A [i - 1]! = A [i]) count = count + 1; else count = count - 1;} if (i <n - 1) {if (A [i + 1]! = A [i]) count = count + 1; else count = count - 1;} r = Math. Max (r, count);}//max

2

Re: Problem of the adjacent coins (The Adjacent Coins Problem)

Hello, licedey, you wrote: L> the Description and the correct implementation here. L> There is a task, to correct the variant offered more low changing no more than three lines. L> on a test dial-up where for example A [] = {0, 0}, the correct implementation produces 0, that that more low - 1. L> Help to rectify an error. For those a coma, laziness to walk under links, here the general description: Here is a problem that I considered solved for some months: The Adjacent Coins Problem. This is a problem where you have to choose a coin that maximizes your gain or minimizes your loss, but you have to do it in linear time and constant memory Consider N coins aligned in a row. Each coin is showing either heads or tails. The adjacency of these coins is the number of adjacent pairs of coins with the same side facing up. Write an a C ++ function int solution (const vector <int> &A); that, given a non-empty zero-indexed array A consisting of N integers representing the coins, returns the maximum possible adjacency that can be obtained 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 0s and/or 1s: 0 represents a coin with heads facing up; 1 represents a coin with tails facing up. For example, given array A consisting of eight numbers, such that: A [0] = 1 A [1] = 1 A [2] = 0 A [3] = 1 A [4] = 0 A [5] = 0 A [6] = 1 A [7] = 1 the function should return 5. The initial adjacency is 3, as there are three pairs of adjacent coins with the same side facing up, namely (0, 1), (4, 5) and (6, 7). After reversing the coin represented by A [2], the adjacency equals 5, as there are five pairs of adjacent coins with the same side facing up, namely: (0, 1), (1, 2), (2, 3), (4, 5) and (6, 7), and it is not possible to obtain a higher adjacency. The same adjacency can be obtained by reversing the coin represented by A [3]. Assume that: N is an integer within the range [1. 100,000]; each element of array A is an integer within the range [0. 1]. Complexity: expected worst-case time complexity is O (N); expected worst-case space complexity is O (1), beyond input storage (not counting the storage required for input arguments).

3

Re: Problem of the adjacent coins (The Adjacent Coins Problem)

Hello, licedey, you wrote://Calculate max delta int r =-2;

4

Re: Problem of the adjacent coins (The Adjacent Coins Problem)

Hello, StatujaLeha, you wrote: SL> Hello, licedey, you wrote: SL> SL>//Calculate max delta SL> int r =-2; SL> Thanks that is necessary! And that brains boiled to understand that in this algorithm not so.