Learner Level

Implement the isArraySortedAscending method that checks whether the array is sorted in ascending order.

The input contains an integer array nums and its length size. Your task is to check whether the array is sorted in ascending order.

Ascending order means each element must be less than or equal to the element after it. Equal neighboring values are allowed. For example, [1,2,2,4] is sorted, but [1,3,2] is not.

Example 1

Input
nums (int[]) = [1,2,2,4]
size (int) = 4
Returns (boolean) true

Example 2

Input
nums (int[]) = [1,3,2]
size (int) = 3
Returns (boolean) false

Example 3

Input
nums (int[]) = [5]
size (int) = 1
Returns (boolean) true

Compare every element with the element before it.

If any current value is smaller than the previous value, the array is not sorted in ascending order and you can return false. If the loop completes, return true.

Pseudocode:

function isArraySortedAscending(nums, size):
    for i from 1 to size - 1:
        if nums[i] < nums[i - 1]:
            return false
    return true
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.