1#include<iostream>
2
3using namespace std;
4
5public void getMax_MinValue(int arr[])
6{
7 int max, min;
8
9 max = arr[0];
10 min = arr[0];
11 for (int i = 0; i < sizeof(arr); i++)
12 {
13 if (max < arr[i])
14 max = arr[i];
15 else if (min > arr[i])
16 min = arr[i];
17 }
18
19 cout << "Largest element : " << max;
20 cout << "Smallest element : " << min;
21
22}
1*max_element (first_index, last_index);
2ex:- for an array arr of size n
3*max_element(arr, arr + n);
1#include <iostream>
2using namespace std;
3int main(){
4 //n is the number of elements in the array
5 int n, largest;
6 int num[50];
7 cout<<"Enter number of elements you want to enter: ";
8 cin>>n;
9
10 /* Loop runs from o to n, in such a way that first
11 * element entered by user is stored in num[0], second
12 * in num[1] and so on.
13 */
14 for(int i = 0; i < n; i++) {
15 cout<<"Enter Element "<<(i+1)<< ": ";
16 cin>>num[i];
17 }
18 // Storing first array element in "largest" variable
19 largest = num[0];
20 for(int i = 1;i < n; i++) {
21 /* We are comparing largest variable with every element
22 * of array. If there is an element which is greater than
23 * largest variable value then we are copying that variable
24 * to largest, this way we have the largest element copied
25 * to the variable named "largest" at the end of the loop
26 *
27 */
28 if(largest < num[i])
29 largest = num[i];
30 }
31 cout<<"Largest element in array is: "<<largest;
32 return 0;
33}
1---without sort method---
2public static int maxValue( int[] n ) {
3
4int max = Integer.MIN_VALUE;
5
6for(int each: n)
7
8if(each > max)
9
10max = each;
11
12
13return max;
14
15---with sort method---
16public static int maxValue( int[] n ) {
17
18Arrays.sort( n );
19
20return n [ n.lenth-1 ];
21
22}