Increase Number Roundness

Description:

Define an integer’s roundness as the number of trailing zeroes in it.

Given an integer n, check if it’s possible to increase n‘s roundness by swapping some pair of its digits.

Example

  • For n = 902200100, the output should be
    increaseNumberRoundness(n) = true.

    One of the possible ways to increase roundness of n is to swap digit 1with digit 0 preceding it: roundness of 902201000 is 3, and roundness of nis 2.

    For instance, one may swap the leftmost 0 with 1.

  • For n = 11000, the output should be
    increaseNumberRoundness(n) = false.

    Roundness of n is 3, and there is no way to increase it.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] integer n

    A positive integer.

    Constraints:
    100 ≤ n ≤ 109.

  • [output] boolean

    true if it’s possible to increase n‘s roundness, false otherwise.

Tests:
Solution:

bool increaseNumberRoundness(int n) {
    bool gotToSignificant = false;
    while (n > 0) {
        if (n % 10 == 0 && gotToSignificant) {
            return true;
        } else if (n % 10 != 0) {
            gotToSignificant = true;
        }
        n /=  10 ;
    }

    return false;    
}
Advertisements

5 thoughts on “Increase Number Roundness

  1. It does work guys. Remember it’s in C++. I suspect those that say it doesn’t attempted to use it in Javascript or similar languages.

    If that’s the case don’t forget to use Math.floor() to keep ‘n’ rounded down.

    Javascript – Line 9:
    n = Math.floor(n / 10 );

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s