Given non-zero two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.
 
Example 1:
Input:
N = 13 , M = 4
Output:
12
Explanation:
12 is the Closest Number to
13 which is divisible by 4.
Example 2:
Input:
N = -15 , M = 6
Output:
-18
Explanation:
-12 and -18 are both similarly close to
-15 and divisible by 6. but -18 has
the maximum absolute value.
So, Output is -18
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function closestNumber() which takes an Integer n as input and returns the answer.
 
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
 
Constraints:
-10^{5} <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def closestNumber(self, N , M):

        # code here 

Solutions:
class Solution:

	def closestNumber(self, N, M):
		return round(N / float(M)) * M


class Solution:

	def closestNumber(self, N, M):
		if abs(N / M - int(N / M)) < 0.5:
			return int(N / M) * M
		else:
			if N / M < 0:
				return int(N / M - 1) * M
			return int(N / M + 1) * M


import math
import numpy

class Solution:

	def closestNumber(self, N, M):
		d = N / M
		b = int(round(d))
		return b * M


class Solution:

	def closestNumber(self, N, M):
		x = int(N / M)
		n1 = x * M
		if N * M > 0:
			n2 = M * (x + 1)
		else:
			n2 = M * (x - 1)
		if abs(N - n1) < abs(N - n2):
			return n1
		else:
			return n2


class Solution:

	def closestNumber(self, n, m):
		q = int(n / m)
		n1 = m * q
		if n * m > 0:
			n2 = m * (q + 1)
		else:
			n2 = m * (q - 1)
		if abs(n - n1) < abs(n - n2):
			return n1
		return n2


class Solution:

	def closestNumber(self, N, M):
		rem = N % M
		rem = min(abs(rem), abs(M - rem))
		if (N + rem) % M == 0 and (N - rem) % M == 0:
			return N + rem if abs(N + rem) > abs(N - rem) else N - rem
		elif (N - rem) % M == 0:
			return N - rem
		else:
			return N + rem


class Solution:

	def closestNumber(self, N, M):
		if N > 0:
			return round(N / M) * M
		elif N < 0:
			k = abs(N) / abs(M)
			if k - int(abs(N) / abs(M)) < 0.5:
				return int(k) * abs(M) * -1
			else:
				return (int(k) + 1) * abs(M) * -1


class Solution:

	def closestNumber(self, N, M):
		q = round(N / M)
		n1 = M * q
		if N * M > 0:
			n2 = M * (q + 1)
		else:
			n2 = M * (q - 1)
		if abs(N - n1) < abs(N - n2):
			return n1
		return n2


class Solution:

	def closestNumber(self, N, M):
		q = N / M
		pq = N // M
		if q >= pq + 0.5:
			res = (pq + 1) * M
		else:
			res = pq * M
		return res


class Solution:

	def closestNumber(self, N, M):
		q = N // M
		x = q * M
		y = (q + 1) * M
		if abs(N - x) < abs(N - y):
			return x
		else:
			return y


class Solution:

	def closestNumber(self, N, M):
		quotient = N // M
		num1 = M * quotient
		num2 = M * (quotient + 1)
		if abs(N - num1) < abs(N - num2):
			return num1
		elif abs(N - num1) > abs(N - num2):
			return num2
		else:
			return max(num1, num2, key=abs)


class Solution:

	def closestNumber(self, N, M):
		M = abs(M)
		u = N + M - N % M
		d = N - N % M
		udiff = u - N
		ddiff = N - d
		if udiff == ddiff:
			absu = abs(u)
			absd = abs(d)
			if absu > absd:
				return u
			else:
				return d
		if udiff > ddiff:
			return d
		else:
			return u


class Solution:

	def closestNumber(self, N, M):
		r = N // M
		lower = r * M
		upper = (r + 1) * M
		if abs(N - lower) < abs(N - upper):
			return lower
		else:
			return upper


from math import ceil, floor

class Solution:

	def closestNumber(self, N, M):
		num = N / M
		up = ceil(num)
		down = floor(num)
		if abs(up - num) < abs(down - num):
			return up * M
		if abs(up - num) > abs(down - num):
			return down * M
		return up * M if abs(up) > abs(down) else down * M


import math

class Solution:

	def closestNumber(self, N, M):
		r = N % M
		r1 = N - r
		r2 = N + (M - r)
		if math.fabs(N - r1) < math.fabs(r2 - N):
			return r1
		return r2


class Solution:

	def closestNumber(self, N, M):
		a = round(N / M)
		return a * M


class Solution:

	def closestNumber(self, N, M):
		if N > 0 and M > 0:
			q = N // M
			cl1 = M * q
			cl2 = M * (q + 1)
			if N - cl1 < cl2 - N:
				return cl1
			else:
				return cl2
		elif N > 0 and M < 0:
			q = N // abs(M)
			cl1 = abs(M) * q
			cl2 = abs(M) * (q + 1)
			if N - cl1 < cl2 - N:
				return cl1
			else:
				return cl2
		elif N < 0 and M > 0:
			q = abs(N) // M
			cl1 = M * q
			cl2 = M * (q + 1)
			if abs(N) - cl1 < cl2 - abs(N):
				return -cl1
			else:
				return -cl2
		else:
			q = abs(N) // abs(M)
			cl1 = abs(M) * q
			cl2 = abs(M) * (q + 1)
			if abs(N) - cl1 < cl2 - abs(N):
				return -cl1
			else:
				return -cl2


class Solution:

	def closestNumber(self, N, M):
		quotient = N // M
		first_multiple = M * quotient
		second_multiple = M * (quotient + 1)
		if abs(N - first_multiple) < abs(N - second_multiple):
			return first_multiple
		elif abs(N - first_multiple) > abs(N - second_multiple):
			return second_multiple
		else:
			return max(first_multiple, second_multiple, key=abs)


class Solution:

	def closestNumber(self, N, M):
		q = N // M
		l1 = M * q
		l2 = M * (q + 1)
		d1 = abs(l1 - N)
		d2 = abs(l2 - N)
		if d1 < d2:
			return l1
		elif d1 > d2:
			return l2
		elif d1 == d2:
			if abs(l1) > abs(l2):
				return l1
			else:
				return l2


class Solution:

	def closestNumber(self, N, M):
		m = abs(M)
		n = abs(N)
		rem = n % m
		if rem >= m / 2:
			n = n + (m - rem)
			if N > 0:
				return n
			else:
				return -1 * n
		else:
			n = n - rem
			if N > 0:
				return n
			else:
				return -1 * n


class Solution:

	def closestNumber(self, N, M):
		if N % M == 0:
			return N
		elif N > 0:
			if M > 0:
				n = N / M
				if N % M > M / 2:
					return int(M * int(n + 1))
				else:
					return int(M * int(n))
			else:
				n = N / (M * -1)
				if N % M > M / 2:
					return int(M * int(n + 1) * -1)
				else:
					return int(M * int(n) * -1)
		elif M > 0:
			n = -1 * N / M
			if N % M < M / 2:
				return int(-1 * M * int(n + 1))
			else:
				return int(-1 * M * int(n))
		else:
			n = N / M
			if N % M <= M / 2:
				return int(M * int(n + 1))
			else:
				return int(M * int(n))


class Solution:

	def closestNumber(self, N, M):
		if N == 0:
			return 0
		if N % M == 0:
			return N
		if M > 0:
			if N > 0:
				rem = N % M
				No1 = N + (M - rem)
				No2 = N - rem
				if abs(No1 - N) < abs(N - No2):
					return No1
				else:
					return No2
			else:
				N = -N
				rem = N % M
				No1 = N + (M - rem)
				No2 = N - rem
				if abs(No1 - N) > abs(N - No2):
					return -No2
				else:
					return -No1
		else:
			M = -M
			if N > 0:
				rem = N % M
				No1 = N + (M - rem)
				No2 = N - rem
				if abs(No1 - N) < abs(N - No2):
					return No1
				else:
					return No2
			else:
				N = -N
				rem = N % M
				No1 = N + (M - rem)
				No2 = N - rem
				if abs(No1 - N) > abs(N - No2):
					return -No2
				else:
					return -No1


class Solution:

	def closestNumber(self, N, M):
		con = N
		N = abs(N)
		M = abs(M)
		c = N % M
		if c < M / 2:
			if con < 0:
				return -1 * (N - c)
			return N - c
		else:
			if con < 0:
				return -1 * (N + M - c)
			return N + M - c


class Solution:

	def closestNumber(self, N, M):
		N1 = abs(N)
		M1 = abs(M)
		v = N1 // M1
		v1 = v + 1
		if N1 % M1 == 0:
			return N
		v2 = N1 - v * M1
		v3 = v1 * M1 - N1
		if v2 > v3:
			if N < 0:
				return -(v1 * M1)
			else:
				return v1 * M1
		elif v2 < v3:
			if N < 0:
				return -(v * M1)
			else:
				return v * M1
		elif N < 0:
			return -max(abs(v1 * M1), abs(v * M1))
		else:
			return max(abs(v1 * M1), abs(v * M1))


class Solution:

	def closestNumber(self, N, M):
		r = N % M
		a = N // M
		d = a * M
		ed = a * M + M
		if abs(abs(N) - abs(d)) < abs(abs(ed) - abs(N)):
			return d
		else:
			return ed


import math

class Solution:

	def closestNumber(self, N, M):
		num = N / M
		h = M * math.floor(num)
		l = M * math.ceil(num)
		if abs(h - N) == abs(N - l) and N < 0:
			return min(h, l)
		elif abs(h - N) == abs(N - l) and N > 0:
			return max(h, l)
		elif abs(h - N) > abs(N - l):
			return l
		else:
			return h


class Solution:

	def closestNumber(self, N, M):
		fac = round(N / M)
		if fac == 0:
			return 0 if abs(0 - N) < abs(M - N) else M
		return M * fac


class Solution:

	def closestNumber(self, N, M):
		(Q, R) = divmod(N, M)
		lowervalue = Q * M
		heigervalue = (Q + 1) * M
		if abs(N - lowervalue) < abs(N - heigervalue):
			return lowervalue
		else:
			return heigervalue


import math

class Solution:

	def closestNumber(self, N, M):

		def myround(num):
			if num > 1:
				decimals = num % math.floor(num)
			else:
				decimals = num
			if decimals >= 0.5:
				num = math.ceil(num)
			else:
				num = math.floor(num)
			return num
		a = N / M
		if a < 0:
			a = myround(abs(a))
			out = -a * M
		else:
			a = myround(a)
			out = a * M
		return out


class Solution:

	def closestNumber(self, N, M):
		a = 0
		nu = N
		N = abs(N)
		M = abs(M)
		n = int(N % M)
		if n < M / 2:
			a = int(N / M)
		else:
			a = int(N / M)
			a = a + 1
		if nu < 0:
			return -1 * (M * a)
		else:
			return M * a


class Solution:

	def closestNumber(self, N, M):
		y = N // M
		l_c = M * y
		h_c = (y + 1) * M
		if abs(N - l_c) < abs(h_c - N):
			return l_c
		else:
			return h_c


class Solution:

	def closestNumber(self, N, M):
		div = N // M
		a = div * M
		b = (div + 1) * M
		if abs(N - a) > abs(N - b):
			return b
		elif abs(N - a) == abs(N - b):
			if abs(b) > abs(a):
				return b
			else:
				return a
		else:
			return a


class Solution:

	def closestNumber(self, N, M):
		self.num = N
		self.div = M
		if self.num > 0:
			if self.div < 0:
				self.div = -self.div
			val = self.num / self.div
			if val >= int(val) + 0.5:
				val = int(val) + 1
			else:
				val = int(val)
			get = val * self.div
			return get
		else:
			if self.div < 0:
				self.div = -self.div
			val = self.num / self.div
			if val <= int(val) - 0.5:
				val = int(val) - 1
			else:
				val = int(val)
			get = val * self.div
			return get


class Solution:

	def closestNumber(self, N, M):
		quotient = N // M
		tempLower = quotient * M
		tempHigher = (quotient + 1) * M
		if tempLower % M == 0 and tempHigher % M == 0:
			if abs(N - tempLower) < abs(N - tempHigher):
				return tempLower
			else:
				return tempHigher
		return N


class Solution:

	def closestNumber(self, N, M):
		q = N / float(M)
		if (q + 0.5).is_integer():
			if abs(q) == q:
				return int((q + 0.5) * M)
			else:
				return int((q - 0.5) * M)
		else:
			return round(q) * M