How can we find second maximum from array efficiently?

前端 未结 16 1077
Happy的楠姐
Happy的楠姐 2020-12-25 14:39

Is it possible to find the second maximum number from an array of integers by traversing the array only once?

As an example, I have a array of five integers from whi

相关标签:
16条回答
  • 2020-12-25 14:55

    Other way to solve this problem, is to use comparisons among the elements. Like for example,

    a[10] = {1,2,3,4,5,6,7,8,9,10}
    

    Compare 1,2 and say max = 2 and second max = 1

    Now compare 3 and 4 and compare the greatest of them with max.

    if element > max
         second max = max
         element = max
    else if element > second max
         second max = element
    

    The advantage with this is, you are eliminating two numbers in just two comparisons.

    Let me know, if you have any problem understanding this.

    0 讨论(0)
  • 2020-12-25 14:56

    Your initialization of max and second_max to -1 is flawed. What if the array has values like {-2,-3,-4}?

    What you can do instead is to take the first 2 elements of the array (assuming the array has at least 2 elements), compare them, assign the smaller one to second_max and the larger one to max:

    if(arr[0] > arr[1]) {
     second_max = arr[1];
     max = arr[0];
    } else {
     second_max = arr[0];
     max = arr[1];
    }
    

    Then start comparing from the 3rd element and update max and/or second_max as needed:

    for(int i = 2; i < arr_len; i++){
        // use >= n not just > as max and second_max can hav same value. Ex:{1,2,3,3}   
        if(arr[i] >= max){  
            second_max=max;
            max=arr[i];          
        }
        else if(arr[i] > second_max){
            second_max=arr[i];
        }
    }
    
    0 讨论(0)
  • 2020-12-25 14:56

    Here you are:

    std::pair<int, int> GetTwoBiggestNumbers(const std::vector<int>& array)
    {
        std::pair<int, int> biggest;
        biggest.first = std::max(array[0], array[1]);  // Biggest of the first two.
        biggest.second = std::min(array[0], array[1]); // Smallest of the first two.
    
        // Continue with the third.
        for(std::vector<int>::const_iterator it = array.begin() + 2;
            it != array.end();
            ++it)
        {
            if(*it > biggest.first)
            {
                biggest.second = biggest.first;
                biggest.first = *it;
            }
            else if(*it > biggest.second)
            {
                biggest.second = *it;
            }
        }
    
        return biggest;
    }
    
    0 讨论(0)
  • 2020-12-25 14:56

    How about the following below. make_heap is O(n) so this is efficient and this is 1-pass We find the second max by taking advantage that it must be one of the heap children of the parent, which had the maximum.

    #include <algorithm>
    #include <iostream>
    
    int main()
    {
        int arr[6]={0,1,2,3,4,5};
    
        std::make_heap(arr, arr+6);
        std::cout << "First Max: " << arr[0] << '\n';
        std::cout << "Second Max: " << std::max(arr[1], arr[2]) << '\n';
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-25 15:01

    Check this solution.

    max1 = a[0];
    max2 = a[1];
    
    for (i = 1; i < n; i++)
    {
        if (max1 < a[i])
        {
            max2 = max1;
            max1 = a[i];
        }
    
        if (max2 == max1) max2 = a[i + 1];
    
        if (max2 == a[n])
        {
            printf("All numbers are the same no second max.\n");
            return 0;
        }
    
        if (max2 < a[i] && max1 != a[i]) max2 = a[i];
    }
    
    0 讨论(0)
  • 2020-12-25 15:02

    Can't we just sort this in decreasing order and take the 2nd element from the sorted array?

    0 讨论(0)
提交回复
热议问题