Dada una array de N enteros. La tarea es imprimir los elementos de la array que son mayores que sus elementos anteriores y siguientes inmediatos.
Ejemplos :
Input : arr[] = {2, 3, 1, 5, 4, 9, 8, 7, 5} Output : 3, 5, 9 In above given example 3 is greater than its left element 2 and right element 1. Similar logic is applied to other elements hence our final output is 3, 5, 9. Input : arr[] = {1, 2, 3, 2, 1} Output : 3
Enfoque : dado que no hay ningún elemento a la izquierda del primer elemento (arr[0]) y no hay ningún elemento a la derecha del último elemento (arr[N-1]), estos dos elementos se excluirán de la respuesta final. .
Ahora, recorra la array desde el índice 1 hasta N-2 y para cada elemento arr[i] verifique si arr[i] > arr[i-1] y arr[i] > arr[i+1] .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print elements greater than // the previous and next element in an Array #include <bits/stdc++.h> using namespace std; // Function to print elements greater than // the previous and next element in an Array void printElements(int arr[], int n) { // Traverse array from index 1 to n-2 // and check for the given condition for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]) cout << arr[i] << " "; } } // Driver Code int main() { int arr[] = { 2, 3, 1, 5, 4, 9, 8, 7, 5 }; int n = sizeof(arr) / sizeof(arr[0]); printElements(arr, n); return 0; }
Java
// Java program to print elements greater than // the previous and next element in an Array class GfG { // Function to print elements greater than // the previous and next element in an Array static void printElements(int arr[], int n) { // Traverse array from index 1 to n-2 // and check for the given condition for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) System.out.print(arr[i] + " "); } } // Driver Code public static void main(String[] args) { int arr[] = { 2, 3, 1, 5, 4, 9, 8, 7, 5 }; int n = arr.length; printElements(arr, n); } } // This code is contributed by Prerna Saini
Python3
# Python 3 program to print elements greater than # the previous and next element in an Array # Function to print elements greater than # the previous and next element in an Array def printElements(arr, n): # Traverse array from index 1 to n-2 # and check for the given condition for i in range(1, n - 1, 1): if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]): print(arr[i], end = " ") # Driver Code if __name__ == '__main__': arr = [2, 3, 1, 5, 4, 9, 8, 7, 5] n = len(arr) printElements(arr, n) # This code is contributed by # Surendra_Gangwar
C#
using System; // c# program to print elements greater than // the previous and next element in an Array public class GfG { // Function to print elements greater than // the previous and next element in an Array public static void printElements(int[] arr, int n) { // Traverse array from index 1 to n-2 // and check for the given condition for (int i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { Console.Write(arr[i] + " "); } } } // Driver Code public static void Main(string[] args) { int[] arr = new int[] {2, 3, 1, 5, 4, 9, 8, 7, 5}; int n = arr.Length; printElements(arr, n); } }
PHP
<?php // PHP program to print elements greater than // the previous and next element in an Array // Function to print elements greater than // the previous and next element in an Array function printElements($arr, $n) { // Traverse array from index 1 to n-2 // and check for the given condition for ($i = 1; $i < $n - 1; $i++) { if ($arr[$i] > $arr[$i - 1] and $arr[$i] > $arr[$i + 1]) echo $arr[$i] . " "; } } // Driver Code $arr = array(2, 3, 1, 5, 4, 9, 8, 7, 5); $n = sizeof($arr); printElements($arr, $n); // This code is contributed // by Akanksha Rai
Javascript
<script> // Javascript implementation of the approach // Function to print elements greater than // the previous and next element in an Array function printElements(arr, n) { // Traverse array from index 1 to n-2 // and check for the given condition for (var i = 1; i < n - 1; i++) { if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) document.write( arr[i] + " "); } } var arr = [ 2, 3, 1, 5, 4, 9, 8, 7, 5 ]; var n = arr.length; printElements(arr, n); // This code is contributed by SoumikMondal </script>
3 5 9
Tiempo Complejidad : O(n)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por HbhushanMahajan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA