Given an array arr[] of size N, check if it is sorted in non-decreasing order or not.
Example 1:
Input:
N=5
arr[] = {10, 20, 30, 40, 50}
Output: YES
Explanation: The given array is sorted.
Example 2:
Input:
N-6
arr[] = {90, 80, 100, 70, 40, 30}
Output: NO
Explanation: The given array is not sorted.
for Checking if an array is sorted, you can follow these steps:-
1. Input:- The code first takes an integer n as input, representing the number of elements in
the array.
2. Array input:- It then takes input for an array of integers arr with n elements using a for
loop.
3. Assumption:-The code initializes a boolean variable isSorted to true, assuming that the
array is sorted
initially.
4. Array Traversal:- The code uses another for loop to iterate through the elements of the
array from the first
element (index 0) to the second-to-last element (index n - 2). The purpose of this loop is to compare
adjacent elements to check if they are in non-decreasing order.
If arr[i] is greater than arr[i + 1], it means the array is not sorted in non-decreasing order. In this
case, isSorted is set to false, and the loop breaks because there is no need to continue checking the
remaining elements.
5. Output:- After the loop, the code checks the value of isSorted. If it remains true after the
loop, it means
the array is sorted in non-decreasing order, and it outputs "YES." Otherwise, it outputs "NO."