Dada una array arr[] , la tarea es contar el número de veces que se actualiza el valor mínimo y máximo durante el recorrido de la array.
Ejemplos:
Entrada: arr[] = {10, 5, 20, 22}
Salida:
Número de veces que se actualiza el valor mínimo = 2
Número de veces que se actualiza el valor máximo = 3
Explicación:
Paso 1: Mínimo = 10, Máximo = 10
Paso 2: Mínimo = 5, Máximo = 10
Paso 3: Mínimo = 5, Máximo = 20
Paso 3: Mínimo = 5, Máximo = 22
Entrada: arr[] = {1, 2, 3, 4, 5}
Salida:
Número de veces que se actualiza el valor mínimo = 1
Número de veces que se actualiza el valor máximo = 5
Explicación:
Paso 1: Mínimo = 1, Máximo = 1
Paso 2: Mínimo = 1, Máximo = 2
Paso 3: Mínimo = 1, Máximo = 3
Paso 4: Mínimo = 1, Máximo = 4
Paso 5: Mínimo = 1, Máximo = 5
Enfoque: La idea es hacer un seguimiento del valor mínimo y el valor máximo. Inicialmente inicialice estos valores como el primer elemento de la array. Finalmente, itere sobre la array, y cada vez que se cambie el valor máximo o mínimo, incremente el conteo en consecuencia.
if (cur_min > arr[i]) cur_min = arr[i] count_min++; if (cur_max < arr[i]) cur_max = arr[i] count_max++;
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to find the // number of times minimum and // maximum value updated during the // traversal of the array #include<bits/stdc++.h> 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<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 (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<int> 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<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[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
Javascript
<script> // Javascript 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 function maxUpdated(arr) { let h_score = arr[0]; let l_score = arr[0]; let i = 1, j = 1; // Increment i if new // highest value occurs // Increment j if new // lowest value occurs for (let n in arr) { if (h_score < arr[n]) { h_score = n; i++; } if (l_score > n) { l_score = n; j++; } } document.write( "Number of times maximum value "); document.write( "updated = " + i + "<br/>"); document.write( "Number of times minimum value "); document.write( "updated = " + j); } // Driver Code let arr = [ 10, 5, 20, 22 ]; maxUpdated(arr); </script>
Number of times maximum value updated = 3 Number of times minimum value updated = 2
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por utkarsh_kumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA