![]() |
VOOZH | about |
Given an array arr[], check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.
Examples:
Input: arr[] = [10, 20, 30, 40, 50]
Output: true
Explanation: The given array is sorted.Input: arr[] = [90, 80, 100, 70, 40, 30]
Output: false
Explanation: The given array is not sorted.
Table of Content
We traverse from the second element. For every element we check if it is smaller than or equal to previous element or not. At any point if we find previous element greater, we return false.
For example: arr[] = [10, 20, 30, 5, 6]
i = 1 : (10 <= 20), continue
i = 2 : (20 <= 30), continue
i = 3 : (30 > 5), return false.
true
The idea is to check if the last two elements are in order, then recursively check the rest of the array. The base case is when the array has zero or one element, which is always considered sorted.
Step-By-Step Approach:
true
The idea behind here is we use built-in methods in STL of C++ and Python to check if an array is sorted or not.
true