101 algorithms questions you must know Arora A - The full ebook version is just one click away
101 algorithms questions you must know Arora A - The full ebook version is just one click away
com
https://textbookfull.com/product/101-algorithms-questions-
you-must-know-arora-a/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/law-101-everything-you-need-to-know-
about-american-law-5th-edition-jay-m-feinman/
textbookfull.com
https://textbookfull.com/product/digital-signal-processing-101-second-
edition-everything-you-need-to-know-to-get-started-michael-parker/
textbookfull.com
https://textbookfull.com/product/must-know-math-grade-6-1st-edition-
nicholas-falletta/
textbookfull.com
https://textbookfull.com/product/5-conversations-you-must-have-with-
your-son-courtney/
textbookfull.com
You Must Not Miss 1st Edition Katrina Leno [Leno
https://textbookfull.com/product/you-must-not-miss-1st-edition-
katrina-leno-leno/
textbookfull.com
1001 Movies You Must See Before You Die 7th Edition Steven
Jay Schneider (Editor)
https://textbookfull.com/product/1001-movies-you-must-see-before-you-
die-7th-edition-steven-jay-schneider-editor/
textbookfull.com
https://textbookfull.com/product/can-we-know-anything-a-debate-little-
debates-about-big-questions-bryan-frances/
textbookfull.com
https://textbookfull.com/product/organic-chemistry-25-must-know-
classes-of-organic-compounds-de-gruyter-textbook-2nd-edition-
elzagheid/
textbookfull.com
How Well Do You Know Your Bible?: Over 500 Questions and
Answers to Test Your Knowledge of the Good Book 2nd
Edition James Bell
https://textbookfull.com/product/how-well-do-you-know-your-bible-
over-500-questions-and-answers-to-test-your-knowledge-of-the-good-
book-2nd-edition-james-bell/
textbookfull.com
101 Algorithms Questions
You Must Know
by
Amrinder Arora
Department of Computer Science,
The George Washington University
101 Algorithms Questions You Must Know
Copyright© 2018, Amrinder Arora
All rights reserved. No part of this book may be reproduced or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording,
or by an information storage and retrieval system - except by a reviewer who may
quote brief passages in a review to be printed in a magazine or newspaper - without
permission in writing from the publisher.
Table of Contents
Acknowledgments
Section 1: Warm Up Questions
Section 2: Asymptotic Analysis
Solution
We recollect that log (xy) = log x + log y .
Therefore, using log 10 2 = 0.3010 and log 10 3 = 0.4771 , we can
calculate log 10 6 = 0.3010 + 0.4771 = 0.7781
Solution
Suppose S = ∑ i=1 to n i 2 i
S = 1×2+2×22+3×23+
… +n×2n
That is, 2S = 1×22+ 2 × 23 + 3 × 2 4 +
…. + (n-1) × 2 n + n × 2 n+1
Subtract the second equation from the first, and we obtain:
–S = 1 × 2 + 1 × 2 2 + 1 × 2 3 + ... + 1 × 2 n – n 2 n+1
= 2 + 2 2 + 2 3 + ... + 2 n – n 2 n+1
= 2(1 + 2 1 + 2 2 + ... + 2 n-1 ) – n 2 n+1
By using geometric progression, we obtain :
–S = 2 (2 n – 1) – n 2 n+1
Solution
While we can always use principle of mathematical induction (PMI)
to solve these kinds of problems, that requires us to know or guess
the solution. If we do not have a good guess, we may need to solve
it directly. The good news is that although it is a bit more
complicated, like the previous question, this question can also be
solved using term sliding.
S=122+22 22+32 23+… +n22n
=> 2S = 1 2 2 2 + 2 2 2 3 + 3 2 2 4 + …. + (n-1 2 ) 2 n + n 2 2 n+1
Subtracting the second term from the first one, we obtain that:
– S = 1 2 2 + (2 2 – 1 2 ) 2 2 + (3 2 – 2 2 ) 2 3 + … + (n 2 – (n-
1) 2 ) 2 n – n 2 2 n+ 1
Since i 2 – (i-1) 2 can be written as 2i-1 , we can now write the
previous equation as:
–S = ∑ i=1 to n (2i-1) 2 i – n 2 2 n+1
Solutio n
Both of these terms can be independently solved and compared.
We can observe that: ∑ 1 to n i 2 = n (n+1) (2n+1)/6 , while ∑ 1 to n*n
= n 2 (n 2 +1)/2 .
Thus, the second term is significantly larger for larger values of n.
Solution
We observe that the outer loop (on counter j ) just increments one by
one. So, that loop runs in O(n) time.
The inner loop runs on counter k , and the value of k gets squared
every time, starting with 2 .
Therefore, the value of k jumps from 2 , to 4 , to 16 to 256 . We
j=1 observe that after m iterations of the loop, the value of
while (j < n) {
k=2 k becomes 2 2^m . The loop terminates when the value
while (k < n) { of k becomes larger or equal to n , that is, 2 2^m ≥ n ,
Sum +=
a[j]*b[k] that is, m ≥ log log (n) . Therefore the inner loop runs
k=k*k in O(log log n) time .
}
j++ Since the two loops are nested, the entire program runs
}
in O(n log log n) time.
Since the loops are nested, the entire program runs in constant, that
is, O(log n log log n) time.
Solution
Since f 1 (n) = O(g 1 (n)) , therefore there exist constants c 1 and n 1 ,
such that f 1 (n) ≤ c 1 g 1 (n)) for all values of n ≥ n 1 .
From the two choices of n 1 and n 2, we select the larger value, let us
call it n 3 . Similarly, we select the larger value from c 1 and c 2 , let
us call it c 3 . Therefore, we have:
f 1 (n) ≤ c 3 g 1 (n)) for all values of n ≥ n 3
Solution
Since f 1 (n) = O(g 1 (n)) , therefore there exist constants c 1 and n 1 ,
such that f 1 (n) ≤ c 1 g 1 (n)) for all values of n ≥ n 1 .
Similarly, since f 2 (n) = O(g 2 (n)) , therefore there exist constants c
2 and n 2 , such that f 2 (n) ≤ c 2 g 2 (n)) for all values of n ≥ n 2 .
From the two choices of n 1 and n 2, we select the larger value, let us
call it n 3 . We define a new constant c 3 = c 1 c 2 . Therefore, we
have:
f 1 (n) ≤ c 1 g 1 (n)) for all values of n ≥ n 3
Solution
We would like to evaluate lim n->infinity f(n)/g(n) , and we again use
the helpful L’Hopital’s rule, which states that assuming certain
conditions apply, lim n->infinity f(n)/g(n) = lim n->infinity f’(n)/g’(n) ,
where f’(n) and g’(n) represent the first derivatives of functions f(n)
and g(n) respectively.
Applying this to our case, we obtain that:
f’(n) = 2n and g’(n) = 80 (log n) 79 / n
Therefore, we have that:
lim n->infinity f(n)/g(n) = lim n->infinity 2n 2 /80(log n) 79
We can repeat this process a few more times (79 to be precise!), and
at that point, we have that:
lim n->infinity f(n)/g(n) = 2 80 n 2 /80! which obviously approaches
infinity as n tends to infinity .
Since lim n->infinity f(n)/g(n) = infinity , we conclude that n 2 = ω ((log
n) 80 ), or equivalently stated, (log n) 80 = o(n 2 ) . This relationship
between log functions and polynomial functions is very standard,
and all the following results are also correct and can be proved using
the same methodology.
(log (n+1)) 6 = o(n 2 )
(log (n)) 63 = o(n 1.1 )
log 63 (n) = o(n 1.1 ) // log k (n) is the short hand
notation for writing (log(n)) k
(log (n + n 2 + n 3 )) 2300 + (log (n+n 2 )) 613 = o(n
1.0001 )
=0
Therefore, we conclude that 10 n = o(2 n^2 ) .
We can see that after a finite number of steps, this limit can be
evaluated to be 0 .
Therefore, as expected, we have that n 100 = o(2 sqrt(n) ).
For a child five years old, give a teaspoonful three or four times a
day; older or younger children in proportion. If the cough is hard
and dry, ten to twenty drops of syrup of ipecac alone should be
given instead of the mixture, and when the cough is loosened, the
mixture can be again administered. When in the course of the
affection the breathing suddenly becomes labored, and the fever
increases, it is fair to presume that the case is complicated with
pneumonia.
(i) Eruptive fevers, as their name implies, are characterized by an
eruption or exanthema. The most virulent of this class is smallpox.
The eruption of this disease is of the nature of vesicles, or pustules,
while that of measles, scarlatina, and rose rash is dry, and is
properly called a rash. Chickenpox, however, has also vesicles and
pustules, and for this reason it is very liable in times of an epidemic
of smallpox to be mistaken for a mild form of the latter disease. For
obvious reasons it is not proper to consider smallpox in this
connection; its gravity and its management require experience, and
further, it generally comes under special quarantine regulations of
the proper constituted authorities.
The eruptive fevers are all divided into three stages, namely: a
stage of invasion or development; a second stage when the eruption
appears, and while it lasts; and a third stage, that of desquamation,
when the eruption begins to fade or exfoliate in branny scales.
(j) Measles is generally a mild and not serious disease, and only
attacks the individual but once in a lifetime; only through gross
carelessness the disease becomes complicated, and then it may
become a very dangerous affection. It begins with all the symptoms
of a common cold. There is frequent sneezing, and an acrid muco-
serous discharge from the nostrils. The eyes are irritable, reddened
and watery, and there is more or less intolerance of light. The voice
becomes hoarse and there is always a bronchitis present which is
characterized by a dry, harsh cough. The patient is generally
feverish, alternating with chilly sensations or shivering; the appetite
is poor or absent and in some cases there is nausea and vomiting.
The children feel drowsy; they complain of pain in the head and
limbs and want to lounge around. The bowels may be constipated,
but diarrhœa supervenes in a certain proportion of cases. In nervous
children convulsions may occur; bleeding from the nose and false
croup are not infrequently met with in the development of this
disease. The duration of the first stage varies greatly in different
individuals, and comprises a period extending from one to seven
days. The eruption begins generally on the temples and forehead,
whence it extends over the head and neck, thence down the back
and over the entire body, occupying in its development from thirty-
six to forty-eight hours. The eruption bears a resemblance to flea
bites at first; it appears as minute red specks which gradually
enlarge and become slightly elevated and arrange themselves in
circular clusters. The portions of the skin that are free from the
eruption retain their white appearance; the face is more or less
swollen and the eyelids puffed. In some patients there is
considerable annoyance from itching in the skin. The cough and
bronchitis continue to be prominent symptoms, and the
expectoration, consisting of yellowish sputa becomes abundant. In
some children the fever runs very high in this stage and they
become delirious and restless, but this is only temporary, for it
generally diminishes with the eruption on the third or fourth day.
When the eruption begins to fade the third stage of the affection
is inaugurated, and when there exist no complications, the patient
may now be considered on the way to recovery which takes from
four to eight days longer.
The treatment in measles should consist in good nursing, rather
than in medication. Owing to the inflammation of the membranes of
the eyes, the patient should be kept in a darkened chamber, and the
eyes occasionally bathed with a solution of borax, by dissolving half
a teaspoonful in a tumblerful of water. Good judgment forbids that
the patient should be sweltered, but that he should be kept
comfortably warm and never allowed to cool off suddenly is also
very important. When the eruption is slow to develop a good sweat
will often bring it out; so will undue exposure, to cold drafts and the
transportation out of a warm bed into a cold one or drinking
immoderately of cold drinks either delay the development of the
eruption or drive it back, and from this undoubtedly dangerous
complications arise, like pneumonia, diphtheritic croup, and
convulsions. A mouthful of cold water now and then is harmless, but
on the whole the drinks should be quite warm; the cough and
bronchitis alone would require that.
Warm milk thoroughly beaten is the most suitable form of diet;
broths and soups may be given for a change, so can a mixture of
equal parts of weak hot tea and milk. The bowels should be moved
with a mild laxative and if the fever runs very high, ten to fifteen
drops of the sweet spirits of nitre, for a child five years old, in half
wineglass of water every few hours will generally reduce the
temperature. For the itching, the skin should be rubbed with equal
parts of glycerine and warm water. The cough is generally the most
troublesome feature, and the only symptom requiring regular
medication; for this a good general cough mixture will serve every
purpose, such as:
Mix and give to a child four years old a teaspoonful every four
hours; older or younger children in proportion.
(k) Rose rash, sometimes called false or German measles is a
comparatively trivial affection and of very little importance, for it
never has any serious complications and lasts only twenty-four or
forty-eight hours in the majority of cases. It is often mistaken for
measles, and one attack affords no protection against recurrences.
The eruption appears in small rose-colored spots or patches which
are not elevated. It does not commence on the head, but appears
on different parts of the body. The eruption may be preceded by
headache, loss of appetite, occasionally vomiting, and more or less
fever or chilly sensations.
The affection of the eyes and air passages, especially the
bronchitis which is characteristic of measles, are wanting in rose
rash, and when we hear of children having had measles several
times it is reasonable to presume that it was rose rash instead. This
eruption hardly calls for treatment, but a mild laxative and a
regulated diet would fulfill all requirements.
(l) Scarlet fever or scarlatina has received its name from the color
of its eruption. This affection presents itself differently in different
cases. It may be so mild in its attack that it constitutes a trifling
ailment and again it may be so severe that life is seriously
threatened, and destroyed in a few days. This has formed the basis
of dividing scarlatina into three varieties, namely: simple scarlatina,
diphtheritic scarlatina and malignant scarlatina.
The fever, as a rule, is notably higher than in other eruptive
fevers. The attack may begin with a chill, nausea and vomiting and
headache. There is also bleeding from the nose in a certain
proportion of cases. The most constant sign is redness and more or
less swelling of the throat, either with or without a sense of soreness
and pain in the act of swallowing. The stage of development lasts in
the average twenty-four hours, although exceptionally it may appear
in a few hours after the first symptoms of the disease have
appeared. The eruption often begins on the back first, and from
there rapidly spreads over the entire body in twenty-four hours. It
greatly differs from the eruption in measles in not allowing any
healthy or white skin to intervene between the red specks, but the
entire skin has a reddish blush. This has given rise to the expression
boiled lobster appearance in scarlatina, and indeed nothing could
illustrate the color of the skin better than by comparing the one with
the other. The eruption is sometimes accompanied with a great deal
of itching and burning and reaches its fullest development on the
third day after its first appearance; it lasts from four to six days.
The extent and redness of the eruption varies greatly in different
individuals, in some it is very slight and transient while in others
there is not a spot as large as a dime which is not covered. Yet a
very light attack in one patient when communicated to another
individual may develop in that one a most malignant form. One
attack secures against a recurrence.
The throat affection seems to modify the eruption of the skin for
in some instances in which the throat is very bad, the skin eruption
is comparatively slight. There may be simply a little redness over the
tonsils, extending more or less over the soft palate, or on the other
hand, the disease in the throat may become in every respect equal
to if not identical with the worst type of diphtheria. Some writers
consider the sore throat of scarlatina and that of diphtheria identical,
and from a practical point of view there is certainly no difference,
and the very best results are obtained when all severe sore throats
of scarlatina are treated precisely as if they were diphtheria; this has
always been my practice.
The tongue is quite distinctive of the affection; early in the
development of the disease it is generally furred, but later the
coating peels off in patches, and these spots present the appearance
of a ripe strawberry; sometimes the entire surface of the tongue
looks as if cayenne pepper or red sand had been sprinkled on it.
The fever generally increases during the eruptive stage, and the
skin becomes very dry and hot. The pulse may run up to 130 to 140
per minute, while the temperature runs up as high as 106°
Fahrenheit; this state of affairs places the patient in imminent
danger, he now may become restless and even delirious. In a certain
proportion of cases the kidneys become involved and albuminuria is
a result, but this usually passes off with the improvement and
recovery of the patient.
In the third stage the cuticle begins to exfoliate in the form of
branny scales. In the absence of serious complications this stage
marks the beginning of convalescence; the fever subsides, the
appetite returns, and the soreness and redness of the throat
disappear.
The treatment of this affection always depends upon the nature
of the case. Simple scarlatina requires no medical treatment; the
precaution and intelligence which are comprised in good nursing
supply everything that is required. There is not the same danger of
the eruption striking in with scarlatina as there is with measles, and
the patient need not be kept so warmly covered. When the fever
runs very high, sponging the surface with cool water is very grateful
and reduces the temperature. After the eruption has thoroughly
developed, the water for sponging off may be very cold without the
least danger, and this may be repeated as often as comfort or the
high temperature demands. To relieve the burning and itching of the
skin, the entire body should be rubbed over with glycerine night and
morning; this relieves the system and makes the skin moist and
supple. If the throat is only reddened, a teaspoonful of chlorate of
potash dissolved in a cupful of hot water and when cold used as a
gargle and a teaspoonful of the solution swallowed at the same time
every two hours is all that is necessary. The diphtheritic scarlatina is
treated precisely similar to and according to the instructions laid
down for diphtheria in another part of this work. Move the bowels
freely with the Femina laxative syrup.
(m) Chickenpox has neither distressing symptoms nor is it
significant as regards danger. It is an eruptive fever which
occasionally affects adults, but for the most part children. The
eruption is generally preceded by a slight fever and nausea, and
appears first on the body and afterwards on the scalp where it is
usually more abundant. There always remains healthy white skin
between the vesicles, which at first contain a transparent liquid,
hence called by the Germans waterpox, which afterward becomes
cloudy or opaline. The eruption begins to dry up from the fifth to the
seventh day forming granular crusts that are sometimes followed by
pitting. The disease is contagious and develops twelve to seventeen
days after the exposure. Chickenpox claims no treatment; its only
point of interest is its resemblance to varioloid, but as a successful
vaccination guarantees against the latter, and as the vesicles of
varioloid have a central depression while those of chickenpox have
not, the individuality of the affection is readily established.
CHAPTER XXXI.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com