Closest Number | Practice | GeeksforGeeks

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:
-105 <= N <= 105
0
danieldevil7320012 weeks ago
// 6 lines easiest c++ code#include <bits/stdc++.h>using namespace std;class Solution { public:   int closestNumber(int N , int M) {        int i=1;      while(1){           if(N%M == 0){              return N;           }           else if(i%2 ==0){               N+=i;           }           else {          N-=i;           }           i++;      }   }};
0
gupta2411sumit2 months ago
Easy to understand C++ solution , crystal clear 
int closestNumber(int N , int M) {       // code here              int ans1 = N ;       int ans2 = N ;       while(ans1%M!=0)       {           ans1-- ;       }              while(ans2%M!=0)       {           ans2++ ;       }                     int d1 = N-ans1 ;       int d2 = ans2 - N ;              if(d1==d2)       {           if( abs(ans1)> abs(ans2)){               return ans1 ;           }           else{               return ans2 ;           }       }             if(d1>d2)      {          return ans2 ;      }      else{          return ans1 ;      }   }};
0
indiakamanthan2 months ago
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)
0
jhav16432 months ago
int closestNumber(int N , int M) {       double n = N, m = M;       double temp = n/m;              int x = round(temp);              if(x==0 && abs(N)>abs(M))           return M;              else if(x == 0 && abs(N)<abs(M))           return 0;           
+1
paramveerkumar41693 months ago
c++ solution:-
 int closestNumber(int N , int M) {          int Q=N/M;//first find quotient             int N1 = M* Q;//1st possible closest number      int N2;      if((N*M)>0)//       N2=M*(Q+1);      else      N2=M*(Q-1);            if(abs(N-N1)<abs(N-N2))      return N1;      return N2;           }   
 
+1
tannakrishi11093 months ago
JavaScript Solution : -
closestNumber(N,M){     var sign = 1;     var i = 0;          if (N<0)     {         N = N*-1;         sign = -1;     }     if (M<0)     {         M = M*-1;     }          while (i!==M)     {         if ((N+i)%M === 0)         {             return (N+i)*sign;         }         else if ((N-i)%M === 0)         {             return (N-i)*sign;         }         i++;     }     return 1;   }
0
mgkr3214 months ago
C++ Solution:
 
   int closestNumber(int N , int M)   {      int sign = 1;      int i = 0;            if (N<0)      {          N = N*-1;          sign = -1;      }      if (M<0)      {          M = M*-1;      }            while (!(i==M))      {          if ((N+i)%M == 0)          {              return (N+i)*sign;          }          else if ((N-i)%M == 0)          {              return (N-i)*sign;          }          i++;      }      return 1;   }
0
yashvarshney984 months ago
class Solution {   static int closestNumber(int N , int M) {       int n=N;       int m=M;       if (N<0){           N=N*-1;       }if(M<0){           M=M*-1;       }              int rem=N%M;       int ques=N/M;              int num1=N-rem;       int num2=(ques+1)*M;              if(num2-N==N-num1){           if (n==N && m==M){               return (num2);           }           else if(n==N && m!=M){               return (num2);           }           else{               return(num2*-1);           }       }       else if(num2-N>N-num1){           if (n==N && m==M){               return (num1);           }           else if(n==N && m!=M){               return (num1);           }           else{               return(num1*-1);           }       }       else{           if (n==N && m==M){               return (num2);           }           else if(n==N && m!=M){               return (num2);           }           else{               return(num2*-1);           }       }   }
0
azharsubair35 months ago
This Correct output is so Incorrect.
 
Input: 
3103 27891
Its Correct output is: 
0
0
kumarviju785
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.