Given an array we have to find the maximum and minimum of array and print the result.
Example:
input:arr[]={67,34,23,56,8}
output:8
Logic:
step 1- declare the max and min variable.
step 2-compare the max and min value with the array value
step 3- update max and min value
#include<bits/stdc++.h> using namespace std; int main() { int arr[]={67,2,3,5,9}; int n= sizeof(arr)/sizeof(arr[0]); int max=INT_MIN; int min=INT_MAX; for(int i=0;i<n;i++) { if(arr[i]<min) { min=arr[i]; } if(arr[i]>max) { max=arr[i]; } } cout<<"max "<<" "<<max<<endl; cout<<"min "<<" "<<min<<endl; }
Output:
max 67 min 2
Time Complexity-O(N)
space complexity-O(1)