Dada una array arr[] de tamaño N , la tarea es encontrar el número de puntos valle en la array.
Punto de valle: cualquier elemento de la array se conoce como punto de valle si es más pequeño que sus dos elementos adyacentes, es decir .
Ejemplos:
Entrada: arr[] = {3, 2, 5}
Salida: 1
Explicación:
Solo hay un punto de valle. Eso es arr[1].Entrada: arr[] = {5, 4, 8, 3, 6}
Salida: 2
Explicación:
Hay dos puntos de valle. Eso es arr[1] y arr[3].
Enfoque: la idea es iterar sobre la array desde 1 hasta y para cada elemento, verificar si ese elemento es un punto de valle o no, si es así, luego incrementar el recuento del punto de valle en 1.
if (arr[i-1] > arr[i] < arr[i]) count += 1;
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to count the number // of valley points in the array #include<bits/stdc++.h> using namespace std; // Function to count the valley points // in the given character array int countValleys(int n, int arr[]) { int count = 0, temp = 0; // Loop to iterate over the // elements of the given array for(int i = 1; i < n - 1; i++) { // Condition to check if the given // element is a valley point if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) { count++; } } return count; } // Driver Code int main() { int arr[] = { 3, 2, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << countValleys(n, arr); } // This code is contributed by Surendra_Gangwar
Java
// Java program to count the number // of valley points in the array import java.io.*; class GFG { // Function to count the valley points // in the given character array static int countValleys(int n, int arr[]) { int count = 0, temp = 0; // Loop to iterate over the elements // of the given array for (int i = 1; i < n - 1; i++) { // Condition to check if the given // element is a valley point if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) { count++; } } return count; } // Driver Code public static void main(String[] args) { int arr[] = { 3, 2, 5 }; int n = arr.length; System.out.println( countValleys(n, arr)); } }
Python3
# Python3 program to count the number # of valley points in the array # Function to count the valley points # in the given character array def countValleys(n, arr): count = 0; temp = 0; # Loop to iterate over the # elements of the given array for i in range(1, n): # Condition to check if the given # element is a valley point if (arr[i - 1] > arr[i] and arr[i] < arr[i + 1]): count += 1; return count; # Driver Code arr = [ 3, 2, 5 ]; n = len(arr); print(countValleys(n, arr)); # This code is contributed by Code_Mech
C#
// C# program to count the number // of valley points in the array using System; class GFG{ // Function to count the valley points // in the given character array static int countValleys(int n, int []arr) { int count = 0; // Loop to iterate over the elements // of the given array for (int i = 1; i < n - 1; i++) { // Condition to check if the given // element is a valley point if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) { count++; } } return count; } // Driver Code public static void Main() { int []arr = { 3, 2, 5 }; int n = arr.Length; Console.Write(countValleys(n, arr)); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript program to count the number // of valley points in the array // Function to count the valley points // in the given character array function countValleys(n, arr) { let count = 0, temp = 0; // Loop to iterate over the // elements of the given array for(let i = 1; i < n - 1; i++) { // Condition to check if the given // element is a valley point if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) { count++; } } return count; } // Driver Code let arr = [ 3, 2, 5 ]; let n = arr.length; document.write(countValleys(n, arr)); // This code is contributed by rishavmahato348 </script>
1
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