Open zoom-alex-yermakov opened 7 years ago
@ayermakov-zoom yep, edge case is a no-case actually :) I think it should look like this: (added check for X no in A)
function solution(X, A) {
N = A.length;
let sum = 0, chunk = 0;
for (let i=0; i<N; i++) {
if (A[i]==X) {
sum++;
chunk++;
} else {
chunk = 0;
}
}
return (sum > chunk) && sum > 0 ? (N-sum) : -1;
}
What happens for X = 5, A = [9]. This satisfies the constraints given in the problem but I can't find the right answer
I believe your solution for the "Split array into two parts" is not 100% correct, due to wrong assumption about the edge case. You described the case [3,5,5,5,5] -> [3 | 5,5,5,5] as wrong "because for X=5 by cutting 4 elements from the right eventually we will get an index of 1 thus leaving no elements equal to X on the left at all.". However, if you remember the task requirements, we are looking for K where the number of elements equal to X in A[0..K−1] is the same as the number of elements different from X in A[K..N−1]. In the edge case above, the number of elements equal to X in the left part is 0, and the number of elements different from X in the right part is also 0, so the task requirement is satisfied. Correct me if I am wrong ;)