Question #71969

Use pseudocode to describe an algorithm that displays the last occurrence of each element in an input sequence a_0,a_1,…,a_n. In other words, if an element appears more than once in the sequence, only the last occurrence is displayed.

Example:

4, -5, 3, 2, 3, -1, 4, 3  displays -5, 2, -1, 4, 3

Determine the running time of your algorithm as a function of n (in the worst, best and average case) and then use asymptotic notation to simplify it.
1

Expert's answer

2017-12-19T10:24:07-0500

Answer on Question#71969 – Math | Discrete Mathematics

Use pseudocode to describe an algorithm that displays the last occurrence of each element in an input sequence a0,a1,,ana_0, a_1, \ldots, a_n. In other words, if an element appears more than once in the sequence, only the last occurrence is displayed.

Example:

4, -5, 3, 2, 3, -1, 4, 3 \diamond displays -5, 2, -1, 4, 3

Determine the running time of your algorithm as a function of nn (in the worst, best and average case) and then use asymptotic notation to simplify it.

Answer


Declare array array1
Declare array array2
For index = 0 to Length(array1) - 1
    Input array1[index]
End For
For index = 0 to Length(array1) - 1
    Set array2[index] = 1
End For
For index1 = 0 to Length(array1) - 2 //so, the array length is 3 minimum
    Set w = 0
    For index2 = index1 + 1 to Length(array1)
        If array1[index1] == array1[index2] then
            Set array2[index1] = 0
        End For
    End For
For index = 0 to Length(array1) - 1
    If array2[index] == 1 then
        Display array1[index1]
    End For


Running time of the algorithm on the nn-length array is n+n+(1+2++n)+n=3n+(1+n)n/2n + n + (1 + 2 + \ldots + n) + n = 3n + (1 + n)^n / 2.

Simplifying it using asymptotic notation: 3n+(1+n)n/2(1+n)n/2(1+n)nnn3n + (1 + n)^n / 2 \rightarrow (1 + n)^n / 2 \rightarrow (1 + n)^n \rightarrow n^*n in every case.

Answer provided by www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS