Comprobar si una array es creciente o decreciente

Dada una array arr[] de N elementos donde N ≥ 2 , la tarea es verificar el tipo de array si es: 
 

  1. Creciente.
  2. Decreciente.
  3. Creciendo y luego decreciendo.
  4. Disminuyendo y luego aumentando.

Tenga en cuenta que la array dada es definitivamente uno de los tipos dados.
Ejemplos: 
 

Entrada: arr[] = {1, 2, 3, 4, 5} 
Salida: Creciente
Entrada: arr[] = {1, 2, 4, 3} 
Salida: Creciente y luego decreciente 
 

Enfoque: Deben cumplirse las siguientes condiciones para: 
 

  1. Array creciente: Los dos primeros y los dos últimos elementos deben estar en orden creciente.
  2. Array decreciente: Los dos primeros y los dos últimos elementos deben estar en orden decreciente.
  3. Array creciente y luego decreciente: los primeros dos elementos deben estar en orden creciente y los dos últimos elementos deben estar en orden decreciente.
  4. Array decreciente y luego creciente: los primeros dos elementos deben estar en orden decreciente y los dos últimos elementos deben estar en orden creciente.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check the type of the array
void checkType(int arr[], int n)
{
 
    // If the first two and the last two elements
    // of the array are in increasing order
    if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])
        cout << "Increasing";
 
    // If the first two and the last two elements
    // of the array are in decreasing order
    else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])
        cout << "Decreasing";
 
    // If the first two elements of the array are in
    // increasing order and the last two elements
    // of the array are in decreasing order
    else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])
        cout << "Increasing then decreasing";
 
    // If the first two elements of the array are in
    // decreasing order and the last two elements
    // of the array are in increasing order
    else
        cout << "Decreasing then increasing";
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    checkType(arr, n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.math.*;
 
class GFG
{
 
    // Function to check the type of the array
    public static void checkType(int arr[], int n)
    {
     
        // If the first two and the last two elements
        // of the array are in increasing order
        if (arr[0] <= arr[1] &&
            arr[n - 2] <= arr[n - 1])
            System.out.println("Increasing");
     
        // If the first two and the last two elements
        // of the array are in decreasing order
        else if (arr[0] >= arr[1] &&
                 arr[n - 2] >= arr[n - 1])
            System.out.println("Decreasing");
     
        // If the first two elements of the array are in
        // increasing order and the last two elements
        // of the array are in decreasing order
        else if (arr[0] <= arr[1] &&
                 arr[n - 2] >= arr[n - 1])
            System.out.println("Increasing then decreasing");
 
        // If the first two elements of the array are in
        // decreasing order and the last two elements
        // of the array are in increasing order
        else
            System.out.println("Decreasing then increasing");
    }
         
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = new int[]{ 1, 2, 3, 4 };
         
        int n = arr.length;
 
        checkType(arr, n);
    }
}
 
// This code is contributed by Naman_Garg

Python3

# Python3 implementation of the approach
 
# Function to check the type of the array
def checkType(arr, n):
 
    # If the first two and the last two elements
    # of the array are in increasing order
    if (arr[0] <= arr[1] and
        arr[n - 2] <= arr[n - 1]) :
        print("Increasing");
 
    # If the first two and the last two elements
    # of the array are in decreasing order
    elif (arr[0] >= arr[1] and
          arr[n - 2] >= arr[n - 1]) :
        print("Decreasing");
 
    # If the first two elements of the array are in
    # increasing order and the last two elements
    # of the array are in decreasing order
    elif (arr[0] <= arr[1] and
          arr[n - 2] >= arr[n - 1]) :
        print("Increasing then decreasing");
 
    # If the first two elements of the array are in
    # decreasing order and the last two elements
    # of the array are in increasing order
    else :
        print("Decreasing then increasing");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 2, 3, 4 ];
    n = len(arr);
 
    checkType(arr, n);
 
# This code is contributed by AnkitRai01

C#

// C# implementation of the approach
using System;
class GFG
{
     
    // Function to check the type of the array
    public static void checkType(int []arr, int n)
    {
     
        // If the first two and the last two elements
        // of the array are in increasing order
        if (arr[0] <= arr[1] &&
            arr[n - 2] <= arr[n - 1])
            Console.Write("Increasing");
     
        // If the first two and the last two elements
        // of the array are in decreasing order
        else if (arr[0] >= arr[1] &&
                 arr[n - 2] >= arr[n - 1])
            Console.Write("Decreasing");
     
        // If the first two elements of the array are in
        // increasing order and the last two elements
        // of the array are in decreasing order
        else if (arr[0] <= arr[1] &&
                 arr[n - 2] >= arr[n - 1])
            Console.Write("Increasing then decreasing");
 
        // If the first two elements of the array are in
        // decreasing order and the last two elements
        // of the array are in increasing order
        else
            Console.Write("Decreasing then increasing");
    }
 
    // Driver code
    static public void Main ()
    {
        int[] arr = new int[]{ 1, 2, 3, 4 };
         
        int n = arr.Length;
 
        checkType(arr, n);
    }
}
 
// This code is contributed by ajit

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function to check the type of the array
function checkType(arr, n)
{
 
    // If the first two and the last two elements
    // of the array are in increasing order
    if (arr[0] <= arr[1] && arr[n - 2] <= arr[n - 1])
        document.write("Increasing");
 
    // If the first two and the last two elements
    // of the array are in decreasing order
    else if (arr[0] >= arr[1] && arr[n - 2] >= arr[n - 1])
        document.write("Decreasing");
 
    // If the first two elements of the array are in
    // increasing order and the last two elements
    // of the array are in decreasing order
    else if (arr[0] <= arr[1] && arr[n - 2] >= arr[n - 1])
        document.write("Increasing then decreasing");
 
    // If the first two elements of the array are in
    // decreasing order and the last two elements
    // of the array are in increasing order
    else
        document.write("Decreasing then increasing");
}
 
// Driver code
    let arr = [ 1, 2, 3, 4 ];
    let n = arr.length;
 
    checkType(arr, n);
 
</script>
Producción: 

Increasing

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por gp6 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *