Data Structures & AlgorithmsHardGATE CS · competitive programming
How many times does the body of for (i = 2; i * i ≤ n; i++) run for n = 10⁶?
- A. 1000
- B. 998
- C. 500
- D. 999
Correct answer: D. 999
i * i ≤ 10⁶ holds for i ≤ 1000, and i starts at 2, so i = 2..1000 gives 999 runs. Starting at 1 would give 1000.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For (i = 1; i ≤ n; i = 3) with n = 1000, how many times does the body run?
- Naive recursion fib(n) = fib(n−1) + fib(n−2), with base cases n < 2, makes calls to itself. How many calls in total does fib(10) make…
- rec(n) returns at once if n ≤ 1; otherwise it calls rec(n/2) twice with integer division. How many calls are made for n = 16, counting the…
- A singly linked list has a tail of 4 nodes leading into a cycle of 6 nodes. Floyd's method moves a slow pointer 1 step and a fast pointer…
- Reverse each complete group of k = 3 nodes in the list 1 → 2 → … → 10, leaving a final incomplete group as it is. What value is at…
- What is the area of the largest rectangle in the histogram with bar heights [2, 4, 2, 1, 6, 5, 5]?