Check if the array is sorted

Beginner Problems Basic Arrays Easy
  • This problem's underlying concept plays an integral role in many database management systems
  • When you search for something in a database, for example, a name in the contact application on your phone, it doesn't start from the beginning and search through every single entry
  • Instead, it uses the concept of a "sorted array" to execute binary search algorithms, allowing for significantly faster search responses
  • So, this fundamental concept contributes critically to the overall efficiency and response time of numerous software applications

Given an array arr of size n, the task is to check if the given array is sorted in (ascending / Increasing / Non-decreasing) order. If the array is sorted then return True, else return False.

Examples:

Input: n = 5, arr = [1,2,3,4,5]


Output: True


Explanation: The given array is sorted i.e Every element in the array is smaller than or equals to its next values, So the answer is True.

Input: n = 5, arr = [5,4,6,7,8]


Output: False


Explanation: The given array is Not sorted i.e Every element in the array is not smaller than or equal to its next values, So the answer is False. Here element 5 is not smaller than or equal to its future elements.

Input: n = 5, arr = [5,4,3,2,1]

Constraints

  • 1 ≤ n ≤ 106
  • - 109 ≤ arr[i] ≤ 109

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree