You are given an array arr[] having unique elements. Your task is to return the type of array described below.
- Return 1 if the array is in ascending order
- Return 2 if the array is in descending order
- Return 3 if the array is in descending rotated order
- Return 4 if the array is in ascending rotated order
You may assume that the input array is always one of the four types.
Examples:
Input: arr[] = [2, 1, 5, 4, 3]
Output: 3
Explanation: Descending rotated, rotate 2 times left.Input: arr[] = [3, 4, 5, 1, 2]
Output: 4
Explanation: Ascending rotated, rotate 2 times right.
Table of Content
[Naive Approach] Using Single Pass Order Detection - O(n) Time O(1) Space
The Idea is to Traverse the array once to find where the sorted order breaks. If no break occurs, the array is ascending or descending. If a break appears in between, the array is rotated. The position of the first break point (i) determines the type of array.
Algorithm:
- Start from index 0 and move forward while elements are in increasing order.
- If traversal reaches the end, the array is Ascending -> return 1.
- If the order breaks at index 0, check if the array is fully decreasing -> return 2.
- If not fully decreasing, decide rotation: increasing next -> Descending Rotated (3), else Ascending Rotated (4).
- If the array was partially increasing, use the next element to classify -> Ascending Rotated (4) or Descending Rotated (3).
// 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.
int typeOfArr(vector<int> &arr)
{
int n = arr.size();
int i = 0;
// Check ascending
while (i < n - 1 && arr[i] <= arr[i + 1])
i++;
// array increasing to Ascending
if (i == n - 1)
{
return 1;
}
// decreasing
if (i == 0)
{
while (i < n - 1 && arr[i] >= arr[i + 1])
i++;
// array decreasing is Descending
if (i == n - 1)
{
return 2;
}
// Rotated cases
if (arr[0] < arr[i + 1])
{
return 3; // Descending Rotated
}
else
{
return 4; // Ascending Rotated
}
}
// Partially increasing → Ascending Rotated
if (i < n - 1 && arr[0] > arr[i + 1])
{
return 4;
}
// Otherwise Descending Rotated
return 3;
}
// Driver code
int main() {
vector<int> arr = {2, 1, 5, 4, 3};
int result = typeOfArr(arr);
cout << "Type of array: " << result << endl;
return 0;
}
#include <stdio.h>
// Function to find the type of an array
// and maximum element in it.
int typeOfArr(int arr[], int n)
{
int i = 0;
// Check ascending
while (i < n - 1 && arr[i] <= arr[i + 1])
i++;
// array increasing to Ascending
if (i == n - 1)
{
return 1;
}
// decreasing
if (i == 0)
{
while (i < n - 1 && arr[i] >= arr[i + 1])
i++;
// array decreasing is Descending
if (i == n - 1)
{
return 2;
}
// Rotated cases
if (arr[0] < arr[i + 1])
{
return 3; // Descending Rotated
}
else
{
return 4; // Ascending Rotated
}
}
// Partially increasing → Ascending Rotated
if (i < n - 1 && arr[0] > arr[i + 1])
{
return 4;
}
// Otherwise Descending Rotated
return 3;
}
// Driver code
int main() {
int arr[] = {2, 1, 5, 4, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int result = typeOfArr(arr, n);
printf("Type of array: %d\n", result);
return 0;
}
import java.util.*;
// Function to find the type of an array
// and maximum element in it.
public class GfG {
public static int typeOfArr(int[] arr) {
int n = arr.length;
int i = 0;
// Check ascending
while (i < n - 1 && arr[i] <= arr[i + 1])
i++;
// array increasing to Ascending
if (i == n - 1)
{
return 1;
}
// decreasing
if (i == 0)
{
while (i < n - 1 && arr[i] >= arr[i + 1])
i++;
// array decreasing is Descending
if (i == n - 1)
{
return 2;
}
// Rotated cases
if (arr[0] < arr[i + 1])
{
return 3; // Descending Rotated
}
else
{
return 4; // Ascending Rotated
}
}
// Partially increasing → Ascending Rotated
if (i < n - 1 && arr[0] > arr[i + 1])
{
return 4;
}
// Otherwise Descending Rotated
return 3;
}
// Driver code
public static void main(String[] args) {
int[] arr = {2, 1, 5, 4, 3};
int result = typeOfArr(arr);
System.out.println("Type of array: " + result);
}
}
class Solution:
# Function to find the type of an array
# and maximum element in it.
def typeOfArr(self, arr):
n = len(arr)
i = 0
# Check ascending
while i < n - 1 and arr[i] <= arr[i + 1]:
i += 1
# array increasing to Ascending
if i == n - 1:
return 1
# decreasing
if i == 0:
while i < n - 1 and arr[i] >= arr[i + 1]:
i += 1
# array decreasing is Descending
if i == n - 1:
return 2
# Rotated cases
if arr[0] < arr[i + 1]:
return 3 # Descending Rotated
else:
return 4 # Ascending Rotated
# Partially increasing → Ascending Rotated
if i < n - 1 and arr[0] > arr[i + 1]:
return 4
# Otherwise Descending Rotated
return 3
# Driver code
if __name__ == "__main__":
arr = [2, 1, 5, 4, 3]
obj = Solution()
result = obj.typeOfArr(arr)
print("Type of array:", result)
// C# program to find type of array, ascending
// descending, clockwise rotated or anti-clockwise
// rotated.
using System;
using System.Collections.Generic;
class GfG
{
// Function to find the type of an array
// and maximum element in it.
static int typeOfArr(List<int> arr)
{
int n = arr.Count;
int i = 0;
// Check ascending
while (i < n - 1 && arr[i] <= arr[i + 1])
i++;
// array increasing to Ascending
if (i == n - 1)
{
return 1;
}
// decreasing
if (i == 0)
{
while (i < n - 1 && arr[i] >= arr[i + 1])
i++;
// array decreasing is Descending
if (i == n - 1)
{
return 2;
}
// Rotated cases
if (arr[0] < arr[i + 1])
{
return 3; // Descending Rotated
}
else
{
return 4; // Ascending Rotated
}
}
// Partially increasing → Ascending Rotated
if (i < n - 1 && arr[0] > arr[i + 1])
{
return 4;
}
// Otherwise Descending Rotated
return 3;
}
// Driver code
static void Main()
{
List<int> arr = new List<int> { 2, 1, 5, 4, 3 };
int result = typeOfArr(arr);
Console.WriteLine("Type of array: " + result);
}
}
// JavaScript 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 typeOfArr(arr) {
let n = arr.length;
let i = 0;
// Check ascending
while (i < n - 1 && arr[i] <= arr[i + 1])
i++;
// array increasing to Ascending
if (i == n - 1)
{
return 1;
}
// decreasing
if (i == 0)
{
while (i < n - 1 && arr[i] >= arr[i + 1])
i++;
// array decreasing is Descending
if (i == n - 1)
{
return 2;
}
// Rotated cases
if (arr[0] < arr[i + 1])
{
return 3; // Descending Rotated
}
else
{
return 4; // Ascending Rotated
}
}
// Partially increasing → Ascending Rotated
if (i < n - 1 && arr[0] > arr[i + 1])
{
return 4;
}
// Otherwise Descending Rotated
return 3;
}
// Driver code
let arr = [2, 1, 5, 4, 3];
let result = typeOfArr(arr);
console.log("Type of array: " + result);
Output
Type of array: 3
Time Complexity : O(n)
Auxiliary Space : O(1)
[Expected Approach] Using Maximum Element Position - O(n) Time O(1) Space
The Idea is to traverse the array to find the maximum element and its index. If the maximum is at the start, the array is descending; if at the end, it is ascending. Otherwise, the array is rotated, and its type is determined using the neighbors of the maximum element.
Algorithm:
- Traverse array to find maximum element and its index.
- If max is at index 0 and array is decreasing -> Descending (2).
- If max is at last index and array is increasing -> Ascending (1).
- Compare neighbors of max: left > right -> Ascending Rotated (4).
- Otherwise -> Descending Rotated (3).
// Function to find the type of an array
// and maximum element in it
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
int typeOfArr(vector<int> &arr)
{
// Size of the array
int n = arr.size();
// To store the maximum element and its index
int max = 0, index = 0, type;
// Traverse the array to find maximum element
// and its position
for (int i = 0; i < n; i++)
{
// If a new maximum is found
if (arr[i] > max)
{
// Update maximum element
// and its index
max = arr[i];
index = i;
}
}
// Case 1:
// If maximum element is at the beginning
// and array is in decreasing order
// then it is a Descending array
if (arr[0] == max && arr[n - 2] > arr[n - 1])
type = 2;
// Case 2:
// If maximum element is at the end
// and array is in increasing order
// then it is an Ascending array
else if (arr[n - 1] == max && arr[1] > arr[0])
type = 1;
// Case 3:
// Check neighbors of maximum element
// If left neighbor is greater than right neighbor
// then array is Ascending Rotated
else if (arr[(n + index - 1) % n] > arr[(n + index + 1) % n])
type = 4;
// Case 4:
// Otherwise, array is Descending Rotated
else
type = 3;
// Return the type of array
return type;
}
// Driver code
int main() {
vector<int> v = {2, 1, 5, 4, 3};
int result = typeOfArr(v);
cout << "Type of array: " << result << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Function to find the type of an array
// and maximum element in it
// C implementation of the approach
int typeOfArr(int arr[], int n)
{
// To store the maximum element and its index
int max = 0, index = 0, type;
// Traverse the array to find maximum element
// and its position
for (int i = 0; i < n; i++)
{
// If a new maximum is found
if (arr[i] > max)
{
// Update maximum element
// and its index
max = arr[i];
index = i;
}
}
// Case 1:
// If maximum element is at the beginning
// and array is in decreasing order
// then it is a Descending array
if (arr[0] == max && arr[n - 2] > arr[n - 1])
type = 2;
// Case 2:
// If maximum element is at the end
// and array is in increasing order
// then it is an Ascending array
else if (arr[n - 1] == max && arr[1] > arr[0])
type = 1;
// Case 3:
// Check neighbors of maximum element
// If left neighbor is greater than right neighbor
// then array is Ascending Rotated
else if (arr[(n + index - 1) % n] > arr[(n + index + 1) % n])
type = 4;
// Case 4:
// Otherwise, array is Descending Rotated
else
type = 3;
// Return the type of array
return type;
}
// Driver code
int main() {
int v[] = {2, 1, 5, 4, 3};
int n = sizeof(v) / sizeof(v[0]);
int result = typeOfArr(v, n);
printf("Type of array: %d\n", result);
return 0;
}
// Function to find the type of an array
// and maximum element in it
// Java implementation of the approach
public class GfG {
static int typeOfArr(int[] arr) {
// Size of the array
int n = arr.length;
// To store the maximum element and its index
int max = 0, index = 0, type;
// Traverse the array to find maximum element
// and its position
for (int i = 0; i < n; i++) {
// If a new maximum is found
if (arr[i] > max) {
// Update maximum element
// and its index
max = arr[i];
index = i;
}
}
// Case 1:
// If maximum element is at the beginning
// and array is in decreasing order
// then it is a Descending array
if (arr[0] == max && arr[n - 2] > arr[n - 1])
type = 2;
// Case 2:
// If maximum element is at the end
// and array is in increasing order
// then it is an Ascending array
else if (arr[n - 1] == max && arr[1] > arr[0])
type = 1;
// Case 3:
// Check neighbors of maximum element
// If left neighbor is greater than right neighbor
// then array is Ascending Rotated
else if (arr[(n + index - 1) % n] > arr[(n + index + 1) % n])
type = 4;
// Case 4:
// Otherwise, array is Descending Rotated
else
type = 3;
// Return the type of array
return type;
}
public static void main(String[] args) {
int[] v = {2, 1, 5, 4, 3};
int result = typeOfArr(v);
System.out.println("Type of array: " + result);
}
}
def typeOfArr(arr):
# Size of the array
n = len(arr)
# To store the maximum element and its index
max_val = 0
index = 0
type_val = 0
# Traverse the array to find maximum element
# and its position
for i in range(n):
# If a new maximum is found
if arr[i] > max_val:
# Update maximum element
# and its index
max_val = arr[i]
index = i
# Case 1:
# If maximum element is at the beginning
# and array is in decreasing order
# then it is a Descending array
if arr[0] == max_val and arr[n - 2] > arr[n - 1]:
type_val = 2
# Case 2:
# If maximum element is at the end
# and array is in increasing order
# then it is an Ascending array
elif arr[n - 1] == max_val and arr[1] > arr[0]:
type_val = 1
# Case 3:
# Check neighbors of maximum element
# If left neighbor is greater than right neighbor
# then array is Ascending Rotated
elif arr[(n + index - 1) % n] > arr[(n + index + 1) % n]:
type_val = 4
# Case 4:
# Otherwise, array is Descending Rotated
else:
type_val = 3
# Return the type of array
return type_val
# Driver code
if __name__ == '__main__':
v = [2, 1, 5, 4, 3]
result = typeOfArr(v)
print("Type of array:", result)
// Function to find the type of an array
// and maximum element in it
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GfG {
static int typeOfArr(List<int> arr)
{
// Size of the array
int n = arr.Count;
// To store the maximum element and its index
int max = 0, index = 0, type;
// Traverse the array to find maximum element
// and its position
for (int i = 0; i < n; i++)
{
// If a new maximum is found
if (arr[i] > max)
{
// Update maximum element
// and its index
max = arr[i];
index = i;
}
}
// Case 1:
// If maximum element is at the beginning
// and array is in decreasing order
// then it is a Descending array
if (arr[0] == max && arr[n - 2] > arr[n - 1])
type = 2;
// Case 2:
// If maximum element is at the end
// and array is in increasing order
// then it is an Ascending array
else if (arr[n - 1] == max && arr[1] > arr[0])
type = 1;
// Case 3:
// Check neighbors of maximum element
// If left neighbor is greater than right neighbor
// then array is Ascending Rotated
else if (arr[(n + index - 1) % n] > arr[(n + index + 1) % n])
type = 4;
// Case 4:
// Otherwise, array is Descending Rotated
else
type = 3;
// Return the type of array
return type;
}
// Driver code
static void Main(string[] args) {
List<int> v = new List<int> {2, 1, 5, 4, 3};
int result = typeOfArr(v);
Console.WriteLine("Type of array: " + result);
}
}
// Function to find the type of an array
// and maximum element in it
// JavaScript implementation of the approach
function typeOfArr(arr) {
// Size of the array
let n = arr.length;
// To store the maximum element and its index
let max = 0, index = 0, type;
// Traverse the array to find maximum element
// and its position
for (let i = 0; i < n; i++) {
// If a new maximum is found
if (arr[i] > max) {
// Update maximum element
// and its index
max = arr[i];
index = i;
}
}
// Case 1:
// If maximum element is at the beginning
// and array is in decreasing order
// then it is a Descending array
if (arr[0] === max && arr[n - 2] > arr[n - 1])
type = 2;
// Case 2:
// If maximum element is at the end
// and array is in increasing order
// then it is an Ascending array
else if (arr[n - 1] === max && arr[1] > arr[0])
type = 1;
// Case 3:
// Check neighbors of maximum element
// If left neighbor is greater than right neighbor
// then array is Ascending Rotated
else if (arr[(n + index - 1) % n] > arr[(n + index + 1) % n])
type = 4;
// Case 4:
// Otherwise, array is Descending Rotated
else
type = 3;
// Return the type of array
return type;
}
// Driver code
let v = [2, 1, 5, 4, 3];
let result = typeOfArr(v);
console.log("Type of array: " + result);
Output
Type of array: 3