1. Home
  2. Explore
  3. Computer Science
  4. 6.006 Problem Session

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

6.006 Problem Session

  • 25Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Master Theorem Application

What is the primary purpose of the Master Theorem in algorithms?

25 questions

  1. 1

    What is the primary purpose of the Master Theorem in algorithms?

    1. A

      To analyze the space complexity of iterative algorithms.

    2. B

      To provide a systematic way to optimize recursive functions.

    3. C

      To compare the performance of different sorting algorithms.

    4. D

      To offer a quick method for solving recurrence relations.

    Correct answer
    D

    To offer a quick method for solving recurrence relations.

    Explanation

    The Master Theorem is a powerful tool designed to quickly solve recurrence relations of the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)T(n)=aT(n/b)+f(n), providing asymptotic bounds without needing to draw computation trees every time. It's often referred to as a 'sledgehammer' for recurrences due to its efficiency.

  2. 2

    Which component in the Master Theorem recurrence T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)T(n)=aT(n/b)+f(n) represents the branching factor of the computation tree?

    1. A

      The term 'n/b' indicating problem size reduction.

    2. B

      The function 'f(n)' representing work at each node.

    3. C

      The constant 'a' indicating the number of subproblems.

    4. D

      The constant 'b' indicating the division factor for 'n'.

    Correct answer
    C

    The constant 'a' indicating the number of subproblems.

    Explanation

    In the Master Theorem recurrence T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)T(n)=aT(n/b)+f(n), 'a' represents the number of subproblems, or the branching factor, at each step of the recursion. For example, in merge sort, 'a' would be 2 because it splits into two pieces.

  3. 3

    If a recurrence relation is T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(\sqrt{n})T(n)=2T(n/2)+O(n​), what is the value of 'n to the log base b of a'?

    1. A

      It simplifies to n1n^1n1, indicating linear growth.

    2. B

      It evaluates to n2n^2n2, indicating quadratic growth.

    3. C

      It becomes n1/2n^{1/2}n1/2, indicating square root growth.

    4. D

      It is n0n^0n0, indicating constant time growth.

    Correct answer
    A

    It simplifies to n1n^1n1, indicating linear growth.

    Explanation

    For T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(\sqrt{n})T(n)=2T(n/2)+O(n​), we have a=2a=2a=2 and b=2b=2b=2. Therefore, nlog⁡ban^{\log_b a}nlogb​a becomes nlog⁡22n^{\log_2 2}nlog2​2, which simplifies to n1=nn^1 = nn1=n.

  4. 4

    Given T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(\sqrt{n})T(n)=2T(n/2)+O(n​), which case of the Master Theorem applies?

    1. A

      Case 3, where f(n)f(n)f(n) dominates the work.

    2. B

      None of the standard Master Theorem cases are applicable.

    3. C

      Case 2, where f(n)f(n)f(n) is asymptotically similar to nlog⁡ban^{\log_b a}nlogb​a.

    4. D

      Case 1, where f(n)f(n)f(n) is upper bounded by nlog⁡ban^{\log_b a}nlogb​a minus an epsilon.

    Correct answer
    D

    Case 1, where f(n)f(n)f(n) is upper bounded by nlog⁡ban^{\log_b a}nlogb​a minus an epsilon.

    Explanation

    For T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(\sqrt{n})T(n)=2T(n/2)+O(n​), nlog⁡ba=nlog⁡22=n1n^{\log_b a} = n^{\log_2 2} = n^1nlogb​a=nlog2​2=n1. The function f(n)=O(n)=O(n1/2)f(n) = O(\sqrt{n}) = O(n^{1/2})f(n)=O(n​)=O(n1/2). We can write n1/2n^{1/2}n1/2 as O(n1−1/2)O(n^{1 - 1/2})O(n1−1/2), where epsilon is 1/2>01/2 > 01/2>0. This matches Case 1 of the Master Theorem (f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b a - \epsilon})f(n)=O(nlogb​a−ϵ)).

  5. 5

    If f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b a - \epsilon})f(n)=O(nlogb​a−ϵ) for some ϵ>0\epsilon > 0ϵ>0, what is the asymptotic bound for T(n)T(n)T(n) according to Master Theorem Case 1?

    1. A

      T(n)T(n)T(n) is Θ(f(n))\Theta(f(n))Θ(f(n)), meaning f(n)f(n)f(n) dominates.

    2. B

      T(n)T(n)T(n) is Θ(nlog⁡balog⁡kn)\Theta(n^{\log_b a} \log^k n)Θ(nlogb​alogkn), with a logarithmic factor.

    3. C

      T(n)T(n)T(n) is Θ(nlog⁡ba+ϵ)\Theta(n^{\log_b a + \epsilon})Θ(nlogb​a+ϵ), for some positive epsilon.

    4. D

      T(n)T(n)T(n) is Θ(nlog⁡ba)\Theta(n^{\log_b a})Θ(nlogb​a), indicating tree traversal dominates.

    Correct answer
    D

    T(n)T(n)T(n) is Θ(nlog⁡ba)\Theta(n^{\log_b a})Θ(nlogb​a), indicating tree traversal dominates.

    Explanation

    Case 1 of the Master Theorem states that if f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b a - \epsilon})f(n)=O(nlogb​a−ϵ) for some positive ϵ\epsilonϵ, then T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a})T(n)=Θ(nlogb​a). In this case, the work done in dividing and combining (represented by nlog⁡ban^{\log_b a}nlogb​a) dominates the work done at each node (f(n)f(n)f(n)).

  6. 6

    When solving recurrences by drawing a computation tree, how is the total number of levels in the tree determined?

    1. A

      By the total number of nodes present at the deepest level.

    2. B

      By the value of 'a', the branching factor, at each node.

    3. C

      By the work function 'f(n)' performed at each node.

    4. D

      By the base 'b' of the problem size reduction, usually log⁡bn\log_b nlogb​n.

    Correct answer
    D

    By the base 'b' of the problem size reduction, usually log⁡bn\log_b nlogb​n.

    Explanation

    In a computation tree where the problem size is divided by 'b' at each step, the total number of levels is approximately log⁡bn\log_b nlogb​n. The recursion stops when the problem size reaches a base case, typically 1.

  7. 7

    For a recurrence T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(\sqrt{n})T(n)=2T(n/2)+O(n​), what is the amount of work done at level lll of the computation tree?

    1. A

      It is 2l⋅O(n⋅2−l)2^l \cdot O(\sqrt{n \cdot 2^{-l}})2l⋅O(n⋅2−l​), considering node count and work.

    2. B

      It is 2l⋅O(n⋅2−l)2^l \cdot O(n \cdot 2^{-l})2l⋅O(n⋅2−l), showing linear growth.

    3. C

      It is O(n)O(\sqrt{n})O(n​), as the total work is constant per level.

    4. D

      It is 2l⋅O(n)2^l \cdot O(n)2l⋅O(n), as work at each node is proportional to nnn.

    Correct answer
    A

    It is 2l⋅O(n⋅2−l)2^l \cdot O(\sqrt{n \cdot 2^{-l}})2l⋅O(n⋅2−l​), considering node count and work.

    Explanation

    At level lll, there are 2l2^l2l nodes. Each node processes an input of size n/2ln/2^ln/2l. The work done at each node is O(n/2l)=O(n⋅2−l/2)O(\sqrt{n/2^l}) = O(\sqrt{n} \cdot 2^{-l/2})O(n/2l​)=O(n​⋅2−l/2). So the total work at level lll is 2l⋅O(n⋅2−l/2)=O(n⋅2l/2)2^l \cdot O(\sqrt{n} \cdot 2^{-l/2}) = O(\sqrt{n} \cdot 2^{l/2})2l⋅O(n​⋅2−l/2)=O(n​⋅2l/2).

  8. 8

    Which of the following describes the sum of work in a computation tree for a recurrence like T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)T(n)=aT(n/b)+f(n)?

    1. A

      It is always a simple linear sum of terms.

    2. B

      It often simplifies into a logarithmic series.

    3. C

      It usually results in an arithmetic progression of tasks.

    4. D

      It typically forms a geometric series that can be summed.

    Correct answer
    D

    It typically forms a geometric series that can be summed.

    Explanation

    The sum of work across levels in a computation tree for these types of recurrences often forms a geometric series. Each level's work can be expressed as a term in this series, allowing for a closed-form summation.

  9. 9

    For T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), what is the value of 'n to the log base b of a'?

    1. A

      n3/2n^{3/2}n3/2, derived from log⁡48\log_4 8log4​8.

    2. B

      n1/2n^{1/2}n1/2, derived from log⁡84\log_8 4log8​4.

    3. C

      n2n^2n2, derived from log⁡48⋅2\log_4 8 \cdot 2log4​8⋅2.

    4. D

      n1n^1n1, derived from log⁡84⋅2\log_8 4 \cdot 2log8​4⋅2.

    Correct answer
    A

    n3/2n^{3/2}n3/2, derived from log⁡48\log_4 8log4​8.

    Explanation

    For T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), we have a=8a=8a=8 and b=4b=4b=4. nlog⁡ban^{\log_b a}nlogb​a becomes nlog⁡48n^{\log_4 8}nlog4​8. Since 43/2=(4)3=23=84^{3/2} = (\sqrt{4})^3 = 2^3 = 843/2=(4​)3=23=8, log⁡48=3/2\log_4 8 = 3/2log4​8=3/2. So, the value is n3/2n^{3/2}n3/2.

  10. 10

    Given T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), which case of the Master Theorem applies?

    1. A

      Case 2, because f(n)f(n)f(n) is asymptotically equal to nlog⁡ban^{\log_b a}nlogb​a.

    2. B

      Case 3, because f(n)f(n)f(n) is significantly larger than nlog⁡ban^{\log_b a}nlogb​a.

    3. C

      Case 1, because f(n)f(n)f(n) is significantly smaller than nlog⁡ban^{\log_b a}nlogb​a.

    4. D

      None, as the terms aaa and bbb do not fit the criteria.

    Correct answer
    A

    Case 2, because f(n)f(n)f(n) is asymptotically equal to nlog⁡ban^{\log_b a}nlogb​a.

    Explanation

    For T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), we found nlog⁡ba=n3/2n^{\log_b a} = n^{3/2}nlogb​a=n3/2. The work function is f(n)=O(nn)=O(n3/2)f(n) = O(n\sqrt{n}) = O(n^{3/2})f(n)=O(nn​)=O(n3/2). Since f(n)f(n)f(n) is O(n3/2)O(n^{3/2})O(n3/2), and the base compares to n3/2n^{3/2}n3/2, this fits Master Theorem Case 2 with k=0k=0k=0 (as there's no additional logarithmic factor). However, because f(n)f(n)f(n) is given as 'big O' and not 'big theta', the resulting bound will also be 'big O'.

  11. 11

    What is the asymptotic bound for T(n)T(n)T(n) for the recurrence T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​)?

    1. A

      O(n1/2)O(n^{1/2})O(n1/2), due to the square root term.

    2. B

      O(n3/2log⁡n)O(n^{3/2} \log n)O(n3/2logn), from Case 2 of Master Theorem.

    3. C

      O(n2)O(n^2)O(n2), due to the n/4n/4n/4 division.

    4. D

      O(nlog⁡n)O(n \log n)O(nlogn), if it were merge sort.

    Correct answer
    B

    O(n3/2log⁡n)O(n^{3/2} \log n)O(n3/2logn), from Case 2 of Master Theorem.

    Explanation

    This recurrence fits Master Theorem Case 2, where f(n)=O(nlog⁡balog⁡kn)f(n) = O(n^{\log_b a} \log^k n)f(n)=O(nlogb​alogkn). Here, nlog⁡ba=n3/2n^{\log_b a} = n^{3/2}nlogb​a=n3/2 and f(n)=O(n3/2)f(n) = O(n^{3/2})f(n)=O(n3/2). This means k=0k=0k=0. Case 2's result is Θ(nlog⁡balog⁡k+1n)\Theta(n^{\log_b a} \log^{k+1} n)Θ(nlogb​alogk+1n). Since f(n)f(n)f(n) is an 'O' bound, the result is O(n3/2log⁡0+1n)=O(n3/2log⁡n)O(n^{3/2} \log^{0+1} n) = O(n^{3/2} \log n)O(n3/2log0+1n)=O(n3/2logn).

  12. 12

    In the computation tree for T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), how many nodes are at level lll?

    1. A

      8l8^l8l nodes, reflecting the branching factor.

    2. B

      nln^lnl nodes, proportional to input size.

    3. C

      4l4^l4l nodes, reflecting the division factor.

    4. D

      Exactly lll nodes, based on the level count.

    Correct answer
    A

    8l8^l8l nodes, reflecting the branching factor.

    Explanation

    The 'a' term in aT(n/b)aT(n/b)aT(n/b) indicates the number of recursive calls made at each step, which corresponds to the branching factor. For T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), a=8a=8a=8, so there are 8l8^l8l nodes at level lll.

  13. 13

    For T(n)=8T(n/4)+O(nn)T(n) = 8T(n/4) + O(n\sqrt{n})T(n)=8T(n/4)+O(nn​), what is the input size for each node at level lll?

    1. A

      n/ln/ln/l, as the level determines reduction.

    2. B

      n/8ln/8^ln/8l, as there are 8 children.

    3. C

      n/4ln/4^ln/4l, as the problem size divides by 4.

    4. D

      n/(4⋅8)ln/ (4 \cdot 8)^ln/(4⋅8)l, due to combined factors.

    Correct answer
    C

    n/4ln/4^ln/4l, as the problem size divides by 4.

    Explanation

    The 'b' term in T(n/b)T(n/b)T(n/b) indicates how the problem size is reduced for each subproblem. Here, b=4b=4b=4, so the input size for each node at level lll is n/4ln/4^ln/4l.

  14. 14

    What common algorithmic strategy is suggested by a problem requiring log⁡k\log klogk time to find an item in an unbounded range?

    1. A

      Binary search or bisection method.

    2. B

      Linear scan from the beginning.

    3. C

      Randomized search to quickly converge.

    4. D

      Hashing the potential locations.

    Correct answer
    A

    Binary search or bisection method.

    Explanation

    A time complexity of log⁡k\log klogk for searching typically indicates the use of binary search or a bisection method, which repeatedly halves the search space. This is highly efficient for ordered data.

  15. 15

    When performing binary search on an unbounded set of planets (e.g., indexed 1, 2, 3...), how can a finite search range be established?

    1. A

      Assume a maximum planet index and search within it.

    2. B

      Start at planet 1 and linearly scan until a bound is found.

    3. C

      Query random planets until the target is located within a known interval.

    4. D

      Incrementally expand the search range by powers of 2 until the target is bracketed.

    Correct answer
    D

    Incrementally expand the search range by powers of 2 until the target is bracketed.

    Explanation

    To apply binary search to an unbounded set, one must first establish a finite upper bound. This is often done by visiting planets at indices like 20,21,22,…,2m2^0, 2^1, 2^2, \dots, 2^m20,21,22,…,2m until the target planet kkk is found to be less than or equal to 2m2^m2m. This process takes O(log⁡k)O(\log k)O(logk) time, after which a standard binary search can be performed within the range [2m−1,2m][2^{m-1}, 2^m][2m−1,2m].

  16. 16

    After establishing a finite range [L,R][L, R][L,R] for an unbounded search problem, what is the time complexity of the subsequent binary search?

    1. A

      O(log⁡R)O(\log R)O(logR), logarithmic with respect to the upper bound.

    2. B

      O(R)O(R)O(R), directly proportional to the upper bound.

    3. C

      O(1)O(1)O(1), as the range is now finite and known.

    4. D

      O(R−L)O(R - L)O(R−L), proportional to the size of the range.

    Correct answer
    A

    O(log⁡R)O(\log R)O(logR), logarithmic with respect to the upper bound.

    Explanation

    Once a finite range [L,R][L, R][L,R] has been established, a standard binary search can be performed. The time complexity of binary search on a range of size NNN is O(log⁡N)O(\log N)O(logN). If RRR is approximately kkk, then the search time is O(log⁡k)O(\log k)O(logk).

  17. 17

    A data structure for image layers needs to support 'import (add on top)' in O(n)O(n)O(n) and 'move below (reorder)' in O(log⁡n)O(\log n)O(logn). What combination of data structures would effectively meet these requirements?

    1. A

      A stack for imports and a queue for moves.

    2. B

      An array for imports and a hash table for moves.

    3. C

      A doubly-linked list for ordering and a sorted array/tree of pointers for searching.

    4. D

      Two separate arrays, one for ascending order and one for descending.

    Correct answer
    C

    A doubly-linked list for ordering and a sorted array/tree of pointers for searching.

    Explanation

    The O(n)O(n)O(n) import time suggests maintaining an explicit sequence (like a linked list) where adding to the top might involve shifting. The O(log⁡n)O(\log n)O(logn) 'move below' operation, requiring quick element location, points to a balanced binary search tree or sorted array (potentially with a two-finger approach for O(N)O(N)O(N) overall) that stores pointers to the linked list nodes. A doubly-linked list allows O(1)O(1)O(1) reordering once the nodes are found.

  18. 18

    Why would attaching pointers from a sorted array/tree to nodes in a doubly-linked list be beneficial for 'move below' operations?

    1. A

      It speeds up adding new elements to the top of the list.

    2. B

      It allows the linked list to be automatically sorted by ID.

    3. C

      It enables O(log⁡n)O(\log n)O(logn) lookup of specific elements in the linked list.

    4. D

      It reduces the memory footprint of the data structure.

    Correct answer
    C

    It enables O(log⁡n)O(\log n)O(logn) lookup of specific elements in the linked list.

    Explanation

    A sorted array or balanced binary search tree allows for O(log⁡n)O(\log n)O(logn) lookup of an element by its ID. By storing pointers to the actual nodes in the doubly-linked list, once an element's ID is found, its corresponding linked list node can be accessed in O(1)O(1)O(1) time, facilitating quick reordering.

  19. 19

    What is the time complexity of finding a specific image (by ID) within a doubly-linked list if no auxiliary data structure is used?

    1. A

      O(1)O(1)O(1), if the head is known.

    2. B

      O(n2)O(n^2)O(n2), due to potential nested iterations.

    3. C

      O(n)O(n)O(n), requiring a linear scan through the list.

    4. D

      O(log⁡n)O(\log n)O(logn), using a binary search approach.

    Correct answer
    C

    O(n)O(n)O(n), requiring a linear scan through the list.

    Explanation

    In a basic doubly-linked list, finding a specific element by its value (ID) requires traversing the list from the beginning (or end) until the element is found. In the worst case, this takes O(n)O(n)O(n) time, where nnn is the number of elements in the list.

  20. 20

    If a linked list element needs to be moved from one position to another, what is the time complexity of the actual splicing and re-linking, assuming pointers to the relevant nodes are already available?

    1. A

      O(1)O(1)O(1), as only a few pointers need to be updated locally.

    2. B

      O(n)O(n)O(n), as the entire list might need to be shifted.

    3. C

      O(log⁡n)O(\log n)O(logn), requiring a search for the new position.

    4. D

      O(nlog⁡n)O(n \log n)O(nlogn), for complex reordering tasks.

    Correct answer
    A

    O(1)O(1)O(1), as only a few pointers need to be updated locally.

    Explanation

    In a doubly-linked list, once you have pointers to the node to be moved and its new neighbors, the operation of removing it from its current position and inserting it into a new one involves only updating a constant number of pointers. This makes the actual splicing and re-linking an O(1)O(1)O(1) operation.

  21. 21

    In the 'brick blowing' problem, the wolf blows houses to the east, and houses with fewer bricks to the east also fall. If the wolf blows on house iii, what property must house jjj (where j>ij > ij>i) have to fall?

    1. A

      House jjj must be a 'special' house.

    2. B

      House jjj must have more bricks than house iii.

    3. C

      House jjj must have fewer bricks than house iii.

    4. D

      House jjj must be directly adjacent to house iii.

    Correct answer
    C

    House jjj must have fewer bricks than house iii.

    Explanation

    The problem states that when the wolf blows on a house, not only does that house fall, but 'also all the houses to the east of that house that have fewer bricks.' This is the critical condition for houses further down the line to fall.

  22. 22

    Consider a sequence of houses with brick counts. If you blow on a house with XXX bricks, how is the total damage calculated for that house?

    1. A

      It is simply 1, as only the blown house falls.

    2. B

      It is the sum of bricks of all houses to its east.

    3. C

      It is 1 (for the house itself) plus the count of houses to its west with more bricks.

    4. D

      It is 1 (for the house itself) plus the count of houses to its east with fewer bricks.

    Correct answer
    D

    It is 1 (for the house itself) plus the count of houses to its east with fewer bricks.

    Explanation

    The damage is the house blown (count 1) plus any house to its east (j>ij > ij>i) that has fewer bricks than the blown house (Bricksj<BricksiBricks_j < Bricks_iBricksj​<Bricksi​). This defines the rule for damage calculation.

  23. 23

    A house in Porkland is 'special' if it has no easterly neighbor or its adjacent easterly neighbor has at least as many bricks. If all but one house is special, what does this imply about the sequence of brick counts?

    1. A

      The sequence increases monotonically until one house, then decreases monotonically.

    2. B

      The sequence forms two segments, each sorted in increasing order.

    3. C

      The sequence is sorted in ascending order, with one exception.

    4. D

      The sequence is completely random, except for one house.

    Correct answer
    B

    The sequence forms two segments, each sorted in increasing order.

    Explanation

    A 'special' house indicates that Bricksi≤Bricksi+1Bricks_i \le Bricks_{i+1}Bricksi​≤Bricksi+1​. If all but one house is special, it means the sequence is generally increasing, with exactly one point where Bricksi>Bricksi+1Bricks_i > Bricks_{i+1}Bricksi​>Bricksi+1​. This results in two increasing segments, connected at the non-special house (which is the start of the second segment).

  24. 24

    Given the 'special house' condition (all but one house special), if the wolf blows on a house in the first increasing segment (before the decrease), what part of the array can it affect?

    1. A

      Only houses to its west with more bricks.

    2. B

      Only houses in the second increasing segment (to its right).

    3. C

      All houses in the array, regardless of position.

    4. D

      Only houses within its own increasing segment.

    Correct answer
    B

    Only houses in the second increasing segment (to its right).

    Explanation

    If a house is in the first increasing segment, all houses to its immediate right within that segment will have an equal or greater number of bricks. The rule states only houses with fewer bricks fall. Therefore, only houses in the second (decreasing or later increasing) segment could potentially fall, as these are the only ones to its right that could be smaller.

  25. 25

    To count the damage for each house in O(N)O(N)O(N) time when the array consists of two increasing segments, which algorithmic technique is most suitable?

    1. A

      Sorting the entire array and then performing a linear scan.

    2. B

      A nested loop, comparing each house to all subsequent houses.

    3. C

      Binary search for each house to find smaller ones to its right.

    4. D

      A two-finger (or two-pointer) algorithm, moving pointers along the array.

    Correct answer
    D

    A two-finger (or two-pointer) algorithm, moving pointers along the array.

    Explanation

    A two-finger (or two-pointer) algorithm is ideal here. Since the left segment's values are increasing, the range of values in the right segment that they can blow over also changes predictably (monotonically). By maintaining one pointer in the left segment and another in the right, both pointers only advance, leading to an O(N)O(N)O(N) total time complexity.

Question 1 of 25
Question 1 of 25

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent