Given an array of length n, we need to find the Array elements with least value and greatest value.

Approach 1:

In this approach, we simply sort the given array to get the increasing order of the elements. After getting the sorted array, we can say that the first element in the sorted array has the least value. So this element is the min_ele. The last element of the sorted array has the greatest value. So, this element is the max_ele.

EXAMPLE:

Let the given array be {29,68,86,18}

Screenshot 57

Time complexity : O(nlogn)
Space complexity : O(1)

LET US SEE THE CODE:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,min_ele,max_ele;
    cout << "Enter the length of array" << endl;
    cin >> n; // Taking array length as input
    vector<int> arr(n);
    cout << "Enter array elements" << endl;
    for(i=0;i<n;i++) // Loop to take array elements as input
    {
        cin >> arr[i];
    }
    sort(arr.begin(),arr.end()); // Sorting array elements
    min_ele=arr[0]; // first element obtained after sorting is the min_ele
    max_ele=arr[n-1]; // last element obtained after sorting is the max_ele
    cout << "Minimum element is " << min_ele << endl;
    cout << "Maximum element is " << max_ele << endl;
    return 0;
}

 TESTCASE:

Enter the length of array
5
Enter array elements
44 75 88 35 22
Minimum element is 22
Maximum element is 88

Approach 2:

In this approach, we first initialize the min_ele and max_ele to the first element of the array and then traverse the remaining array. While traversing the remaining array, we compare each element with the min_ele and max_ele and update their values based on the value of the array element.
After traversing the entire array, we get the minimum and maximum element of the complete array.

EXAMPLE:

Let the given array be {40,66,79,12}

Screenshot 56

Time complexity : O(n)
Space complexity : O(1)

LET US SEE THE CODE:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,min_ele,max_ele;
    cout << "Enter the length of array" << endl;
    cin >> n; // Taking array length as input
    vector<int> arr(n);
    cout << "Enter array elements" << endl;
    for(i=0;i<n;i++) // Loop to take array elements as input
    {
        cin >> arr[i];
    }
    min_ele=arr[0]; // setting min_ele as first element of array
    max_ele=arr[0]; // setting max_ele as first element of array
    for(i=1;i<n;i++) // loop to iterate remaining elements
    {
        if(arr[i]<min_ele) // condition to check if the current element is less than min_ele
        {
            min_ele=arr[i]; // if yes, current element has minimum value. So it becomes max_ele
        }
        if(arr[i]>max_ele) // condition to check if the current element is greater than max_ele
        {
            max_ele=arr[i]; // if yes, current element has maximum value. So it becomes max_ele
        }
    }
    cout << "Minimum element is " << min_ele << endl;
    cout << "Maximum element is " << max_ele << endl;
    return 0;
}

 TESTCASE:

Enter the length of array
5
Enter array elements
48 18 85 32 96
Minimum element is 18
Maximum element is 96