
Problem
Solution
Submissions
Check if a List is Sorted
Certification: Basic Level
Accuracy: 43.33%
Submissions: 60
Points: 5
Write a Python program that determines whether a given list of numbers is sorted in ascending order.
Example 1
- Input: list = [1, 2, 3, 4, 5]
- Output: True
- Explanation:
- Step 1: Compare each adjacent pair of elements starting from the first element.
- Step 2: Check if element[i] <= element[i+1] for all indices i from 0 to n-2.
- Step 3: Since all adjacent elements satisfy this condition, the list is sorted.
- Step 4: Return True.
Example 2
- Input: list = [1, 3, 2, 4, 5]
- Output: False
- Explanation:
- Step 1: Compare each adjacent pair of elements starting from the first element.
- Step 2: When checking elements at index 1 and 2, we find 3 > 2.
- Step 3: Since this violates the ascending order condition, the list is not sorted.
- Step 4: Return False.
Constraints
- 0 ≤ len(list) ≤ 10^6
- List elements are integers
- Time Complexity: O(n) where n is the length of the list
- Space Complexity: O(1) as no additional space is needed
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Compare adjacent elements: all(nums[i] <= nums[i+1] for i in range(len(nums)-1))
- Create a sorted copy and compare: sorted(nums) == nums
- Use a loop to check each element against the next one