Skip to main content

Closest Number

Closest Number

Basic Accuracy: 11.21% Submissions: 1615 Points: 1

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

 

=>   *****************SOLUTION **************************


// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends


class Solution {
  public:
    int closestNumber(int N , int M) {
        int closestNum;
        float division=(float)N/M;
        int main=N/M;
        float campare;

         if(division<0){
             
               campare=main-0.5;

               if(division<=campare){
                main=main-1;
                closestNum=main*M;
                }else{
                 closestNum=main*M;
                }
             
         }else{
                 campare=main+0.5;
              
                 if(division>=campare){
                 main=main+1;
                 closestNum=main*M;
                 }else{
                 closestNum=main*M;
                 }
         
         }
         
       
       return closestNum;
    }
};

// { Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        int N,M;
        
        cin>>N>>M;

        Solution ob;
        cout << ob.closestNumber(N,M) << endl;
    }
    return 0;
} 

Comments