*max_element (first_index, last_index);
ex:- for an array arr of size n
*max_element(arr, arr + n);
cout << " max element is: " << *max_element(array , array + n) << endl;
#include <iostream>
using namespace std;
//How to find max element in C++
int main() {
int n;
cout <<"How many element yo will enter?"<<endl;
cin >> n;
int arr[n];
for(int i=0;i<n;i++){
cout << "Your "<< i+1 <<" number : ";
cin >> arr[i];
}
for(int i=0;i<n;i++){
if(arr[0]<arr[i]){
arr[0]=arr[i];
}
}
cout << "The max element is :"<<arr[0];
return 0;
}
class findMaxMinFromArray
{
public:
//! Find the maximum element from an array
void arrayInput(int arr[], int size)
{
arr[size] = {0};
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
}
int getMaxUsingSort(int arr[], int size)
{
arrayInput(arr, size);
sort(arr, arr + size, greater<int>());
int max_element = arr[0];
return max_element;
}
void getMaxMin(int num[], int size)
{
arrayInput(num, size);
int max_num = INT32_MIN, min_num = INT32_MAX;
for (int i = 0; i < size; i++)
{
if (num[i] > max_num)
max_num = num[i];
if (num[i] < min_num)
min_num = num[i];
}
cout << "Max number of the array: " << max_num << endl;
cout << "Minimum number of the array: " << min_num << endl;
}
void getMaxMinUsingSTL(int num[], int size)
{
//* Using STL
arrayInput(num, size);
cout << "Maximum element: " << *max_element(num, num + size) << endl;
cout << "Minimum element: " << *min_element(num, num + size) << endl;
}
};
vector<int>arr {1, 34 , 50, 4, 400};
int maximumElement = *max_element(arr.begin(), arr.end());
int minimumElement = *min_element(arr.begin(), arr.end());
int main(int argc, char** argv) {
int A[4] = {0, 2, 3, 1};
const int N = sizeof(A) / sizeof(int);
cout << "Index of max element: "
<< distance(A, max_element(A, A + N))
<< endl;
return 0;
}