how to find max element in array in c++ code example

Example 1: c++ max of array

cout << " max element is: " << *max_element(array , array + n) << endl;

Example 2: find min and max in array c++

#include<iostream>

using namespace std;

public void getMax_MinValue(int arr[])
{
    int max, min;

    max = arr[0];
    min = arr[0];
    for (int i = 0; i < sizeof(arr); i++)
    {
        if (max < arr[i])
            max = arr[i];
        else if (min > arr[i])
            min = arr[i];
    }

    cout << "Largest element : " << max;
    cout << "Smallest element : " << min;
 
}

Example 3: max element in array c++ stl

*max_element (first_index, last_index);
ex:- for an array arr of size n
*max_element(arr, arr + n);

Tags:

Cpp Example