ProgrammingVery hardGATE CS · Campus placements
int count = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) count++; What is count at the end?
- A. 10
- B. 55
- C. 27
- D. 45
Correct answer: C. 27
The inner loop runs floor(10 / i) times for each i, giving 10 + 5 + 3 + 2 + 2 + 1 + 1 + 1 + 1 + 1 = 27. This harmonic count is O(n log n), which is why such loops are acceptable in contests.
More C Programming questions
- A contest program must test whether n is prime for n up to 10^9 with a 1 second limit. Which method is fast enough for one query?
- How many elements in the 1 to 50 range have exactly three 1 bits in binary?
- char buf[5] = "abcd"; printf("%zu", sizeof(buf) - strlen(buf)); What is printed?
- int a[5] = {0}; int p = a + 5; p[-1] = 7; printf("%d", a[4] + (a + 4)); What is printed?
- The sorted array is {1, 3, 3, 3, 7, 9}. What is the index of the first element that is at least 4?
- A qsort comparator compares double values with (x > y) - (x < y). After sorting {2.5, -1.0, 2.25}, which value is last?