Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximum XOR Queries With an Element From Array
Next article icon

Type of array and its maximum element

Last Updated : 08 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an array, it can be of 4 types. 

  1. Ascending 
  2. Descending 
  3. Ascending Rotated 
  4.  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

Recommended Practice
Type of array
Try It!

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.

Implementation:

C++




// 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;
}
 
 

Java




// 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);
    }
}
 
 

Python3




# 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
 
 

C#




// 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.
 
 

PHP




<?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.
?>
 
 

Javascript




<script>
    // Javascript program to find type of array, ascending
    // descending, clockwise rotated or anti-clockwise
    // rotated.
     
    function max(a, b)
    {
        return (a > b) ? a : b;
    }
   
    // Function to find the type of an array
    // and maximum element in it
    function findType(arr)
    {
        let i = 0;
        let 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) {
            document.write("Ascending with "
                              + "maximum element = " + arr[n - 1] + "</br>");
            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) {
                document.write("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]) {
                document.write("Descending rotated"
                                  + " with maximum element = "
                                  + max(arr[0], arr[i + 1]) + "</br>");
                return;
            }
            else {
                document.write("Ascending rotated"
                                  + " with maximum element = "
                                  + max(arr[0], arr[i + 1]) + "</br>");
                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]) {
   
            document.write("Ascending rotated"
                              + " with maximum element = "
                              + max(arr[i], arr[0]) + "</br>");
            return;
        }
   
        document.write("Descending rotated with"
                          + " maximum element "
                          + max(arr[i], arr[0]) + "</br>");
    }
     
    // Ascending rotated
    let arr1 = [ 4, 5, 6, 1, 2, 3 ];
    findType(arr1);
 
    // Descending rotated
    let arr2 = [ 2, 1, 7, 5, 4, 3 ];
    findType(arr2);
 
    // Ascending
    let arr3 = [ 1, 2, 3, 4, 5, 8 ];
    findType(arr3);
 
    // Descending
    let arr4 = [ 9, 5, 4, 3, 2, 1 ];
    findType(arr4);
     
    // This code is contributed by rameshtravel07.
</script>
 
 
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)

Another approach: Iterate through the array once and store the indices of the first and the last occurrences of the minimum and the maximum element of the array. Now there are four cases: 

  1. If the first occurrence of the minimum element is at the beginning of the array and the last occurrence of the maximum element is at the end of the array, then the array is in ascending order. For example, {1, 1, 1, 2, 3, 4, 5, 6, 6, 6}.
  2. If the first occurrence of the maximum element is at the beginning of the array and the last occurrence of the minimum element is at the end of the array, then the array is in descending order. For example, {6, 6, 6, 5, 4, 3, 2, 1, 1, 1}.
  3. If the first occurrence of the maximum element is equal to the last occurrence of the minimum element + 1 then the array is in descending rotated order. For example, {3, 2, 1, 1, 1, 6, 6, 6, 5, 4}.
  4. If the first occurrence of the minimum element is equal to the last occurrence of the maximum element + 1 then the array is in ascending rotating order. For example, {4, 5, 6, 6, 6, 1, 1, 1, 2, 3}.

Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#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)
{
 
    // To store the minimum and the maximum
    // element from the array
    int min_element = INT_MAX, max_element = INT_MIN;
 
    // To store the first and the last occurrences
    // of the minimum and the maximum
    // element from the array
    int min_index1, max_index1, max_index2, min_index2;
 
    for (int i = 0; i < n; i++) {
 
        // If new minimum is found
        if (arr[i] < min_element) {
 
            // Update the minimum so far
            // and its occurrences
            min_element = arr[i];
            min_index1 = i;
            min_index2 = i;
        }
 
        // If current element is equal the found
        // minimum so far then update the last
        // occurrence of the minimum element
        else if (arr[i] == min_element)
            min_index2 = i;
 
        // If new maximum is found
        if (arr[i] > max_element) {
 
            // Update the maximum so far
            // and its occurrences
            max_element = arr[i];
            max_index1 = i;
            max_index2 = i;
        }
 
        // If current element is equal the found
        // maximum so far then update the last
        // occurrence of the maximum element
        else if (arr[i] == max_element)
            max_index2 = i;
    }
 
    // First occurrence of minimum element is at the
    // beginning of the array and the last occurrence
    // of the maximum element is at the end of the
    // array then the array is sorted in ascending
    // For example, {1, 1, 1, 2, 3, 4, 5, 6, 6, 6}
    if (min_index1 == 0 && max_index2 == n - 1) {
        cout << "Ascending with maximum element = "
             << max_element << endl;
    }
 
    // First occurrence of maximum element is at the
    // beginning of the array and the last occurrence
    // of the minimum element is at the end of the
    // array then the array is sorted in descending
    // For example, {6, 6, 6, 5, 4, 3, 2, 1, 1, 1}
    else if (min_index2 == n - 1 && max_index1 == 0) {
        cout << "Descending with maximum element = "
             << max_element << endl;
    }
 
    // First occurrence of maximum element is equal
    // to the last occurrence of the minimum element + 1
    // then the array is descending and rotated
    // For example, {3, 2, 1, 1, 1, 6, 6, 6, 5, 4}
    else if (max_index1 == min_index2 + 1) {
        cout << "Descending rotated with maximum element = "
             << max_element << endl;
    }
 
    // First occurrence of minimum element is equal
    // to the last occurrence of the maximum element + 1
    // then the array is ascending and rotated
    // For example, {4, 5, 6, 6, 6, 1, 1, 1, 2, 3}
    else {
        cout << "Ascending rotated with maximum element = "
             << max_element << endl;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 4, 5, 6, 6, 6, 1, 1, 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    findType(arr, n);
 
    return 0;
}
 
 

Java




// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to find the type of an array
// and maximum element in it
static void findType(int arr[], int n)
{
 
    // To store the minimum and the maximum
    // element from the array
    int min_element = Integer.MAX_VALUE,
        max_element = Integer.MIN_VALUE;
 
    // To store the first and the last occurrences
    // of the minimum and the maximum
    // element from the array
    int min_index1 = -1, max_index1 = -1,
        max_index2 = -1, min_index2 = -1;
 
    for (int i = 0; i < n; i++)
    {
 
        // If new minimum is found
        if (arr[i] < min_element)
        {
 
            // Update the minimum so far
            // and its occurrences
            min_element = arr[i];
            min_index1 = i;
            min_index2 = i;
        }
 
        // If current element is equal the found
        // minimum so far then update the last
        // occurrence of the minimum element
        else if (arr[i] == min_element)
            min_index2 = i;
 
        // If new maximum is found
        if (arr[i] > max_element)
        {
 
            // Update the maximum so far
            // and its occurrences
            max_element = arr[i];
            max_index1 = i;
            max_index2 = i;
        }
 
        // If current element is equal the found
        // maximum so far then update the last
        // occurrence of the maximum element
        else if (arr[i] == max_element)
            max_index2 = i;
    }
 
    // First occurrence of minimum element is at the
    // beginning of the array and the last occurrence
    // of the maximum element is at the end of the
    // array then the array is sorted in ascending
    // For example, {1, 1, 1, 2, 3, 4, 5, 6, 6, 6}
    if (min_index1 == 0 && max_index2 == n - 1)
    {
        System.out.println("Ascending with maximum" +  
                        " element = " + max_element);
    }
 
    // First occurrence of maximum element is at the
    // beginning of the array and the last occurrence
    // of the minimum element is at the end of the
    // array then the array is sorted in descending
    // For example, {6, 6, 6, 5, 4, 3, 2, 1, 1, 1}
    else if (min_index2 == n - 1 && max_index1 == 0)
    {
        System.out.println("Descending with maximum" +
                         " element = " + max_element);
    }
 
    // First occurrence of maximum element is equal
    // to the last occurrence of the minimum element + 1
    // then the array is descending and rotated
    // For example, {3, 2, 1, 1, 1, 6, 6, 6, 5, 4}
    else if (max_index1 == min_index2 + 1)
    {
        System.out.println("Descending rotated with " +
                   "maximum element = " + max_element);
    }
 
    // First occurrence of minimum element is equal
    // to the last occurrence of the maximum element + 1
    // then the array is ascending and rotated
    // For example, {4, 5, 6, 6, 6, 1, 1, 1, 2, 3}
    else
    {
        System.out.println("Ascending rotated with " +
                  "maximum element = " + max_element);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 5, 6, 6, 6, 1, 1, 1, 2, 3 };
    int n = arr.length;
 
    findType(arr, n);
}
}
 
// This code is contributed by Princi Singh
 
 

Python3




# Python3 implementation of the approach
# Function to find the type of an array
# and maximum element in it
import sys
 
def findType(arr, n):
 
    # To store the minimum and the maximum
    # element from the array
    min_element = sys.maxsize;
    max_element = -sys.maxsize;
 
    # To store the first and the last occurrences
    # of the minimum and the maximum
    # element from the array
    min_index1 = -1; max_index1 = -1,
    max_index2 = -1; min_index2 = -1;
 
    for i in range(n):
 
        # If new minimum is found
        if (arr[i] < min_element):
 
            # Update the minimum so far
            # and its occurrences
            min_element = arr[i];
            min_index1 = i;
            min_index2 = i;
         
        # If current element is equal the found
        # minimum so far then update the last
        # occurrence of the minimum element
        else if (arr[i] == min_element):
            min_index2 = i;
 
        # If new maximum is found
        if (arr[i] > max_element):
         
            # Update the maximum so far
            # and its occurrences
            max_element = arr[i];
            max_index1 = i;
            max_index2 = i;
         
        # If current element is equal the found
        # maximum so far then update the last
        # occurrence of the maximum element
        else if (arr[i] == max_element):
            max_index2 = i;
 
    # First occurrence of minimum element is at the
    # beginning of the array and the last occurrence
    # of the maximum element is at the end of the
    # array then the array is sorted in ascending
    # For example, 1, 1, 1, 2, 3, 4, 5, 6, 6, 6
    if (min_index1 == 0 and max_index2 == n - 1):
     
        print("Ascending with maximum",
                          "element = ", max_element);
     
    # First occurrence of maximum element is at the
    # beginning of the array and the last occurrence
    # of the minimum element is at the end of the
    # array then the array is sorted in descending
    # For example, 6, 6, 6, 5, 4, 3, 2, 1, 1, 1
    else if (min_index2 == n - 1 and max_index1 == 0):
     
        print("Descending with maximum",
                           "element = ", max_element);
     
    # First occurrence of maximum element is equal
    # to the last occurrence of the minimum element + 1
    # then the array is descending and rotated
    # For example, [3, 2, 1, 1, 1, 6, 6, 6, 5, 4]
    else if (max_index1 == min_index2 , 1):
     
        print("Descending rotated with",
                   "maximum element = ", max_element);
     
    # First occurrence of minimum element is equal
    # to the last occurrence of the maximum element + 1
    # then the array is ascending and rotated
    # For example, [4, 5, 6, 6, 6, 1, 1, 1, 2, 3]
    else:
     
        print("Ascending rotated with",
                  "maximum element = ", max_element);
     
# Driver code
arr = [4, 5, 6, 6, 6, 1, 1, 1, 2, 3];
n = len(arr);
 
findType(arr, n);
 
# This code is contributed by 29AjayKumar
 
 

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to find the type of an array
// and maximum element in it
static void findType(int []arr, int n)
{
 
    // To store the minimum and the maximum
    // element from the array
    int min_element = int.MaxValue,
        max_element = int.MinValue;
 
    // To store the first and the last occurrences
    // of the minimum and the maximum
    // element from the array
    int min_index1 = -1, max_index1 = -1,
        max_index2 = -1, min_index2 = -1;
 
    for (int i = 0; i < n; i++)
    {
 
        // If new minimum is found
        if (arr[i] < min_element)
        {
 
            // Update the minimum so far
            // and its occurrences
            min_element = arr[i];
            min_index1 = i;
            min_index2 = i;
        }
 
        // If current element is equal the found
        // minimum so far then update the last
        // occurrence of the minimum element
        else if (arr[i] == min_element)
            min_index2 = i;
 
        // If new maximum is found
        if (arr[i] > max_element)
        {
 
            // Update the maximum so far
            // and its occurrences
            max_element = arr[i];
            max_index1 = i;
            max_index2 = i;
        }
 
        // If current element is equal the found
        // maximum so far then update the last
        // occurrence of the maximum element
        else if (arr[i] == max_element)
            max_index2 = i;
    }
 
    // First occurrence of minimum element is at the
    // beginning of the array and the last occurrence
    // of the maximum element is at the end of the
    // array then the array is sorted in ascending
    // For example, {1, 1, 1, 2, 3, 4, 5, 6, 6, 6}
    if (min_index1 == 0 && max_index2 == n - 1)
    {
        Console.Write("Ascending with maximum" +
                   " element = " + max_element);
    }
 
    // First occurrence of maximum element is at the
    // beginning of the array and the last occurrence
    // of the minimum element is at the end of the
    // array then the array is sorted in descending
    // For example, {6, 6, 6, 5, 4, 3, 2, 1, 1, 1}
    else if (min_index2 == n - 1 && max_index1 == 0)
    {
        Console.Write("Descending with maximum" +
                    " element = " + max_element);
    }
 
    // First occurrence of maximum element is equal
    // to the last occurrence of the minimum element + 1
    // then the array is descending and rotated
    // For example, {3, 2, 1, 1, 1, 6, 6, 6, 5, 4}
    else if (max_index1 == min_index2 + 1)
    {
        Console.Write("Descending rotated with " +
              "maximum element = " + max_element);
    }
 
    // First occurrence of minimum element is equal
    // to the last occurrence of the maximum element + 1
    // then the array is ascending and rotated
    // For example, {4, 5, 6, 6, 6, 1, 1, 1, 2, 3}
    else
    {
        Console.Write("Ascending rotated with " +
             "maximum element = " + max_element);
    }
}
 
// Driver code
static public void Main ()
{
    int []arr = { 4, 5, 6, 6, 6, 1, 1, 1, 2, 3 };
    int n = arr.Length;
     
    findType(arr, n);
}
}
 
// This code is contributed by ajit.
 
 

Javascript




<script>
    // Javascript implementation of the approach
     
    // Function to find the type of an array
    // and maximum element in it
    function findType(arr, n)
    {
 
        // To store the minimum and the maximum
        // element from the array
        let min_element = Number.MAX_VALUE,
            max_element = Number.MIN_VALUE;
 
        // To store the first and the last occurrences
        // of the minimum and the maximum
        // element from the array
        let min_index1 = -1, max_index1 = -1,
            max_index2 = -1, min_index2 = -1;
 
        for (let i = 0; i < n; i++)
        {
 
            // If new minimum is found
            if (arr[i] < min_element)
            {
 
                // Update the minimum so far
                // and its occurrences
                min_element = arr[i];
                min_index1 = i;
                min_index2 = i;
            }
 
            // If current element is equal the found
            // minimum so far then update the last
            // occurrence of the minimum element
            else if (arr[i] == min_element)
                min_index2 = i;
 
            // If new maximum is found
            if (arr[i] > max_element)
            {
 
                // Update the maximum so far
                // and its occurrences
                max_element = arr[i];
                max_index1 = i;
                max_index2 = i;
            }
 
            // If current element is equal the found
            // maximum so far then update the last
            // occurrence of the maximum element
            else if (arr[i] == max_element)
                max_index2 = i;
        }
 
        // First occurrence of minimum element is at the
        // beginning of the array and the last occurrence
        // of the maximum element is at the end of the
        // array then the array is sorted in ascending
        // For example, {1, 1, 1, 2, 3, 4, 5, 6, 6, 6}
        if (min_index1 == 0 && max_index2 == n - 1)
        {
            document.write("Ascending with maximum" +
                       " element = " + max_element);
        }
 
        // First occurrence of maximum element is at the
        // beginning of the array and the last occurrence
        // of the minimum element is at the end of the
        // array then the array is sorted in descending
        // For example, {6, 6, 6, 5, 4, 3, 2, 1, 1, 1}
        else if (min_index2 == n - 1 && max_index1 == 0)
        {
            document.write("Descending with maximum" +
                        " element = " + max_element);
        }
 
        // First occurrence of maximum element is equal
        // to the last occurrence of the minimum element + 1
        // then the array is descending and rotated
        // For example, {3, 2, 1, 1, 1, 6, 6, 6, 5, 4}
        else if (max_index1 == min_index2 + 1)
        {
            document.write("Descending rotated with " +
                  "maximum element = " + max_element);
        }
 
        // First occurrence of minimum element is equal
        // to the last occurrence of the maximum element + 1
        // then the array is ascending and rotated
        // For example, {4, 5, 6, 6, 6, 1, 1, 1, 2, 3}
        else
        {
            document.write("Ascending rotated with " +
                 "maximum element = " + max_element);
        }
    }
     
    let arr = [ 4, 5, 6, 6, 6, 1, 1, 1, 2, 3 ];
    let n = arr.length;
      
    findType(arr, n);
       
</script>
 
 
Output
Ascending rotated with maximum element = 6


Next Article
Maximum XOR Queries With an Element From Array
author
kartik
Improve
Article Tags :
  • Arrays
  • DSA
  • Amazon
Practice Tags :
  • Amazon
  • Arrays

Similar Reads

  • Maximum element in a sorted and rotated array
    Given a sorted array arr[] (may contain duplicates) of size n that is rotated at some unknown point, the task is to find the maximum element in it. Examples: Input: arr[] = {5, 6, 1, 2, 3, 4}Output: 6Explanation: 6 is the maximum element present in the array. Input: arr[] = {3, 2, 2, 2}Output: 3Expl
    7 min read
  • Find element with the maximum set bits in an array
    Given an array arr[]. The task is to find an element from arr[] which has the maximum count of set bits.Examples: Input: arr[] = {10, 100, 1000, 10000} Output: 1000 Binary(10) = 1010 (2 set bits) Binary(100) = 1100100 (3 set bits) Binary(1000) = 1111101000 (6 set bits) Binary(10000) = 10011100010000
    5 min read
  • Program to find the minimum (or maximum) element of an array
    Given an array, write functions to find the minimum and maximum elements in it. Examples: Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423 Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of
    14 min read
  • Maximum XOR Queries With an Element From Array
    Given an array arr[] of size n containing non-negative integers and also given a list of q queries in a 2D array queries[][], where each query is of the form [xi, mi]. For each query, you need to find the maximum value of (xi XOR arr[j]) such that arr[j] is less than or equal to mi. In simple terms,
    15+ min read
  • Largest element in an Array
    Given an array arr. The task is to find the largest element in the given array. Examples: Input: arr[] = [10, 20, 4]Output: 20Explanation: Among 10, 20 and 4, 20 is the largest. Input: arr[] = [20, 10, 20, 4, 100]Output: 100 Table of Content Iterative Approach - O(n) Time and O(1) SpaceRecursive App
    7 min read
  • Maximum AND value of a pair in an array
    Given an array of N positive elements, the task is to find the maximum AND value generated by any pair of elements from the array. Examples: Input: arr1[] = {16, 12, 8, 4}Output: 8Explanation: 8 AND12 will give us the maximum value 8 Input: arr1[] = {4, 8, 16, 2}Output: 0 Recommended PracticeMaximum
    12 min read
  • Most frequent element in an array
    Given an array, the task is to find the most frequent element in it. If there are multiple elements that appear a maximum number of times, return the maximum element. Examples: Input : arr[] = [1, 3, 2, 1, 4, 1]Output : 1Explanation: 1 appears three times in array which is maximum frequency. Input :
    10 min read
  • Maximum element in an array which is equal to its frequency
    Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h
    11 min read
  • Find a number that divides maximum array elements
    Given an array A[] of N non-negative integers. Find an Integer greater than 1, such that maximum array elements are divisible by it. In case of same answer print the smaller one. Examples: Input : A[] = { 2, 4, 5, 10, 8, 15, 16 }; Output : 2 Explanation: 2 divides [ 2, 4, 10, 8, 16] no other element
    13 min read
  • Maximize elements using another array
    Given two arrays a[] and b[] of size n. The task is to maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first arr
    15 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences