ProgrammingVery hardCompetitive coding (CP)
What is printed for the sorted array below?
What is printed for the sorted array below?
#include <stdio.h>
int main(void) {
int a[] = {1, 3, 3, 3, 5, 8};
int n = 6, lo = 0, hi = n;
while (lo < hi) {
int m = lo + (hi - lo) / 2;
if (a[m] < 3) lo = m + 1; else hi = m;
}
int lb = lo;
lo = 0; hi = n;
while (lo < hi) {
int m = lo + (hi - lo) / 2;
if (a[m] <= 3) lo = m + 1; else hi = m;
}
printf("%d %d", lb, lo);
}- A. 1 3
- B. 1 4
- C. 0 4
- D. 1 5
Correct answer: B. 1 4
The first search finds the lower bound of 3, which is index 1. The second finds the first index whose value is greater than 3, which is index 4.
More C Programming questions
- What is printed? c #include <stdio.h> int find(const char hay, const char nd) { for (const char p = hay; p; p++) { const char a = p, b =…
- What is printed? Assume int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 10; int b = -3; printf("%u", a % b); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 40; int c = 0; while (!(x & 1u)) { x >>= 1; c++; } printf("%d", c); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 2, 3, 1, 4}; int c = 0; for (int i = 0; i < 5; i++) if (a[i] < a[i…
- For uint32t a = 5 and b = 3, how many of these statements are true? (i) a - b > a (ii) (a - b) + b == a (iii) ~a == UINT32MAX (iv) a / b b…
- What is printed? c #include <stdio.h> int main(void) { int a[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) a[i][j] = i 4…