Please help me in solving
Please explain your code add some comments.
I have added the comments
// as the couont increase to k we increase the j
while (arr[j] != 1) {
j++;//j increasing
}
countZero--;// decreasing the count
j++;
You are increasing j
till you dont find a 1
means you are iterating over zeroes. But only decreasing the count of countZero
by 1
. Why ? I am unable to understand this part.
Did you mean to increase j
till you find a zero. So the while loop must be the following.
while (arr[j] != 0) {
j++; // j increasing till we dont find a zero.
}
countZero--; // decreasing the count as we are removing 1 zero as countZero > k.
j++; // finally taking j to point after that 0.