ProgrammingHardTCS NQT · Infosys

How many times does the value change between neighbours in {1, 1, 2, 2, 2, 3, 1}? Compute with this program.

How many times does the value change between neighbours in {1, 1, 2, 2, 2, 3, 1}? Compute with this program.
#include <stdio.h>
int main(void) {
    int a[] = {1, 1, 2, 2, 2, 3, 1}, c = 0;
    for (int i = 0; i + 1 < 7; i++)
        if (a[i] != a[i + 1]) c++;
    printf("%d", c);
}
  1. A. 2
  2. B. 4
  3. C. 6
  4. D. 3

Correct answer: D. 3

The changes are 1 to 2, 2 to 3 and 3 to 1, so the count is 3.

More C Programming questions

Exams that ask this topic