Given a non-negative integer array A[1..n] (that is, A contains numbers from the set {0, 1,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Given a non-negative integer array A[1..n] (that is, A contains numbers from the set {0, 1, 2, 3, 4, ...}), the next smaller value of A (NSVA) is an integer array of length n, where NSVA[i], 1 ≤ i ≤n, is defined as follows: 1. if A[i]=0 then NSVA[i] = 0 2. otherwise, NSVA[i] = j, where (a) for every h E [1..j-1], A[i] ≤ A[i+h], and (b) either i + j= n+1 or A[i] > A[i + j]. In short, NSV[i] shows how far one should trace forward from A[i] to reach a smaller value than A[i]. Write an algorithm to compute the NSV array in O(n) time, using the stack data structure. Below is an example of an NSV array. [6 marks] i A 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 01 3 3 7 7 15 5 0 NSVA 0 8 4 3 2 1 3 2 1 0 2 2 6 6 0 4 4 4 3 2 1 0 2 1 Given a non-negative integer array A[1..n] (that is, A contains numbers from the set {0, 1, 2, 3, 4, ...}), the next smaller value of A (NSVA) is an integer array of length n, where NSVA[i], 1 ≤ i ≤n, is defined as follows: 1. if A[i]=0 then NSVA[i] = 0 2. otherwise, NSVA[i] = j, where (a) for every h E [1..j-1], A[i] ≤ A[i+h], and (b) either i + j= n+1 or A[i] > A[i + j]. In short, NSV[i] shows how far one should trace forward from A[i] to reach a smaller value than A[i]. Write an algorithm to compute the NSV array in O(n) time, using the stack data structure. Below is an example of an NSV array. [6 marks] i A 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 01 3 3 7 7 15 5 0 NSVA 0 8 4 3 2 1 3 2 1 0 2 2 6 6 0 4 4 4 3 2 1 0 2 1
Expert Answer:
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these computer network questions
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
A compare-exchange operation on two array elements A[i] and A[j], where i < j, has the form COMPARE-EXCHANGE (A, i, j) 1 If A[i] > A[j] 2 exchange A[i] with A[j] After the compare-exchange operation,...
-
QUESTION ONE Matrix X sells computer hardware and software components as well as other ancillary items to customers all over South Africa. Matrix X also provides installation of hardware and software...
-
A string is wrapped several times around the rim of a small hoop with radius 8.00 cm and mass 0.180 kg. The free end of the string is held in place and the hoop is released from rest (Fig. 10.46)....
-
From the following, calculate the net cash flow from operating activities using the indirect method: Merchandise Inventory Accounts Receivable Prepaid Insurance Accounts Payable Accrued Salaries 2012...
-
From 2008 to 2015, auto loan rates in the United States declined from around 8% to near historic lows of around 4%. At the same time, auto sales increased dramatically. How, if at all, does this...
-
Large Lots is planning a seven-day promotion on a discontinued model of 31" color television sets. At a price of $575 per set, the daily demand for this type of TV has been estimated as follows:...
-
1. Assuming firms in a market face a linear downward-sloping demand curve and constant marginal and average costs, carefully explain where equilibrium price would be if it were a competitive market....
-
On March 1, Marigold Inc sells 1,400 common shares to its employees at $26 per share and lends the money to the employees to buy the new shares in exchange for a note receivable The employees pay 45...
-
Why is it necessary to start below room temperature and stop above room temperature by the same temperature difference?
-
A taxpayer uses a vehicle for his sole proprietorship. He purchased the vehicle for $35,000 in Year One and uses it 100% for business. By the end of Year Three he has taken $27,440 in depreciation...
-
Silicon Inc. has provided the following information for the year ended December 31, Year 1. Master Budget Actual Costs 5,000 units 4,500 units Direct materials $ 35,000 $ 32,500 Direct labor 15,000...
-
Annabelle, Boden, and Christy form ABC Partnership as equal partners. Annabelle and Boden contribute cash of $50,000 each and Christy contributes equipment with a $100,000 fair market value and...
-
On January 1, Jim purchased 50 shares of Potomac Corporation stock for $50,000. Potomac is a calendar year S corporation and Jim's share of the company's earnings for the year is $20,000. His...
-
Coco Corp. has the following selected account balances as of 12/31/Year5: Cash $13,500 Accounts receivable $128,000 Allowance for uncollectible accounts $12,000 (NORMAL, credit balance- a negative...
-
Two emerging retail issues attributable to the increased online ordering are O a. Reverse logistics and product losses (yield) O b. Globalization and localization O c. Inflation and theft O d. Omni...
-
State whether each statement is true or false. If false, give a reason. {purple, green, yellow} = {green, pink, yellow}
-
Define lcm (a 1 , a 2 , . . . ,a n ) to be the least common multiple of the n integers a 1 , a 2 , . . . ,a n , that is, the smallest nonnegative integer that is a multiple of each a i . Show how to...
-
How would you modify Strassens algorithm to multiply n n matrices in which n is not an exact power of 2? Show that the resulting algorithm runs in time (n lg 7 ).
-
A probability distribution function P(x) for a random variable X is defined by P(x) = Pr {X x}. Suppose that we draw a list of n random variables X 1 , X 2 , . . . ,X n from a continuous probability...
-
At January 1, 2015, Cheng Company reported retained earnings of 20,000,000. In 2015, Cheng discovered that 2014 depreciation expense was understated by 4,000,000. In 2015, net income was 9,000,000...
-
Cherokee Construction Company changed from the cost-recovery to the percentage-of-completion method of accounting for long-term construction contracts during 2015. For tax purposes, the company...
-
In 2015, Bailey Corporation discovered that equipment purchased on January 1, 2013, for 50,000 was expensed at that time. The equipment should have been depreciated over 5 years, with no residual...
Study smarter with the SolutionInn App