Given an integer N the task is to find the largest number which is smaller or equal to it and has its digits in non-decreasing order.
 
Examples 1:
Input:
N = 200
Output:
199
Explanation:
If the given number 
is 200, the largest 
number which is smaller 
or equal to it having 
digits in non decreasing 
order is 199.
Example 2:
Input: 
N = 139
Output:
139
Explanation:
139 is itself in 
non-decresing order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function find() which takes one integer value N, as input parameter and return the largest number which is smaller or equal to it and has its digits in non-decreasing order.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
1<=N<=10^{5}

Difficulty: MEDIUM
Tags: Mathematics
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

#Back-end complete function Template for Python 3

class Solution:
    def find (self, N):
        # code here
        

Solutions:
class Solution:

	def find(self, N):

		def NDO(n):
			n = str(n)
			for i in range(len(n) - 1):
				if int(n[i]) > int(n[i + 1]):
					return False
			return True
		while True:
			if NDO(N):
				return N
			N -= 1


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			k = list(str(i))
			a = k[:]
			a.sort()
			if a == k:
				return i
		return 0


class Solution:

	def find(self, N):
		for i in range(N, 1, -1):
			n = str(i)
			a = [j for j in n]
			a.sort()
			b = ''.join(a)
			if int(b) == i:
				return i


class Solution:

	def ok(self, n):
		s = str(n)
		for i in range(1, len(s)):
			if s[i] < s[i - 1]:
				return False
		return True

	def find(self, N):
		while True:
			if self.ok(N):
				return N
			N -= 1
		return -1


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			if len(str(i)) == 1:
				return N
			else:
				numStr = list(str(i))
				numStr.sort()
				digit = ''.join(numStr)
				if int(digit) == i:
					return i


class Solution:

	def find(self, N):
		while N > 0:
			p = str(N)
			i = 0
			while i < len(p) - 1:
				if p[i] <= p[i + 1]:
					i = i + 1
				else:
					break
			if i == len(p) - 1:
				return N
			else:
				N = N - 1


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			temp_str = str(i)
			cond = True
			for j in range(len(temp_str) - 1):
				if temp_str[j] > temp_str[j + 1]:
					cond = False
					break
			if cond:
				return i


class Solution:

	def find(self, N):
		for i in range(N, 1, -1):
			k = str(i)
			x = [j for j in k]
			x.sort()
			y = ''.join(x)
			if int(y) == i:
				return i


class Solution:

	def find(self, N):
		for i in range(N, 1, -1):
			n = ''.join(sorted(list(str(i))))
			if int(n) == i:
				return i


class Solution:

	def find(self, n):
		while n:
			k = sorted(str(n))
			if str(n) == ''.join(k):
				return n
			n -= 1


class Solution:

	def find(self, N):
		while N:
			k = str(N)
			l = list(k)
			l.sort()
			l = ''.join(l)
			if l == k:
				return N
			N -= 1


class Solution:

	def find(self, N):
		while True:
			temp = N
			prev = 10
			while temp:
				if temp % 10 > prev:
					break
				prev = temp % 10
				temp //= 10
			if temp == 0:
				return N
			N -= 1
		return 0


class Solution:

	def find(self, N):
		if list(str(N)) == sorted(list(str(N))):
			return N
		else:
			l = str(self.find(int(str(N // 10 - 1)))) + '9'
			return int(l)


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			s = str(i)
			f = 0
			for j in range(len(s) - 1):
				if s[j] <= s[j + 1]:
					continue
				else:
					f = 1
					break
			if f == 0:
				return i
		return -1


class Solution:

	def find(self, N):

		def helper(x):
			x = str(x)
			if [i for i in x] == sorted(x):
				return True
			return False
		for i in range(N, -1, -1):
			if helper(i):
				return i


class Solution:

	def find(self, N):
		for num in range(N, -1, -1):
			if len(str(num)) == 1:
				return N
			else:
				numString = list(str(num))
				numString.sort()
				digit = ''.join(numString)
				if int(digit) == num:
					return num


class Solution:

	def find(self, N):
		for num in range(N, 0, -1):
			st = str(num)
			temp = list(st)
			don = list(st)
			don.sort()
			if don == temp:
				return num


class Solution:

	def find(self, N):
		for i in range(N, 0, -1):
			x = str(i)
			a = 0
			m = 0
			while m < len(x) - 1:
				if x[m] > x[m + 1]:
					a = 1
					break
				m = m + 1
			if a == 0:
				return int(x)


class Solution:

	def find(self, N):
		S = str(N)
		l = []
		for i in S:
			l.append(int(i))
		for i in range(len(l) - 1, 0, -1):
			if l[i - 1] > l[i]:
				l[i] = 9
				l[i - 1] -= 1
		for i in range(0, len(l) - 1):
			if l[i + 1] < l[i]:
				l[i + 1] = 9
		s = ''
		for i in l:
			s += str(i)
		return int(s)


class Solution:

	def find(self, N):
		for i in range(N, 0, -1):
			if list(str(i)) == sorted(list(str(i))):
				return i


class Solution:

	def find(self, N):
		for i in range(N, 0, -1):
			x = str(i)
			temp = x
			temp = list(temp)
			temp.sort()
			c = ''.join(temp)
			if x == c:
				return int(x)


class Solution:

	def find(self, N):
		x = list(str(N))
		y = list(str(N))
		y.sort()
		if y == x:
			return N
		else:
			while N > 0:
				i = list(str(N))
				j = list(str(N))
				j.sort()
				if i == j:
					return N
				N = N - 1


class Solution:

	def find(self, N):
		if int(''.join(sorted(str(N)))) == N:
			return N
		n = 0
		while n != N:
			N = N - 1
			n = int(''.join(sorted(str(N))))
		return n


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			temp = sorted([x for x in str(i)])
			if temp == [x for x in str(i)]:
				return i


class Solution:

	def find(self, N):
		for i in range(0, N):
			lst = []
			m = N - i
			m = str(m)
			for j in range(0, len(m)):
				lst.append(m[j])
			if lst == sorted(lst):
				return int(m)


class Solution:

	def check_for_non_decreasing_digits(self, a):
		for i in range(0, len(a) - 1):
			if a[i] < a[i + 1]:
				return False
		return True

	def find(self, N):
		i = 0
		while i < N:
			answer = N - i
			digit_array = []
			while answer > 0:
				digit_array.append(answer % 10)
				answer = int(answer / 10)
			if self.check_for_non_decreasing_digits(digit_array):
				return N - i
			else:
				i = i + 1


def check(s):
	s = str(s)
	for i in range(1, len(s)):
		if int(s[i - 1]) > int(s[i]):
			return False
	return True

class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			if check(i):
				return i


class Solution:

	def find(self, N):
		for y in range(N, -1, -1):
			if len(str(y)) == 1:
				return N
			else:
				sas = list(str(y))
				sas.sort()
				digit = ''.join(sas)
				if int(digit) == y:
					return y


class Solution:

	def find(self, N):
		m = N
		while N > 10:
			a = m % 10
			m //= 10
			while a >= m % 10 and m != 0:
				a = m % 10
				m //= 10
			if m == 0:
				return N
			N -= 1
			m = N
		return N


class Solution:

	def find(self, n):
		if len(str(n)) == 1:
			return n
		else:
			while n > 9:
				s1 = str(n)
				s2 = sorted(s1)
				s2 = ''.join(s2)
				if s1 == s2:
					return n
				n -= 1
			return n


class Solution:

	def find(self, N):
		s = str(N)
		t = ''
		for i in range(N, -1, -1):
			x = i
			p = str(x)
			y = sorted(p)
			for j in range(len(y)):
				t += y[j]
			if t == p:
				return p
				break
			t = ''
		return -1


class Solution:

	def find(self, N):
		for i in range(N, -1, -1):
			count = 0
			N1 = str(i)
			for j in range(0, len(N1) - 1):
				if int(N1[j]) < int(N1[j + 1]) or int(N1[j]) == int(N1[j + 1]):
					count = count + 1
			if count == len(N1) - 1:
				return N1
				break


class Solution:

	def find(self, N):
		n = N
		if n <= 9:
			return n
		while n >= 10:
			if self.check(n):
				return n
			n -= 1
		return -1

	def check(self, n):
		temp = str(n)
		for i in range(0, len(temp) - 1):
			if temp[i] > temp[i + 1]:
				return False
		return True


class Solution:

	def find(self, N):
		x = 0
		for x in range(N, 0, -1):
			no = x
			prev_dig = 11
			flag = True
			while no != 0:
				if prev_dig < no % 10:
					flag = False
					break
				prev_dig = no % 10
				no //= 10
			if flag == True:
				break
		return x


class Solution:

	def find(self, N):
		while N > 0:
			res = list(map(int, str(N)))
			res.sort()
			temp = int(''.join(map(str, res)))
			if temp == N:
				return N
				break
			N -= 1