# Number of times Maximum and minimum value updated during traversal of array

Given an array arr[], the task is to count the number of times the minimum and the maximum value is updated during the traversal of the array.

Examples:

Input: arr[] = {10, 5, 20, 22}
Output:
Number of times minimum value updated = 2
Number of times maximum value updated = 3
Explanation:
Step 1: Minimum = 10, Maximum = 10
Step 2: Minimum = 5, Maximum = 10
Step 3: Minimum = 5, Maximum = 20
Step 3: Minimum = 5, Maximum = 22

Input: arr[] = {1, 2, 3, 4, 5}
Output:
Number of times minimum value updated = 1
Number of times maximum value updated = 5
Explanation:
Step 1: Minimum = 1, Maximum = 1
Step 2: Minimum = 1, Maximum = 2
Step 3: Minimum = 1, Maximum = 3
Step 4: Minimum = 1, Maximum = 4
Step 5: Minimum = 1, Maximum = 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to keep track of the minimum value and maximum value. Intially intialize these values as first elements of the array. Finally Iterate over the array and whenever the maximum or minimum value is to be changed increment the count accordingly.

```if (cur_min > arr[i])
cur_min = arr[i]
count_min++;
if (cur_max < arr[i])
cur_max = arr[i]
count_max++;
```

Below is the implementation of the above approach:

## Java

 `// Java implementation to find the  ` `// number of times minimum and  ` `// maximum value updated during the ` `// traversal of the array ` ` `  `public` `class` `GFG { ` `     `  `    ``// Function to find the number of ` `    ``// times minimum and maximum value ` `    ``// updated during the traversal ` `    ``// of the given array ` `    ``static` `void` `maxUpdated(``int``[] arr) ` `    ``{ ` `        ``int` `h_score = arr[``0``]; ` `        ``int` `l_score = arr[``0``]; ` `        ``int` `i = ``1``, j = ``1``; ` `         `  `        ``// Increment i if new  ` `        ``// highest value occurs ` `        ``// Increment j if new  ` `        ``// lowest value occurs ` `        ``for` `(Integer n : arr) { ` `            ``if` `(h_score < n) { ` `                ``h_score = n; ` `                ``i++; ` `            ``} ` `            ``if` `(l_score > n) { ` `                ``l_score = n; ` `                ``j++; ` `            ``} ` `        ``} ` `        ``System.out.print( ` `            ``"Number of times maximum value "``); ` `        ``System.out.print( ` `            ``"updated = "` `+ i + ``"\n"``); ` `        ``System.out.print( ` `            ``"Number of times minimum value "``); ` `        ``System.out.print( ` `            ``"updated = "` `+ j); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { ``10``, ``5``, ``20``, ``22` `}; ` `        ``maxUpdated(arr); ` `    ``} ` `} `

## Python

 `# Python implementation to count  ` `# the number of times maximum  ` `# and minimum value updated ` ` `  `# Function to find the count  ` `# the number of times maximum ` `# and minimum value updated ` `def` `maximumUpdates(arr):  ` `    ``min` `=` `arr[``0``] ` `    ``max` `=` `arr[``0``] ` `    ``minCount, maxCount ``=` `1``, ``1` `     `  `    ``# Update the maximum and  ` `    ``# minimum values during traversal ` `    ``for` `arr ``in` `arr :  ` `        ``if` `arr>``max``: ` `            ``maxCount``+``=` `1` `            ``max` `=` `arr ` `        ``if` `arr<``min``: ` `            ``minCount``+``=` `1``; ` `            ``min` `=` `arr ` `         `  `    ``print``(``"Number of times maximum "``, end ``=` `"") ` `    ``print``(``"value updated = "``, maxCount) ` `    ``print``(``"Number of times minimum "``, end ``=` `"") ` `    ``print``(``"value updated = "``, minCount)  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[ ``10``, ``5``, ``20``, ``22` `] ` `    ``maximumUpdates(arr) `

## C#

 `// C# implementation to find the  ` `// number of times minimum and  ` `// maximum value updated during the ` `// traversal of the array ` `using` `System; ` ` `  `class` `GFG { ` `     `  `// Function to find the number of ` `// times minimum and maximum value ` `// updated during the traversal ` `// of the given array ` `static` `void` `maxUpdated(``int``[] arr) ` `{  ` `    ``int` `h_score = arr; ` `    ``int` `l_score = arr; ` `    ``int` `i = 1, j = 1; ` `         `  `    ``// Increment i if new highest  ` `    ``// value occurs Increment j  ` `    ``// if new lowest value occurs ` `    ``foreach``(``int` `n ``in` `arr) ` `    ``{  ` `        ``if` `(h_score < n) ` `        ``{ ` `            ``h_score = n; ` `            ``i++; ` `        ``} ` `        ``if` `(l_score > n) ` `        ``{ ` `            ``l_score = n; ` `            ``j++; ` `        ``} ` `    ``} ` `     `  `    ``Console.Write(``"Number of times maximum value "``); ` `    ``Console.Write(``"updated = "` `+ i + ``"\n"``); ` `    ``Console.Write(``"Number of times minimum value "``); ` `    ``Console.Write(``"updated = "` `+ j); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int``[] arr = { 10, 5, 20, 22 }; ` `    ``maxUpdated(arr); ` `} ` `} ` ` `  `// This code is contributed by Amit Katiyar `

Output:

```Number of times maximum value updated = 3
Number of times minimum value updated = 2
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : amit143katiyar

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.