ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int a[] = {2, 1, 3, 2, 4, 1}, best = 0, s = 0, lo = 0;
for (int hi = 0; hi < 6; hi++) {
s += a[hi];
while (s > 6) s -= a[lo++];
if (hi - lo + 1 > best) best = hi - lo + 1;
}
printf("%d", best);- A. 4
- B. 2
- C. 3
- D. 6
Correct answer: C. 3
This sliding window finds the longest run of consecutive elements with sum at most 6. Windows 2 1 3 and 1 3 2 both sum to 6 with length 3; any 4 consecutive elements sum to at least 8. '6' ignores the limit.
More C Programming questions
- What is wrong with this code? c char p = malloc(10); p++; free(p);
- What is printed? c int x = 5; #define x (x + 1) printf("%d", x);
- What is printed? c int c = 0; for (int i = 1; i <= 16; i = 2) for (int j = 0; j < i; j++) c++; printf("%d", c);
- What is printed? c int i, c = 0; for (i = 0; i < 10; i += i + 1) c++; printf("%d %d", c, i);
- What is printed? c int a = 2, b = 4; printf("%d %d %d", a & b, a && b, a | b);
- What is printed? c int z(int n) { return n < 5 ? 0 : n / 5 + z(n / 5); } int main(void) { printf("%d %d", z(100), z(125)); }