📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    You have a list of numbers and need to find the maximum

    value. Which of the following approaches would be the most efficient in terms of time complexity?
    A Sort the list and take the last element. Correct Answer Incorrect Answer
    B Iterate through the list once, keeping track of the maximum seen so far. Correct Answer Incorrect Answer
    C Use a binary search tree to store elements and find the maximum. Correct Answer Incorrect Answer
    D Use a hash map to store elements and find the maximum. Correct Answer Incorrect Answer
    E Recursively divide the list into halves until a single element remains. Correct Answer Incorrect Answer

    Solution

    Iterating through the list once and maintaining a running maximum takes O(N) time, which is the most efficient approach for this problem.     Sorting takes O(N log N).     Binary search tree operations are O(log N) on average, but building it is O(N log N) or O(N^2) worst case.     Hash map is not suitable for finding maximum efficiently.

    Practice Next
    ask-question