ProgrammingVery hardCompetitive coding (CP)
How many subarrays of {1, -1, 2, -2, 3} have sum zero? Compute with this program.
How many subarrays of {1, -1, 2, -2, 3} have sum zero? Compute with this program.
#include <stdio.h>
int main(void) {
int a[] = {1, -1, 2, -2, 3}, c = 0;
for (int i = 0; i < 5; i++) {
int s = 0;
for (int j = i; j < 5; j++) {
s += a[j];
if (s == 0) c++;
}
}
printf("%d", c);
}- A. 2
- B. 3
- C. 4
- D. 1
Correct answer: B. 3
The zero-sum subarrays are [1, -1], [2, -2] and [1, -1, 2, -2], which is three.
More C Programming questions
- What is printed? c #include <stdio.h> int pc(unsigned n) { return n ? (int)(n & 1u) + pc(n >> 1) : 0; } int main(void) { printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (13 << 1) + 13); }
- How many numbers from 1 to 100 are multiples of 16? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 1u << 31; x <<= 1; printf("%u", x); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'n' == 10 && '0' == 0); }
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x1F; uint8t r =…