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: 1
Explanation: The given array is sorted.
Example 2:
Input:
N = 6
arr[] = {90, 80, 100, 70, 40, 30}
Output: 0
Explanation: The given array is not sorted.
Your Task:
You don't need to read input or print anything. Your task is to complete the function arraySortedOrNot() which takes the arr[] and N as input parameters and returns a boolean value (true if it is sorted otherwise false).
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: #User function Template for python3



class Solution:

    def arraySortedOrNot(self, arr, n):

        # code here

Solutions:
class Solution:

	def arraySortedOrNot(self, arr, n):
		check = False
		for j in range(1, len(arr)):
			if arr[j] < arr[j - 1]:
				check = True
				return 0
		if check == False:
			return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		for i in range(1, n):
			if arr[i - 1] > arr[i]:
				return False
		return True


class Solution:

	def arraySortedOrNot(self, arr, n):
		i = 0
		while i < n - 1:
			if arr[i] > arr[i + 1]:
				return 0
			i += 1
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		x = sorted(arr)
		return arr == x


class Solution:

	def arraySortedOrNot(self, arr, n):
		s = sorted(arr)
		if s == arr:
			return 1
		else:
			return 0


class Solution:

	def arraySortedOrNot(self, arr, n):
		for i in range(0, n - 1):
			if arr[i + 1] < arr[i]:
				return 0
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		t = 0
		for i in range(n - 1):
			if arr[i] > arr[i + 1]:
				t = 1
				break
		if t == 1:
			return 0
		else:
			return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		arr1 = arr[:]
		arr1.sort()
		if arr == arr1:
			return 1
		else:
			return 0


class Solution:

	def arraySortedOrNot(self, a, n):
		for i in range(n - 1):
			if a[i + 1] >= a[i]:
				pass
			else:
				return 0
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		return True if arr == sorted(arr) else False


class Solution:

	def arraySortedOrNot(self, arr, n):
		isSorted = 1
		for i in range(1, len(arr)):
			if arr[i] < arr[i - 1]:
				isSorted = 0
				break
		return isSorted


class Solution:

	def arraySortedOrNot(self, arr, n):
		for i in range(1, len(arr)):
			if arr[i] >= arr[i - 1]:
				continue
			else:
				return 0
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		ans = sorted(arr)
		if arr == ans:
			return 1
		return 0


class Solution:

	def arraySortedOrNot(self, arr, n):
		b = sorted(arr)
		cnt = 0
		for i in range(0, len(arr)):
			if arr[i] != b[i]:
				cnt += 1
		if cnt == 0:
			return 1
		else:
			return 0


class Solution:

	def arraySortedOrNot(self, arr, n):
		l = 0
		r = 1
		while r < len(arr):
			if arr[r] < arr[l]:
				return 0
			r = r + 1
			l += 1
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		count = 0
		for i in range(n - 1):
			if arr[i] < arr[i + 1] or arr[i] == arr[i + 1]:
				count += 1
		if count == n - 1:
			return 1
		else:
			return 0


class Solution:

	def isSorted(self, arr, n):
		if n <= 1:
			return True
		return arr[n] >= arr[n - 1] and self.isSorted(arr, n - 1)

	def arraySortedOrNot(self, arr, n):
		if n == 0 or n == 1:
			return True
		for i in range(1, n):
			if arr[i - 1] > arr[i]:
				return False
		return True


class Solution:

	def arraySortedOrNot(self, arr, n):

		def isSorted(arr, left, right):
			if left == right:
				return True
			mid = (left + right) // 2
			leftSorted = isSorted(arr, left, mid)
			rightSorted = isSorted(arr, mid + 1, right)
			currentSorted = arr[mid + 1] >= arr[mid]
			return leftSorted and rightSorted and currentSorted
		return isSorted(arr, 0, n - 1)


class Solution:

	def arraySortedOrNot(self, arr, n):
		if n == 1:
			return 1
		sorted = 0
		for i in range(n - 1):
			if arr[i] <= arr[i + 1]:
				pass
			else:
				return 0
		return 1


class Solution:

	def arraySortedOrNot(self, arr, n):
		flag = True
		for i in range(1, n):
			if arr[i - 1] <= arr[i]:
				continue
			else:
				flag = False
				break
		return flag