Given a string  N representing a positive number. The task is to round N to the nearest multiple of 10.
Note: If you are having two multiple equally apart from N then we will choose the smallest element among them.
 
Example 1:
 
Input : N = 15
Output : 10
Explanation:
Here N is 15. So, the number which is
multiple of 10 are 10, 20, 30. We can
see 10 and 20 are equally distant
from 20. So, we will choose the
smallest element among them i.e., 10.
Example 2:
Input : N = 29 
Output : 30 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function roundToNearest() that takes a string (N), and returns the nearest multiple of 10. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 <= |Number length| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def roundToNearest (self, N) : 
        #Complete the function


Solutions:
class Solution:

	def roundToNearest(self, N):
		N = int(N)
		if N % 10 <= 5:
			a = N // 10
			return a * 10
		else:
			return (N // 10 + 1) * 10


class Solution:

	def roundToNearest(self, N):
		n = int(N)
		b = n // 10 * 10
		c = b + 10
		if n - b > c - n:
			return c
		return b


class Solution:

	def roundToNearest(self, N):
		a = int(N) // 10 * 10
		b = a + 10
		if int(N) - a > b - int(N):
			return b
		else:
			return a


class Solution:

	def roundToNearest(self, N):
		k = int(N)
		r = k % 10
		if r > 5:
			return k - r + 10
		else:
			return k - r


class Solution:

	def roundToNearest(self, N):
		f = 1
		maxx = int(N)
		while f == 1:
			if maxx % 10 == 0:
				f = 0
			else:
				maxx += 1
		g = 1
		minn = int(N)
		while g == 1:
			if minn % 10 == 0:
				g = 0
			else:
				minn -= 1
		if abs(int(N) - maxx) < abs(int(N) - minn):
			return maxx
		return minn


class Solution:

	def roundToNearest(self, N):
		n = int(N)
		q = n // 10
		r = n % 10
		if r <= 5:
			return 10 * q
		else:
			return 10 * (q + 1)


class Solution:

	def roundToNearest(self, n):
		n = int(n)
		z = n % 10
		if z <= 5:
			x = n - z
			return x
		else:
			p = 10 - z
			x = n + p
			return x


class Solution:

	def roundToNearest(self, N):
		a = int(N)
		for i in range(11):
			if a % 10 == 0:
				return str(a)
			b = a - i
			c = a + i
			if b % 10 == 0:
				return str(b)
			if c % 10 == 0:
				return str(c)
		return ''


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		val = N % 10
		if val == 10:
			return str(N)
		if val > 5:
			N += 10 - val
		else:
			N -= val
		return str(N)


class Solution:

	def roundToNearest(self, N):
		t = int(N)
		ans = ''
		if t % 10 == 5:
			t = t - 5
		elif t % 10 > 5:
			t = t + 10 - t % 10
		else:
			t = t - t % 10
		return str(t)


class Solution:

	def roundToNearest(self, N):
		count1 = 0
		count2 = 0
		n1 = int(N)
		if n1 <= 10:
			if n1 > 5:
				return '10'
			else:
				return '0'
		while n1 % 10 != 0:
			count1 += 1
			n1 += 1
		n2 = int(N)
		while n2 % 10 != 0:
			count2 += 1
			n2 -= 1
		if count1 < count2:
			return str(n1)
		elif count1 > count2:
			return str(n2)
		else:
			return str(n2)


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		if N < 10 and N > 5:
			return 10
		elif N <= 5:
			return 0
		elif N % 10 == 0:
			return N
		rem = N % 10
		if rem <= 5:
			return N - rem
		elif rem > 5:
			return N + 10 - rem


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		a = N % 10
		if a == 0:
			return N
		elif a <= 5:
			return N - int(a)
		else:
			return N + (10 - int(a))


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		if N % 10 > 5:
			n = 10 - N % 10
			N = n + N
		elif N % 10 < 5:
			n = N % 10
			N = N - n
		else:
			N = N - 5
		return N


class Solution:

	def roundToNearest(self, n):
		n = int(n)
		d = n // 10
		first = d * 10
		second = (d + 1) * 10
		if abs(n - first) <= abs(n - second):
			return str(first)
		else:
			return str(second)


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		n = N % 10
		if n < 6:
			return N - n
		return N + (10 - n)


class Solution:

	def roundToNearest(self, N):
		n = int(N)
		pn = abs(n)
		last_digit = pn % 10
		add = 0
		if 5 < last_digit <= 9:
			add = 10 - last_digit
		elif 1 <= last_digit <= 5:
			add = -last_digit
		if n > 0:
			return pn + add
		else:
			return -(pn + add)


class Solution:

	def roundToNearest(self, N):
		if len(N) == 1:
			if int(N) <= 5:
				return 0
			else:
				return 10
		elif int(N[-1]) <= 5:
			return N[0:-1] + '0'
		else:
			return str(int(N[0:-1]) + 1) + '0'


class Solution:

	def roundToNearest(self, N):
		rem = int(N[len(N) - 1])
		if rem > 5:
			rem = abs(10 - rem)
		else:
			rem = -rem
		temp = int(N)
		temp += rem
		return str(temp)


class Solution:

	def roundToNearest(self, N):
		que = int(N) // 10
		remainder = int(N) % 10
		if remainder <= 5:
			return 10 * que
		return 10 * (que + 1)


class Solution:

	def roundToNearest(self, N):
		if int(N) <= 5:
			return '0'
		if int(N) <= 9:
			return '10'
		n = int(N) % 10
		if n == 0:
			return str(N)
		elif n <= 5:
			return str(int(N) - n)
		else:
			return str(int(N) - n + 10)


class Solution:

	def roundToNearest(self, n):
		n = int(n)
		r = n % 10
		n1 = n - r
		if r <= 5:
			return n1
		else:
			return n1 + 10


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		if N % 10 <= 5:
			return 10 * (N // 10)
		elif N % 10 > 5:
			return 10 * (N // 10 + 1)


class Solution:

	def roundToNearest(self, N):

		def getNumber(N):
			n1 = int(N)
			return (n1, n1 % 10)
		if N[-1] <= '5':
			(k1, k2) = getNumber(N)
			return k1 - k2
		else:
			(k1, k2) = getNumber(N)
			return k1 + (10 - k2)


class Solution:

	def roundToNearest(self, N):
		last = int(N[-1])
		if last > 5:
			last = 10 - last
		else:
			last = -last
		res = int(N)
		res += last
		return str(res)


class Solution:

	def roundToNearest(self, N):
		re = int(N[len(N) - 1])
		if re > 5:
			re = abs(10 - re)
		else:
			re = -re
		t = int(N)
		t += re
		return str(t)


class Solution:

	def roundToNearest(self, N):
		a = str(N)
		las = a[-1]
		nea = int(las)
		if 10 - nea < 5:
			return str(int(N) + (10 - nea))
		else:
			ans = int(N) - nea
			return str(ans)


class Solution:

	def roundToNearest(self, N):
		n = int(N)
		low = n // 10 * 10
		high = low + 10
		if n - low > high - n:
			return high
		else:
			return low


class Solution:

	def roundToNearest(self, N):
		x = int(N)
		y = len(N)
		if x % 10 > 5:
			x = x + 10 - x % 10
		else:
			x = x - x % 10
		return str(x)


class Solution:

	def roundToNearest(self, N):
		x = int(N)
		if x % 10 <= 5:
			return x - x % 10
		else:
			return x + (10 - x % 10)


class Solution:

	def roundToNearest(self, N):
		s = int(N)
		k = s % 10
		l = s // 10
		if k <= 5:
			return l * 10
		else:
			return (l + 1) * 10


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		unit_n = N % 10
		ans = None
		if unit_n >= 0 and unit_n <= 5:
			ans = N - unit_n
		else:
			ans = N + (10 - unit_n)
		return ans


class Solution:

	def roundToNearest(self, N):
		s = int(N) // 10
		if int(N) - s * 10 <= (s + 1) * 10 - int(N):
			return s * 10
		else:
			return (s + 1) * 10


class Solution:

	def roundToNearest(self, N):
		ans = 0
		n = int(N)
		if n % 10 > 5:
			ans = (n // 10 + 1) * 10
		else:
			ans = n // 10 * 10
		return int(ans)


class Solution:

	def roundToNearest(self, n):
		t = int(n) % 10
		j = abs(10 - t)
		if j < 5:
			a = str(int(n) + j)
			return a
		else:
			b = str(int(n) - t)
			return b


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		a1 = N // 10
		a2 = (a1 + 1) * 10
		if abs(N - a1 * 10) == abs(N - a2):
			return a1 * 10
		if abs(N - a1 * 10) > abs(N - a2):
			return a2
		else:
			return a1 * 10


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		l = int(N)
		m = int(N)
		while l % 10:
			l -= 1
		while m % 10:
			m += 1
		if m % 10 == 0 and l % 10 == 0:
			if N - l > m - N:
				return m
			return l
		if m % 10 == 0:
			return m
		return l


class Solution:

	def roundToNearest(self, N):
		k = int(N) % 10
		if k <= 5:
			return int(N) - k
		return int(N) + abs(k - 10)


class Solution:

	def roundToNearest(self, N):
		i = int(N)
		N = int(N)
		while i > 0:
			r = N % 10
			if r == 0:
				N = N
			elif r <= 5:
				N = N - r
			else:
				N = N + 5
			i = N % 10
		return N


class Solution:

	def roundToNearest(self, N):
		N = int(N)
		c = N % 10
		if c <= 10 - c:
			return N - c
		return N + (10 - c)


class Solution:

	def roundToNearest(self, N):
		l = int(N[-1])
		x = ''
		if l in [1, 2, 3, 4]:
			x = N[:-1] + '0'
		elif l in [6, 7, 8, 9]:
			x = int(N) + (10 - l)
		elif l == 0:
			x = N
		elif l == 5:
			x = int(N) - 5
		return x


class Solution:

	def roundToNearest(self, N):
		temp = int(N)
		l = temp % 10
		if l <= 5:
			temp -= l
		else:
			temp += 10 - l
		ans = ''
		ans = temp
		return ans