logo CodeStepByStep logo

longestSortedSubsequence

Language/Type: C++ array lists arrays

Write a member function named longestSortedSequence that could be added to the ArrayIntList class. Your function should return the length of the longest sorted sequence within the list of integers. For example, if a variable named list stores the following sequence of values:

{1, 3, 5, 2, 9, 7, -3, 0, 42, 308, 17}

Then the call of list.longestSortedSequence() would return 4 because it is the length of the longest sorted sequence within this list (the sequence -3, 0, 42, 308). If the list is empty, your method should return 0. Notice that for a non-empty list the method will always return a value of at least 1 because any individual element is a sorted sequence.

Constraints: You may call other member functions of the ArrayIntList if you like, but your function should not modify the state of the list. Its header should be declared in such a way as to indicate to the client that this function does not modify the list. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc).

Write the member function as it would appear in ArrayIntList.cpp. You do not need to declare the function header that would appear in ArrayIntList.h. Assume that you are adding this method to the ArrayIntList class as defined below:

class ArrayIntList {
private:
    int* elements;   // array storing element data
    int mysize;      // number of elements in the array
    int capacity;    // array's length

public:
    ...
};
Member function: Write a member function that will become part of an existing class. You do not need to write the complete class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.