We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Mathematics
  3. Number Theory
  4. Closest Number
  5. Discussions

Closest Number

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1387 Discussions, By:

votes

Please Login in order to post a comment

  • Trondbert
    6 years ago+ 13 comments

    I think this one belongs in the easy category.

    16|
    Permalink
    View more Comments..
  • [deleted] 6 years ago+ 16 comments

    Proud to say my code is shorter than the Editorials:

    #include <cmath>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int t; cin >> t;
    
        for (int i = 0; i < t; i++) {
            int a,b,c; cin >> a >> b >> c;
    
            unsigned long long d = pow(a,b) + c / 2;
            cout << d - (d % c) << endl;
        }
        return 0;
    }
    
    7|
    Permalink
    View more Comments..
  • nadifnisfnif
    7 years ago+ 2 comments

    I found it quite devious that test 0 only tests for truncation, not for rounding, which i suspect is why we see a lot of confused people in the comments. Adding even one example would so many people figure it out on their own.

    4|
    Permalink
  • cs_1716410129
    2 years ago+ 2 comments

    python 3

    def closestNumber(a, b, x):
        f=(math.floor((a**b)/x)*x)
        c=(math.ceil((a**b)/x)*x)
        if((a**b)-f<c-(a**b)):
            return f
        return c
    
    2|
    Permalink
  • nikolay_golubov
    5 years ago+ 1 comment

    JS one line:

    console.log(x*Math.round(Math.pow(a,b)/x));
    

    Difficulty should set to easy...

    2|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature