Where is das bug?

So I got this Java code, the challenge is to find a bug in it.

The array element is a coin, which can show heads or tails. You should return the maximum Adjacency of that coin row. Adjacency is a situation where there are 2 coins showing the same head. Can u help /sci? I don't see a bug, and all of my inputs worked well.

>coding "challenge"
>uses an array
it's always an out of index error

This is not my code, I should find an error in it.
The goal is to find a logic error or optimize the time... its time complexity should be O(n) and its space complexity O(1)

what value will A.length return?

Array length (in this case, 11 because there are 11 numbers)

I think it should be result-r at the end

U are addressing A[0]

There is no outOfBound error, however, your algo seems pretty weird, i don't get how it works. Try using the max adjancy in the middle of the arr rather than the start

for(int i = 0; i < n - 1; i++) {
if(A[i] == A[i + 1])
//etc
}

At i = n -1, you will access A[n-1] and A[n], thus going out of range (0 to n - 1, inclusive).

Yeah I don't get it either. I think it would be easier to change your int result into an array. Store all the values at each step, and reset the value to 0 when the adjacency is over. Then find the max of the array at the end.

>So I got this Java code

> At i = n -1, you will access A[n-1] and A[n]

That would be the case if i could ever reach n-1.

However, the for loop test prevents that from ever happening.

The for loop will only execute if i < n-1. But if it's always true that i < n-1, it's not possible for i to be equal to n-1, as you originally claimed. Hence, your case will never happen.

Solution was to change
int r=-1

Because one of the coins MUST be flipped.

This is the most redundant code i've ever read in a while.

makes sense, otherwise that whole part with the count is basically useless

The bug is you're using an int to store a boolean value.

Your description is too vague to be meaningful.

> Adjacency is a situation where there are 2 coins showing the same head.

Do you mean "showing the same SIDE"? Or do you mean literally "showing the same HEAD" -- as in, they both have to be head-side-up for it to count?

And when you say "there are 2 coins", did you actually mean to say "there are 2 ADJACENT coins"? Because the way you wrote it, it implies that those 2 coins could be anywhere. You're the one defining things here, so you have the responsibility to do it absolutely precisely -- I'm not going try to fill in missing words for you.

> return the maximum Adjacency

If you mean "Adjacency" to refer to a boolean-valued relationship between two coins (as you claimed), then what does "maximum Adjacency" mean? How can a boolean-valued relationship between two coins have a "maximum" of any kind?

In other words, you don't make it clear what set of numbers you're finding the maximum of. In order to apply the idea of "maximum", you first need a list of numbers. For example, if I had the list of numbers (3,8,6,1,4) then I could say that the maximum is 8. But it's not clear where all of your different numbers come from. Counting how many adjacent coins there are with the same side up simply yields a single number. Okay, I might understand how to get that single number -- but where am I going to get all the other numbers that I would need in order to calculate a "maximum"?

Basically, your description is so bad, and is so full of holes, that your post is utterly worthless.

Just throw this thread away, take an hour to figure out how to describe your situation coherently, and then try again.

This. Trash thread.

Not sure if pun intended...

> its time complexity should be O(n) and its space complexity O(1)

it is

>Solution was to change
>int r=-1

how about deleting anything after int r = 0 and
insert
return Math.max(result,n-result)

??????????

the bug is everything after line 8, you wrote a retarded function

'Adjacency ' dont know
but you dont count anything becouse it is set to inside the loop ....

forgot 0
becouse it is set to 0 inside the loop ..

java indexes start from 0, right?
the first look, it's going from 0 to less than length - 1, meaning from 0 to less than 10, meaning from 0 to 9. Either put less or equal sign or just n.

*first loop