Type of array and its maximum element

Given an array, it can be of 4 types
(a) Ascending
(b) Descending
(c) Ascending Rotated
(d) Descending Rotated
Find out which kind of array it is and return the maximum of that array.

Examples :

Input :  arr[] = { 2, 1, 5, 4, 3}
Output : Descending rotated with maximum element 5

Input :  arr[] = { 3, 4, 5, 1, 2}
Output : Ascending rotated with maximum element 5

Asked in : Amazon Interview



Implementation:

1- First, check if the whole array is in 
   increasing order, if yes then print arr[n-1].
   and return.

2- If above statement doesn't run even single
   time that means an array is in decreasing 
   order from starting. Two cases arise:
   
      a) Check if the whole array is in 
         decreasing order, if yes then 
         print arr[0] and return.
     (b) Otherwise, the array is descending
         rotated and the maximum element will
         be the index before which array was 
         decreasing.

3- If first point partially satisfies that 
   means the whole array is not in increasing 
   order that means an array is ascending 
   rotated and the maximum element will be the
   point from where it starts decreasing.

C/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find type of array, ascending
// descending, clockwise rotated or anti-clockwise
// rotated.
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the type of an array
// and maximum element in it.
void findType(int arr[] , int n)
{
    int i = 0;
  
    // Check if the array is in ascending order
    while (i < n-1 && arr[i] <= arr[i+1])
        i++;
  
    // If i reaches to last index that means
    // all elements are in increasing order
    if (i == n-1)
    {
        cout << "Ascending with maximum element = "
             << arr[n-1]<<endl ;
        return ;
    }
  
    // If first element is greater than next one
    if (i == 0)
    {
        while (i < n-1 && arr[i] >= arr[i+1])
            i++;
  
        // If i reaches to last index
        if (i == n - 1)
        {
            cout << "Descending with maximum element =  "
                 << arr[0] << endl;
            return ;
        }
  
        // If the whole array is not in decreasing order
        // that means it is first decreasing then
        // increasing, i.e., descending rotated, so
        // its maximum element will be the point breaking
        // the order i.e. i so, max will be i+1
        if (arr[0] < arr[i+1])
        {
            cout << "Descending rotated with maximum element = "
                 << max(arr[0], arr[i+1]) << endl;
            return ;
        }
        else
        {
            cout << "Ascending rotated with maximum element = "
                 << max(arr[0], arr[i+1]) << endl;
            return ;
        }
    }
  
    // If whole array is not increasing that means at some
    // point it is decreasing, which makes it ascending rotated
    // with max element as the decreasing point
    if (i < n -1 && arr[0] > arr[i+1])
    {
        cout << "Ascending rotated with maximum element =  "
             << max(arr[i], arr[0]) << endl;
        return;
    }
  
    cout << "Descending rotated with maximum element "
         << max(arr[i],arr[0]) << endl;
}
  
// Driver code
int main()
{
    int arr1[] = { 4, 5, 6, 1, 2, 3}; // Ascending rotated
    int n = sizeof (arr1) / sizeof (arr1[0]);
    findType(arr1, n);
  
    int arr2[] = { 2, 1, 7, 5, 4, 3}; // Descending rotated
    n = sizeof(arr2) / sizeof (arr2[0]);
    findType(arr2, n);
  
    int arr3[] = { 1, 2, 3, 4, 5, 8}; // Ascending
    n = sizeof(arr3) / sizeof (arr3[0]);
    findType(arr3, n);
  
    int arr4[] = { 9, 5, 4, 3, 2, 1}; // Descending
    n = sizeof(arr4) / sizeof (arr4[0]);
    findType(arr4, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find type of array, ascending
// descending, clockwise rotated or anti-clockwise
// rotated.
import java.io.*;
class GFG
{
    public static int max(int a, int b)
    {
        return (a > b)? a : b;
    }
  
    // Function to find the type of an array
    // and maximum element in it
    public static void findType(int arr[])
    {
        int i = 0;
        int n = arr.length;
  
        // Check if the array is in ascending order
        while (i < n-1 && arr[i] <= arr[i+1])
            i++;
  
        // If i reaches to last index that means
        // all elements are in increasing order
        if (i == n-1)
        {
            System.out.println("Ascending with maximum element = " +
                               arr[n-1]);
            return ;
        }
  
        // If first element is greater than next one
        if (i == 0)
        {
            while (i < n-1 && arr[i] >= arr[i+1])
                i++;
  
            // If i reaches to last index
            if (i == n - 1)
            {
                System.out.println("Descending with maximum " +
                                   "element =  " + arr[0]);
                return ;
            }
  
            // If the whole array is not in decreasing order
            // that means it is first decreasing then
            // increasing, i.e., descending rotated, so
            // its maximum element will be the point breaking
            // the order i.e. i so, max will be i+1
            if (arr[0] < arr[i+1])
            {
                System.out.println("Descending rotated with" +
                " maximum element = " + max(arr[0], arr[i+1]));
                return ;
            }
            else
            {
                System.out.println("Ascending rotated with" +
                    " maximum element = " + max(arr[0], arr[i+1]));
                return ;
            }
        }
  
        // If whole array is not increasing that means at some
        // point it is decreasing, which makes it ascending rotated
        // with max element as the decreasing point
        if (i < n-1 && arr[0] > arr[i+1])
        {
  
            System.out.println("Ascending rotated with maximum" +
                            " element =  " + max(arr[i], arr[0]));
            return;
        }
  
        System.out.println("Descending rotated with maximum " +
                               "element " + max(arr[i],arr[0]));
    }
  
    public static void main (String[] args)
    {
        int arr1[] = { 4, 5, 6, 1, 2, 3}; // Ascending rotated
        findType(arr1);
  
        int arr2[] = { 2, 1, 7, 5, 4, 3}; // Descending rotated
        findType(arr2);
  
        int arr3[] = { 1, 2, 3, 4, 5, 8}; // Ascending
        findType(arr3);
  
        int arr4[] = { 9, 5, 4, 3, 2, 1}; // Descending
        findType(arr4);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find type of array, ascending 
# descending, clockwise rotated or anti-clockwise 
# rotated.
  
def findType(arr, n) :
  
    i = 0
  
    # Check if the array is in ascending order 
    while (i < n-1 and arr[i] <= arr[i+1]) :
        i=i+1
  
    # If i reaches to last index that means 
    # all elements are in increasing order 
    if (i == n-1):
        print(("Ascending with maximum element = "
              + str(arr[n-1])))
        return None
      
  
    # If first element is greater than next one 
    if (i == 0):
        while (i < n-1 and arr[i] >= arr[i+1]):
            i=i+1
  
        # If i reaches to last index 
        if (i == n - 1):
            print(("Descending with maximum element = "
                  + str(arr[0])))
            return None
      
  
        # If the whole array is not in decreasing order 
        # that means it is first decreasing then 
        # increasing, i.e., descending rotated, so 
        # its maximum element will be the point breaking 
        # the order i.e. i so, max will be i+1 
        if (arr[0] < arr[i+1]):
            print(("Descending rotated with maximum element = " 
                  + str(max(arr[0], arr[i+1]))))
            return None
        else:
          
            print(("Ascending rotated with maximum element = " 
                  + str(max(arr[0], arr[i+1]))))
                    
            return None
          
      
  
    # If whole array is not increasing that means at some 
    # point it is decreasing, which makes it ascending rotated 
    # with max element as the decreasing point 
    if (i < n -1 and arr[0] > arr[i+1]):
      
        print(("Ascending rotated with maximum element = " 
             + str(max(arr[i], arr[0]))))
        return None
      
  
    print(("Descending rotated with maximum element "
          + str(max(arr[i],arr[0]))))
  
# Driver code 
if __name__=='__main__':
    arr1 = [ 4, 5, 6, 1, 2, 3] # Ascending rotated 
    n = len(arr1)
    findType(arr1, n); 
  
    arr2 = [ 2, 1, 7, 5, 4, 3] # Descending rotated 
    n = len(arr2) 
    findType(arr2, n); 
  
    arr3 = [ 1, 2, 3, 4, 5, 8] # Ascending 
    n = len(arr3) 
    findType(arr3, n); 
  
    arr4 = [ 9, 5, 4, 3, 2, 1] # Descending 
    n = len(arr4) 
    findType(arr4, n); 
  
# this code is contributed by YatinGupta

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find type of array, ascending
// descending, clockwise rotated or anti-clockwise
// rotated.
using System;
  
class GFG {
      
    public static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
  
    // Function to find the type of an array
    // and maximum element in it
    public static void findType(int []arr)
    {
        int i = 0;
        int n = arr.Length;
  
        // Check if the array is in ascending
        // order
        while (i < n-1 && arr[i] <= arr[i+1])
            i++;
  
        // If i reaches to last index that means
        // all elements are in increasing order
        if (i == n-1)
        {
            Console.WriteLine("Ascending with "
              + "maximum element = " + arr[n-1]);
            return ;
        }
  
        // If first element is greater than
        // next one
        if (i == 0)
        {
            while (i < n-1 && arr[i] >= arr[i+1])
                i++;
  
            // If i reaches to last index
            if (i == n - 1)
            {
                Console.WriteLine("Descending with"
                 + " maximum element = " + arr[0]);
                return ;
            }
  
            // If the whole array is not in 
            // decreasing order that means it is 
            // first decreasing then increasing,
            // i.e., descending rotated, so its 
            // maximum element will be the point
            // breaking the order i.e. i so, max
            // will be i+1
            if (arr[0] < arr[i+1])
            {
                Console.WriteLine("Descending rotated"
                          + " with maximum element = "
                             + max(arr[0], arr[i+1]));
                return ;
            }
            else
            {
                Console.WriteLine("Ascending rotated"
                         + " with maximum element = "
                            + max(arr[0], arr[i+1]));
                return ;
            }
        }
  
        // If whole array is not increasing that
        // means at some point it is decreasing, 
        // which makes it ascending rotated with
        // max element as the decreasing point
        if (i < n-1 && arr[0] > arr[i+1])
        {
  
            Console.WriteLine("Ascending rotated"
                     + " with maximum element = "
                          + max(arr[i], arr[0]));
            return;
        }
  
        Console.WriteLine("Descending rotated with"
                              + " maximum element "
                             + max(arr[i],arr[0]));
    }
  
    // Driver code
    public static void Main ()
    {
          
        // Ascending rotated
        int []arr1 = { 4, 5, 6, 1, 2, 3};
        findType(arr1);
  
        // Descending rotated
        int []arr2 = { 2, 1, 7, 5, 4, 3};
        findType(arr2);
  
        // Ascending
        int []arr3 = { 1, 2, 3, 4, 5, 8};
        findType(arr3);
          
        // Descending
        int []arr4 = { 9, 5, 4, 3, 2, 1};
        findType(arr4);
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find type 
// of array, ascending descending, 
// clockwise rotated or anti-clockwise
// rotated.
  
// Function to find the type
// of an array and maximum 
// element in it.
function findType($arr , $n)
{
    $i = 0;
  
    // Check if the array is 
    // in ascending order
    while ($i < $n - 1 and 
           $arr[$i] <= $arr[$i + 1])
        $i++;
  
    // If i reaches to last index 
    // that means all elements are
    // in increasing order
    if ($i == $n - 1)
    {
        echo "Ascending with maximum "
                         "element = "
                    $arr[$n - 1] ,"\n";
        return ;
    }
  
    // If first element is
    // greater than next one
    if ($i == 0)
    {
        while ($i < $n - 1 and 
               $arr[$i] >= $arr[$i + 1])
            $i++;
  
        // If i reaches to last index
        if ($i == $n - 1)
        {
            echo "Descending with maximum "
                              "element = "
                              $arr[0] ,"\n";
            return ;
        }
  
        // If the whole array is not in 
        // decreasing order that means 
        // it is first decreasing then
        // increasing, i.e., descending
        // rotated, so its maximum element 
        // will be the point breaking the 
        // order i.e. i so, max will be i+1
        if ($arr[0] < $arr[$i + 1])
        {
            echo "Descending rotated with ".
                      "maximum element = "
                                max($arr[0],
                        $arr[$i + 1]), "\n";
            return ;
        }
        else
        {
            echo "Ascending rotated with " .
                       "maximum element = "
                                max($arr[0], 
                       $arr[$i + 1]) , "\n";
            return ;
        }
    }
  
    // If whole array is not increasing 
    // that means at some point it is 
    // decreasing, which makes it 
    // ascending rotated with max element
    // as the decreasing point
    if ($i < $n - 1 and $arr[0] > $arr[$i + 1])
    {
        echo "Ascending rotated with maximum "
                   "element = " , max($arr[$i], 
                                $arr[0]) ,"\n";
        return;
    }
  
    echo "Descending rotated with maximum " .
                   "element " , max($arr[$i], 
                              $arr[0]) ,"\n";
}
  
// Driver code
  
// Ascending rotated
$arr1 = array( 4, 5, 6, 1, 2, 3); 
$n = count($arr1);
findType($arr1, $n);
  
// Descending rotated
$arr2 = array( 2, 1, 7, 5, 4, 3); 
$n = count($arr2);
findType($arr2, $n);
  
// Ascending
$arr3 = array( 1, 2, 3, 4, 5, 8); 
$n = count($arr3);
findType($arr3, $n);
  
// Descending
$arr4 = array( 9, 5, 4, 3, 2, 1); 
$n = count($arr4);
findType($arr4, $n);
  
// This code is contributed by anuj_67.
?>

chevron_right


Output :

Ascending rotated with maximum element = 6
Descending rotated with maximum element = 7
Ascending with maximum element = 8
Descending with maximum element = 9

Time Complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, vt_m, YatinGupta



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.