Given two integers n and m (m != 0). Find the number closest to n and divisible by m. If there is more than one such number, then output the one having maximum absolute value.
Examples:
Input: n = 13, m = 4
Output: 12
Explanation: 12 is the closest to 13, divisible by 4.Input: n = -15, m = 6
Output: -18
Explanation: Both -12 and -18 are closest to -15, but -18 has the maximum absolute value.
Try it on GfG Practice
Table of Content
[Naive Approach] Iterative Checking - O(m) Time and O(1) Space
The basic idea is to start checking from n - m to n + m one by one and take the closest number.
#include <iostream>
#include <climits>
using namespace std;
int closestNumber(int n, int m) {
int closest = 0;
int minDifference = INT_MAX;
// Check numbers around n
for (int i = n - abs(m); i <= n + abs(m); ++i) {
if (i % m == 0) {
int difference = abs(n - i);
if (difference < minDifference ||
(difference == minDifference && abs(i) > abs(closest))) {
closest = i;
minDifference = difference;
}
}
}
return closest;
}
int main() {
int n = 13, m = 4;
cout << closestNumber(n, m) << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int closestNumber(int n, int m) {
// find the quotient
int closest = 0;
int minDifference = INT_MAX;
// Check numbers around n
for (int i = n - abs(m); i <= n + abs(m); ++i) {
if (i % m == 0) {
int difference = abs(n - i);
if (difference < minDifference ||
(difference == minDifference && abs(i) > abs(closest))) {
closest = i;
minDifference = difference;
}
}
}
return closest;
}
int main() {
int n = 13, m = 4;
printf("%d\n", closestNumber(n, m));
return 0;
}
class Main {
static int closestNumber(int n, int m) {
// find the quotient
int closest = 0;
int minDifference = Integer.MAX_VALUE;
// Check numbers around n
for (int i = n - Math.abs(m); i <= n + Math.abs(m); ++i) {
if (i % m == 0) {
int difference = Math.abs(n - i);
if (difference < minDifference ||
(difference == minDifference && Math.abs(i) > Math.abs(closest))) {
closest = i;
minDifference = difference;
}
}
}
return closest;
}
public static void main(String[] args) {
int n = 13, m = 4;
System.out.println(closestNumber(n, m));
}
}
def closest_number(n, m):
# find the quotient
closest = 0
min_difference = float('inf')
# Check numbers around n
for i in range(n - abs(m), n + abs(m) + 1):
if i % m == 0:
difference = abs(n - i)
if difference < min_difference or \
(difference == min_difference and abs(i) > abs(closest)):
closest = i
min_difference = difference
return closest
if __name__ == "__main__":
n = 13
m = 4
print(closest_number(n, m))
using System;
class GfG {
static int ClosestNumber(int n, int m) {
// find the quotient
int closest = 0;
int minDifference = int.MaxValue;
// Check numbers around n
for (int i = n - Math.Abs(m); i <= n + Math.Abs(m); ++i) {
if (i % m == 0) {
int difference = Math.Abs(n - i);
if (difference < minDifference ||
(difference == minDifference && Math.Abs(i) > Math.Abs(closest))) {
closest = i;
minDifference = difference;
}
}
}
return closest;
}
static void Main() {
int n = 13, m = 4;
Console.WriteLine(ClosestNumber(n, m));
}
}
function closestNumber(n, m) {
// find the quotient
let closest = 0;
let minDifference = Infinity;
// Check numbers around n
for (let i = n - Math.abs(m); i <= n + Math.abs(m); i++) {
if (i % m === 0) {
let difference = Math.abs(n - i);
if (difference < minDifference ||
(difference === minDifference && Math.abs(i) > Math.abs(closest))) {
closest = i;
minDifference = difference;
}
}
}
return closest;
}
//driver code
let n = 13, m = 4;
console.log(closestNumber(n, m));
Output
12
[Expected Approach] By finding Quotient - O(1) Time and O(1) Space
We first compute the quotient
q = n / m, then calculate two candidates:
n1 = m * q
This is the closest multiple ofmthat is less than or equal ton.n2 = m * (q + 1)orm * (q - 1)
We chooseq + 1orq - 1based on the signs ofnandm:
- If
nandmhave the same sign, usen2 = m * (q + 1)
This moves in the direction towardn, getting the next closest multiple aboven.- If
nandmhave opposite signs, usen2 = m * (q - 1)
This accounts for the fact that increasingqwould move away fromndue to the sign flip, so we instead go backward to get the next closest multiple.Then we return the one (
n1orn2) that has the smaller absolute difference fromn.
- If both have the same distance from
n, return the one with the greater absolute value, as required.
#include <iostream>
#include <climits>
using namespace std;
int closestNumber(int n, int m) {
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if (abs(n - n1) < abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
int main() {
int n = 13, m = 4;
cout << closestNumber(n, m) << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int closestNumber(int n, int m) {
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if (abs(n - n1) < abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
int main() {
int n = 13, m = 4;
printf("%d\n", closestNumber(n, m));
return 0;
}
class GfG {
static int closestNumber(int n, int m) {
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
public static void main(String args[]) {
int n = 13, m = 4;
System.out.println(closestNumber(n, m));
}
}
def closestNumber(n, m) :
q = int(n / m)
# 1st possible closest number
n1 = m * q
# 2nd possible closest number
if((n * m) > 0) :
n2 = (m * (q + 1))
else :
n2 = (m * (q - 1))
# if true, then n1 is the required closest number
if (abs(n - n1) < abs(n - n2)) :
return n1
# else n2 is the required closest number
return n2
if __name__ == "__main__":
n = 13; m = 4
print(closestNumber(n, m))
using System;
class GfG {
static int closestNumber(int n, int m) {
int q = n / m;
// 1st possible closest number
int n1 = m * q;
// 2nd possible closest number
int n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the required closest number
if (Math.Abs(n - n1) < Math.Abs(n - n2))
return n1;
// else n2 is the required closest number
return n2;
}
public static void Main() {
int n = 13, m = 4;
Console.WriteLine(closestNumber(n, m));
}
}
function closestNumber(n, m) {
let q = parseInt(n / m);
// 1st possible closest number
let n1 = m * q;
// 2nd possible closest number
let n2 = (n * m) > 0 ?
(m * (q + 1)) : (m * (q - 1));
// if true, then n1 is the
// required closest number
if (Math.abs(n - n1) < Math.abs(n - n2))
return n1;
// else n2 is the required
// closest number
return n2;
}
// Driver Code
let n = 13;
let m = 4;
console.log(closestNumber(n, m));
Output
12