Question

In: Computer Science

For any sequence, S[1…n], of length n, a basement is defined as a contiguous subsequence of...

For any sequence, S[1…n], of length n, a basement is defined as a contiguous subsequence of S, which we denote by S[ i, i + 1, ..., j – 1, j ], with 1 £ i < i + 1 <   j – 1 < j £ n, satisfying the following conditions:

  • S[ i ] > S[ i + 1]
  • S[ j – 1] < S[ j ]
  • 1 £ i < i + 1 <   j – 1 < j £ n
  • S[ r ] = S[ t ] for all i < r, t < j

The length of a basement is defined as the count of the elements that make up the basement sequence. As an example, for the sequence

S = [3, 2, 0, 0, 0, 0, 4, 4, 6, 5, 5, 5, 3, 7, 7, 7, 7]

We note that [2, 0, 0, 0, 0, 4,] is a basement (which you can verify, using the definition of a basement), and the length of the basement [2, 0, 0, 0, 0, 4,] is 6. The starting index for this basement is 2 and the ending index for this basement is 7. We also note that [6, 5, 5, 5, 3] is NOT a basement.

  1. Explain why [6, 5, 5, 5, 3] is NOT a basement by clearly pointing out the criteria that holds, as well as the criteria that fails to hold. Circle your choice, or highlight it.      (2 points)

S[ i ] > S[ i + 1]

HOLDS / FAILS

1 £ i < i + 1 <   j – 1 < j £ n

HOLDS / FAILS

S[ j – 1] < S[ j ]

HOLDS / FAILS

S[ r ] = S[ t ] for all i < r, t < j

HOLDS / FAILS

  1. Explain why [7, 7, 7, 7] is NOT a basement by clearly pointing out the criteria that holds, as well as the criteria that fails to hold. Circle your choice, or highlight it.      (2 points)

S[ i ] > S[ i + 1]

HOLDS / FAILS

1 £ i < i + 1 <   j – 1 < j £ n

HOLDS / FAILS

S[ j – 1] < S[ j ]

HOLDS / FAILS

S[ r ] = S[ t ] for all i < r, t < j

HOLDS / FAILS

  1. Identify ALL of the basements of the following sequence: (2 points)

S = [2, 1, 4, 1, 1, 0, 6, 6, 6, 5, 5, 5, 10, 3, 3, 3, 3, 3, 3, 6, 6, 4, 0, 10, 0]

Determine an efficient algorithm to find the starting and ending indices of a basement in a sequence, S, whose length is maximal (in the event that there are multiple basements whose length is maximal, return the starting and indices of ONE of them).   (10 points total: Full points given for a linear algorithm).

Solutions

Expert Solution

[6, 5, 5, 5, 3] is NOT a basement.

Conditions it is holding are:

S[ i ] > S[ i + 1] because 6>5

1 £ i < i + 1 <   j – 1 < j £ n

S[ r ] = S[ t ] for all i < r, t < j (all are having values 5)

Conditions it is not holding(FAILING) is:

S[ j – 1] < S[ j ] Because 5 is not less than 3

[7,7,7,7] is NOT a basement.

Conditions it is holding:

1 £ i < i + 1 <   j – 1 < j £ n

S[ r ] = S[ t ] for all i < r, t < j (all have value 7)

Conditions it is failing are:

S[ i ] > S[ i + 1] because 7 is not greater than 7

S[ j – 1] < S[ j ] because 7 is not lesser than 7

S = [2, 1, 4, 1, 1, 0, 6, 6, 6, 5, 5, 5, 10, 3, 3, 3, 3, 3, 3, 6, 6, 4, 0, 10, 0]

All the basements of S are:

[2,1,4]

[1,0,6]

[6,5,5,5,10]

[10,3,3,3,3,3,3,6]

[4,0,10]

Algorithm to find maximal Basement:

1. Let S be sequence with length of S be n.

2. start= -1, end= -1, curr_start = -1, curr_end = -1. Initialize start, end, current start and current end to -1. Start and end represent the final maximal start and end index whereas current start and current end represent the temporary start and end values.

3. Assign from index(i) 2 and loop till i = n-1

4. If S[i-1]<S[i] or S[i+1]<S[i] then set cur_start = -1, cur_end = -1

5. else if S[i-1]>S[i] and S[i+1]>S[i] then set cur_start = i-1 and cur_end = i+1 and if end - start +1 <= cur_end-cur_start+1 then set start = cur_start and end = cur_end and cur_start = -1 and cur_end = -1.

6. else if S[i-1]>S[i] and S[i]=S[i+1] then set cur_start = i-1, cur_end = -1

7. else if S[i-1] = S[i] and cur_start is not -1 and S[i] < S[i+1] then set cur_end = i+!. If end-start+1 <= cur_end - cur_start+1 then set start = cur_start, end = cur_end and cur_start = -1, cur_end = -1.

8. The start and end are the maximal basement starting and ending indices.

Time complexity of this algorithm = O(n) = Linear time complexity


Related Solutions

2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
Write the first four terms of the sequence defined by the recursive formula a1 = 2, an = an − 1 + n.
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function...
#2 The Jacobsthal sequence is defined by J(1)=J(2)=1 and J(n)=J(n-1)+2J(n-2). Use recursion to write a function that takes in a positive integer n and returns the nth Jacobsthal number. >>> J(8) 85 >>> J(9) 171 #3 Use recursion to write a function that takes in a positive integer n and returns all n digit numbers containing only odd digits. >>> f(1) [1, 3, 5, 7, 9] >>> f(2) [11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, 53,...
1. A length-10 discrete-time signal x[n] is defined by the values { 6, 1, 1, 6,...
1. A length-10 discrete-time signal x[n] is defined by the values { 6, 1, 1, 6, 1, 1, 6, 1, 1, 0 }. After taking the 10-pt DFT, X[k], determine the DC value, i.e., X[0]. 2. Suppose that the N-point DFT of the signal x[n]x[n] is X[k]. A new signal y[n] is formed by adding a constant: y[n]=x[n]+1 for n=0,1,2,…,N−1n=0,1,2,…,N−1 The N-point DFT of Y[k] is:
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many...
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many n-bit binary strings are there? How many n-bit binary strings b1,…,bn are there such that b1b2≠00? In other words, how many n-bit binary strings don't begin with 00? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and b2b3≠11? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and such that b2b3≠01?
1. prove s(n, k) = s(n − 1, k − 1) − (n − 1)s(n −...
1. prove s(n, k) = s(n − 1, k − 1) − (n − 1)s(n − 1, k). 2. What is ∑n k=0 s(n, k)?
1a. Consider the sequence {?? }n≥0 which starts 1,2,7,20,61,122,..., defined by the recurrence relation ?? =...
1a. Consider the sequence {?? }n≥0 which starts 1,2,7,20,61,122,..., defined by the recurrence relation ?? = 2??−1 + 3??−2 and initial conditions ?0 = 1, ?1 = 2. Solve the recurrence relation. That is, find a closed formula for ??. Show your work. The abandoned field behind your house is home to a large prairie dog colony. Each week the size of the colony triples. However, sadly 4 prairie dogs die each week as well (after the tripling occurs). Consider...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
Let {an} be a sequence defined recursively by a1 = 1 and an+1 = 2√ 1...
Let {an} be a sequence defined recursively by a1 = 1 and an+1 = 2√ 1 + an where n ∈ N (b) Does {an} converge or diverge? Justify your answer, making sure to cite appropriate hypotheses/theorem(s) used. Hint : Try BMCT [WHY?]. (c) (Challenge) If {an} converges then find its limit. Make sure to fully justify your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT