Find Closest to n and Divisible by m
Last Updated :
09 Jul, 2025
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.
[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.
C++
#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;
}
C
#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;
}
Java
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));
}
}
Python
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))
C#
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));
}
}
JavaScript
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));
[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 of m that is less than or equal to n.n2 = m * (q + 1) or m * (q - 1)
We choose q + 1 or q - 1 based on the signs of n and m:- If
n and m have the same sign, use n2 = m * (q + 1)
This moves in the direction toward n, getting the next closest multiple above n. - If
n and m have opposite signs, use n2 = m * (q - 1)
This accounts for the fact that increasing q would move away from n due to the sign flip, so we instead go backward to get the next closest multiple.
Then we return the one (n1 or n2) that has the smaller absolute difference from n.
- If both have the same distance from
n, return the one with the greater absolute value, as required.
C++
#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;
}
C
#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;
}
Java
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));
}
}
Python
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))
C#
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));
}
}
JavaScript
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));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem