Dada una array arr[] de N enteros, la tarea es encontrar la amplitud y el número de ondas para la array dada. Si la array no es una array de ondas , imprima -1 .
Conjunto de ondas: un conjunto es un conjunto de ondas si aumenta y disminuye estrictamente de forma continua o viceversa.
La amplitud se define como la diferencia máxima de números consecutivos.
Ejemplos:
Entrada: arr[] = {1, 2, 1, 5, 0, 7, -6}
Salida: Amplitud = 13, Ondas = 3
Explicación:
Para la array observe el patrón 1->2 (aumento), 2-> 1 (disminuir), 1->5 (aumentar), 5->0 (disminuir), 0->7 (aumentar), 7->-6 (disminuir). Amplitud = 13 (entre 7 y -6) y ondas totales = 3
Entrada: arr[] = {1, 2, 1, 5, 0, 7, 7}
Salida: -1
Explicación:
El arreglo no es un arreglo ondulado como el los dos últimos elementos de la array son iguales, por lo que la respuesta es -1.
Enfoque:
la idea es verificar los elementos adyacentes de ambos lados donde ambos deben ser menores o mayores que el elemento actual. Si se cumple esta condición, cuente el número de ondas; de lo contrario, imprima -1 , donde el número de ondas es (n – 1)/2 . Mientras atraviesa la array, siga actualizando la diferencia máxima entre los elementos consecutivos para obtener la amplitud de la array de ondas dada .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the amplitude and // number of waves for the given array bool check(int a[], int n) { int ma = a[1] - a[0]; // Check for both sides adjacent // elements that both must be less // or both must be greater // than current element for (int i = 1; i < n - 1; i++) { if ((a[i] > a[i - 1] && a[i + 1] < a[i]) || (a[i] < a[i - 1] && a[i + 1] > a[i])) // Update amplitude with max value ma = max(ma, abs(a[i] - a[i + 1])); else return false; } // Print the Amplitude cout << "Amplitude = " << ma; cout << endl; return true; } // Driver Code int main() { // Given array a[] int a[] = { 1, 2, 1, 5, 0, 7, -6 }; int n = sizeof a / sizeof a[0]; // Calculate number of waves int wave = (n - 1) / 2; // Function Call if (check(a, n)) cout << "Waves = " << wave; else cout << "-1"; return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the amplitude and // number of waves for the given array static boolean check(int a[], int n) { int ma = a[1] - a[0]; // Check for both sides adjacent // elements that both must be less // or both must be greater // than current element for (int i = 1; i < n - 1; i++) { if ((a[i] > a[i - 1] && a[i + 1] < a[i]) || (a[i] < a[i - 1] && a[i + 1] > a[i])) // Update amplitude with max value ma = Math.max(ma, Math.abs(a[i] - a[i + 1])); else return false; } // Print the Amplitude System.out.print("Amplitude = " + ma); System.out.println(); return true; } // Driver Code public static void main(String[] args) { // Given array a[] int a[] = { 1, 2, 1, 5, 0, 7, -6 }; int n = a.length; // Calculate number of waves int wave = (n - 1) / 2; // Function Call if (check(a, n)) System.out.print("Waves = " + wave); else System.out.print("-1"); } } // This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach # Function to find the amplitude and # number of waves for the given array def check(a, n): ma = a[1] - a[0] # Check for both sides adjacent # elements that both must be less # or both must be greater # than current element for i in range(1, n - 1): if ((a[i] > a[i - 1] and a[i + 1] < a[i]) or (a[i] < a[i - 1] and a[i + 1] > a[i])): # Update amplitude with max value ma = max(ma, abs(a[i] - a[i + 1])) else: return False # Print the Amplitude print("Amplitude = ", ma) return True # Driver Code if __name__ == '__main__': # Given array a[] a = [1, 2, 1, 5, 0, 7, -6] n = len(a) # Calculate number of waves wave = (n - 1) // 2 # Function Call if (check(a, n)): print("Waves = ",wave) else: print("-1") # This code is contributed by Mohit Kumar
C#
// C# program for the above approach using System; class GFG{ // Function to find the amplitude and // number of waves for the given array static bool check(int []a, int n) { int ma = a[1] - a[0]; // Check for both sides adjacent // elements that both must be less // or both must be greater // than current element for (int i = 1; i < n - 1; i++) { if ((a[i] > a[i - 1] && a[i + 1] < a[i]) || (a[i] < a[i - 1] && a[i + 1] > a[i])) // Update amplitude with max value ma = Math.Max(ma, Math.Abs(a[i] - a[i + 1])); else return false; } // Print the Amplitude Console.Write("Amplitude = " + ma); Console.WriteLine(); return true; } // Driver Code public static void Main(String[] args) { // Given array []a int []a = { 1, 2, 1, 5, 0, 7, -6 }; int n = a.Length; // Calculate number of waves int wave = (n - 1) / 2; // Function Call if (check(a, n)) Console.Write("Waves = " + wave); else Console.Write("-1"); } } // This code is contributed by sapnasingh4991
Javascript
<script> // JavaScript program for the above approach // Function to find the amplitude and // number of waves for the given array function check(a, n) { let ma = a[1] - a[0]; // Check for both sides adjacent // elements that both must be less // or both must be greater // than current element for (let i = 1; i < n - 1; i++) { if ((a[i] > a[i - 1] && a[i + 1] < a[i]) || (a[i] < a[i - 1] && a[i + 1] > a[i])) // Update amplitude with max value ma = Math.max(ma, Math.abs(a[i] - a[i + 1])); else return false; } // Print the Amplitude document.write("Amplitude = " + ma); document.write("<br/>"); return true; } // Driver Code // Given array a[] let a = [ 1, 2, 1, 5, 0, 7, -6 ]; let n = a.length; // Calculate number of waves let wave = (n - 1) / 2; // Function Call if (check(a, n)) document.write("Waves = " + wave); else document.write("-1"); </script>
Amplitude = 13 Waves = 3
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por grand_master y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA