1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sashaice [31]
3 years ago
15

Describe an algorithm that, given n integers in range 0 to k, preprocesses its input and then answers any query about how many o

f the n integers fall into a range [a...b] in O(1) time. Your algorithm should use O(n+k) preprocessing time.
Computers and Technology
1 answer:
pogonyaev3 years ago
8 0

Answer:

Explanation:

Assume that our integers are stored in array A[1 · · · n] and thus, length[A] = n. We need two additional  arrays B[1 · · · k] and C[1 · · · k].

Our algorithm will first initialize all elements of array B to 0. This step

requires O(k) time.

Next, for each element of array A (i.e., A[i] for i = 1, 2, · · · , n), we will increment

B[A[i]]. Observe that this step will require O(n) time and B[j] for j = 1, 2, · · · , k, now contains the number

of elements of A having value j. Finally, make C[1] = B[1] and for each element l of array C where

l = 2, 3, · · · , k, we will compute C[l] = B[l] + B[l − 1]. This step takes O(k) time.

Now, to answer any query about how many of n integers fall into a range [a · · · b], we simply compute

C[b] − C[a] + B[a].

Observe that this computation requires only O(1) time after the O(n + k) preprocessing

time.

You might be interested in
Answer pls pls pls pls ​
o-na [289]

Answer:

Flyers and pamphlets, and TV ads most likely are the answers.

4 0
2 years ago
One vulnerability that makes computers susceptible to walmare is:
sammy [17]
C hope it’s right :))) !
6 0
3 years ago
Read 2 more answers
A simple way to think of the Excel application is as a giant ______.
Naddik [55]
Spreadsheet ......zzzzzz
8 0
3 years ago
Read 2 more answers
Communication competence is _____.
posledela
Communication competence is the ability to interact with others in a manner that is honest and appropriate for the situation, individuals, and task.
5 0
3 years ago
How do buffers work?
sasho [114]
A buffer is able to resist pH change because the two components (conjugate acid and conjugate base) are both present in appreciable amounts at equilibrium and are able to neutralize small amounts of other acids and bases (in the form of H3O+ and OH-) when the are added to the solution.
5 0
3 years ago
Other questions:
  • Which piece of information is most useful to an identity thief? *
    6·1 answer
  • Think of a game you are familiar with and identify at least three team responsibilities which were required to make the game, on
    5·1 answer
  • The default primary key for an access database is the id field true or falser
    7·1 answer
  • How do switches and bridges learn where devices are located on a network?
    5·1 answer
  • Modify your solution to Problem 8.24 so that one (and only one) child installs a Segmentation-fault handler which prints an erro
    10·1 answer
  • Why should ERP architecture include a discussion on organizational structure, business processes, and people, instead of just in
    5·1 answer
  • Need the answer ASAP!!!
    14·1 answer
  • 11. You tried to turn on your desktop computer, but your desktop computer would not even turn on. You pressed the power button,
    8·1 answer
  • How is unqualified assumptions a disadvantage of communication​
    14·1 answer
  • As part of your regular system maintenance, you install the latest operating system updates on your Windows 10 computer. After s
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!