Dada una array de N elementos. La tarea es verificar si la array es bitónica o no.
Se dice que una array es bitónica si los elementos de la array primero son estrictamente crecientes y luego estrictamente decrecientes.
Ejemplos :
Input: arr[] = {-3, 9, 11, 20, 17, 5, 1} Output: YES Input: arr[] = {5, 6, 7, 8, 9, 10, 1, 2, 11}; Output: NO
Enfoque :
- Comience a recorrer la array y siga comprobando si el siguiente elemento es mayor que el elemento actual o no.
- Si en algún momento, el siguiente elemento no es mayor que el elemento actual, rompa el bucle.
- Nuevamente comience a atravesar desde el elemento actual y verifique si el siguiente elemento es menor que el elemento actual o no.
- Si en cualquier punto antes de que se alcance el final de la array, si el siguiente elemento no es menor que el elemento actual, rompa el bucle e imprima NO.
- Si se llega al final de la array con éxito, imprima SÍ.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to check if an array is bitonic #include <bits/stdc++.h> using namespace std; // Function to check if the given array is bitonic int checkBitonic(int arr[], int n) { int i, j; // Check for increasing sequence for (i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) continue; if (arr[i] <= arr[i - 1]) break; } if (i > n - 1) return 1; // Check for decreasing sequence for (j = i + 1; j < n; j++) { if (arr[j] < arr[j - 1]) continue; if (arr[j] >= arr[j - 1]) break; } i = j; if (i != n) return 0; return 1; } // Driver code int main() { int arr[] = { 1,2,3 }; int n = sizeof(arr) / sizeof(arr[0]); (checkBitonic(arr, n) == 1) ? cout << "YES" : cout << "NO"; return 0; }
Java
// Java program to check // if an array is bitonic class GFG { // Function to check if the // given array is bitonic static int checkBitonic(int arr[], int n) { int i, j; // Check for increasing sequence for (i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) continue; if (arr[i] <= arr[i - 1]) break; } if (i == n - 1) return 1; // Check for decreasing sequence for (j = i + 1; j < n; j++) { if (arr[j] < arr[j - 1]) continue; if (arr[j] >= arr[j - 1]) break; } i = j; if (i != n) return 0; return 1; } // Driver Code public static void main(String args[]) { int arr[] = { -3, 9, 7, 20, 17, 5, 1 }; int n = arr.length; System.out.println((checkBitonic(arr, n) == 1) ? "YES" : "NO"); } } // This code is contributed by Bilal
Python3
# Python3 program to check if # an array is bitonic or not. # Function to check if the # given array is bitonic def checkBitonic(arr, n) : # Check for increasing sequence for i in range(1, n) : if arr[i] > arr[i - 1] : continue else : break if i == n-1 : return 1 # Check for decreasing sequence for j in range(i + 1, n) : if arr[j] < arr[j - 1] : continue else : break i = j if i != n - 1 : return 0 return 1 # Driver Code if __name__ == "__main__" : arr = [-3, 9, 7, 20, 17, 5, 1] n = len(arr) if checkBitonic(arr, n) == 1 : print("YES") else : print("NO") # This code is contributed # by ANKITRAI1
C#
// C# program to check // if an array is bitonic using System; class GFG { // Function to check if the // given array is bitonic static int checkBitonic(int []arr, int n) { int i, j; // Check for increasing sequence for (i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) continue; if (arr[i] <= arr[i - 1]) break; } if (i == n - 1) return 1; // Check for decreasing sequence for (j = i + 1; j < n; j++) { if (arr[j] < arr[j - 1]) continue; if (arr[j] >= arr[j - 1]) break; } i = j; if (i != n) return 0; return 1; } // Driver Code public static void Main() { int []arr = { -3, 9, 7, 20, 17, 5, 1 }; int n = arr.Length; Console.WriteLine(( checkBitonic(arr, n) == 1) ? "YES" : "NO"); } } // This code is contributed by Bilal
PHP
<?php // PHP program to check if // an array is bitonic // Function to check if the // given array is bitonic function checkBitonic(&$arr, $n) { // Check for increasing sequence for ($i = 1; $i < $n; $i++) { if ($arr[$i] > $arr[$i - 1]) continue; if ($arr[$i] <= $arr[$i - 1]) break; } if ($i == $n - 1) return 1; // Check for decreasing sequence for ($j = $i + 1; $j < $n; $j++) { if ($arr[$j] < $arr[$j - 1]) continue; if ($arr[$j] >= $arr[$j - 1]) break; } $i = $j; if ($i != $n) return 0; return 1; } // Driver code $arr = array( -3, 9, 7, 20, 17, 5, 1 ); $n = sizeof($arr); checkBitonic($arr, $n) == 1 ? print("YES") : print("NO"); // This code is contributed by ChitraNayal ?>
Javascript
<script> // Java Script program to check // if an array is bitonic // Function to check if the // given array is bitonic function checkBitonic(arr,n) { let i, j; // Check for increasing sequence for (i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) continue; if (arr[i] <= arr[i - 1]) break; } if (i == n - 1) return 1; // Check for decreasing sequence for (j = i + 1; j < n; j++) { if (arr[j] < arr[j - 1]) continue; if (arr[j] >= arr[j - 1]) break; } i = j; if (i != n) return 0; return 1; } // Driver Code let arr = [ -3, 9, 7, 20, 17, 5, 1 ]; let n = arr.length; document.write((checkBitonic(arr, n) == 1) ? "YES" : "NO"); // This code is contributed by sravan kumar </script>
Producción:
NO