Self Descriptive Numbers

Overview

In this article we will write a program to find if the given number is a Self Descriptive Number or not. In mathematics, a self-descriptive number is an integer m that in a given base b is b digits long in which each digit d at position n (the most significant digit being at position 0 and the least significant at position b−1) counts how many instances of digit n are in m.

Example

1210 is a four-digit self-descriptive number:

position 0 has value 1 i.e. there is only one 0 in the number
position 1 has value 2 i.e. there are two 1 in the number
position 2 has value 1 i.e. there is only one 2 in the number
position 3 has value 0 i.e. there is no 3 in the number

Code

First let’s convert the number into string format. Note you don’t have to do this – you can also user other ways to get the digits but I find this way easier.

String valueAsString = Integer.valueOf(number).toString();

Now we will create a map where the key is the digit and the value is the number of occurrences of that digit in the given number.

Map<Integer, Integer> map = new HashMap(valueAsString.length());
for (int i = 0; i < valueAsString.length(); i++) {
    final int c = Character.getNumericValue(valueAsString.charAt(i));
    if (map.containsKey(c)) {
        map.put(c, map.get(c) + 1);
    } else {
        map.put(c, 1);
    }
}

Now we will iterate through each digit and will compare the value from the map:

for (int i = 0; i < valueAsString.length(); i++) {
    int c = Character.getNumericValue(valueAsString.charAt(i));
    if (map.getOrDefault(i, 0) != c) {
        return false;
    }
}

SelfDescriptiveNumber.java

import java.util.HashMap;
import java.util.Map;

public class SelfDescriptiveNumber {

    public static boolean isSelfDescriptiveNumber(int number) {
        String valueAsString = Integer.valueOf(number).toString();
        Map<Integer, Integer> map = new HashMap(valueAsString.length());
        for (int i = 0; i < valueAsString.length(); i++) {
            final int c = Character.getNumericValue(valueAsString.charAt(i));
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        for (int i = 0; i < valueAsString.length(); i++) {
            int c = Character.getNumericValue(valueAsString.charAt(i));
            if (map.getOrDefault(i, 0) != c) {
                return false;
            }
        }
        return true;
    }

}

Conclusion

In this article we saw how to write a Java program to find whether a given number is a self descriptive number or not.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: