📌  相关文章
📜  遍历数组时更新的最大值和最小值的次数

📅  最后修改于: 2021-04-26 06:01:47             🧑  作者: Mango

给定数组arr [] ,任务是计算遍历数组期间最小值和最大值被更新的次数。
例子:

方法:想法是跟踪最小值和最大值。最初将这些值初始化为数组的第一个元素。最后,遍历数组,每当要更改最大值或最小值时,都会相应地增加计数。

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

下面是上述方法的实现:

C++
// C++ implementation to find the
// number of times minimum and
// maximum value updated during the
// traversal of the array
#include
using namespace std;
 
// Function to find the number of
// times minimum and maximum value
// updated during the traversal
// of the given array
void maxUpdated(vector 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 (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;
}
 
// Driver Code
int main()
{
  vector arr({10, 5, 20, 22});
  maxUpdated(arr);
}
 
// This code is contributed by bgangwar59


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


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[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
    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


输出:
Number of times maximum value updated = 3
Number of times minimum value updated = 2