Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
Two nested loops iterate: the outer runs n times, and for each outer iteration the inner loop runs from 1 up to the current outer index i. What is the total number of inner-loop iterations?
- A. n(n+1)/2
- B. n^2
- C. n(n-1)/2
- D. 2^n
Correct answer: A. n(n+1)/2
Total iterations are 1 + 2 + ... + n = n(n+1)/2, which is still O(n^2) asymptotically.
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
- What is the time complexity of binary searching for a value in a sorted array of n elements, then linearly scanning the next k elements…
- What does this code compute, and what is its time complexity for an array of n elements? c int best = a[0]; for (int i = 1; i < n; i++) if…
- Quicksort uses the Lomuto partition with the last element as pivot on [99, 24, 44, 86, 76, 60, 84, 3]. After one partition call, at which…
- What is the time complexity of this code, as a function of n? c for (int i = 0; i < n; i++) for (int j = i; j < n; j++) sum += a[i] a[j];
- A sorted array is [1, 9, 9, 10, 13, 14, 18, 23, 23, 25]. Using the standard two-pointer in-place duplicate-removal technique (a slow…
- What is the time complexity of reading the element arr[i] from an array of n elements, given i?