Number of times Maximum and minimum value updated during traversal of array | C ++ implementation to find the number of times minimum and maximum value updated during the traversal of the array ; Function to find the number of times minimum and maximum value updated during the traversal of the given array ; Increment i if new highest value occurs Increment j if new lowest value occurs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxUpdated ( vector < int > arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; for ( auto n : arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } cout << " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ; cout << " updated ▁ = ▁ " << i << endl ; cout << " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ; cout << " updated ▁ = ▁ " << j << endl ; } int main ( ) { vector < int > arr ( { 10 , 5 , 20 , 22 } ) ; maxUpdated ( arr ) ; }