Check whether array is sorted - II

You are given an integer array nums. Using recursion, determine whether the array is sorted in non-decreasing order.

Return true if every element is greater than or equal to the element before it. Otherwise, return false.

Note:

A normal iterative solution will pass the test cases, but the goal of this problem is to solve it using recursion and strengthen your understanding of recursive calls and base cases.

Example 1:

Input: nums = [1, 2, 2, 4, 5]

Output: true

Explanation: Every element is greater than or equal to the element before it, so nums is sorted in non-decreasing order.

Example 2:

Input: nums = [1, 3, 2, 4]

Output: false

Explanation: The value 2 appears after 3, so nums is not sorted in non-decreasing order.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [-5, -2, 0, 3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 100
  • -104 <= nums[i] <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool isSorted(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums