Autobiographical Number in Java
In this section, we will understand an autobiographical number through a Java Program. The autobiographical numbers are often asked in coding exams and in interviews.
The frequency of the digits 0 through 9 in the sequence in which they appear in a number is numerically specified by a digit count. In other words, a number that is arrogant and self-centered exists just to define itself.
Formally, an autobiographical number is defined as follows:
An autobiographical number (N) is a number in which the first digit counts the occurrence of zeroes in it, the second number counts the occurrence of ones in it., the third number counts the occurrence of twos in it, and so on. It is also sometimes referred to as a curious number. It follows the pattern OEIS A046043.
For numbers up to 10, If the sum of all the digits is equal to the count of digits, the number is termed an autobiographical number.
Examples:
1. 1210
The first digit i.e. 1 counts the number of zeros which is 1.
The second digit i.e. 2 counts the number of ones, which is 2.
The third digit i.e., 1 counts the no. of twos, which is 1.
Thus, it is an autobiographical number.
2. 1021
The first digit i.e. 1 counts the number of zeros which is 1.
The second digit i.e., 0 counts the number of ones, which is 2 but it is showing 0.
The third digit i.e., 2 counts the no. of twos, which is 1, which is 1 but it is showing 2.
Thus, it is not an autobiographical number.
Algorithm:
Step 1: Import the java.util package to use the Scanner class.
Step 2: Define a public class named AutobiographicalNumber.
Step 3: Define the main() method of the AutobiographicalNumber class.
Step 4: Create a Scanner object to read input from the user.
Step 5: Prompt the user to enter a number to check if it is autobiographical.
Step 6: Read the integer input from the user.
Step 7: Calculate the absolute value of the input number to handle negative values.
Step 8: Store the input number in a variable called num.
Step 9: Convert the input number to a String using the valueOf() method and store it in a variable called str.
Step 10: Create an integer array called digita with a length equal to the length of the str variable.
Step 11: Use a for loop to iterate over the digits of the input number from right to left.
Step 12: Inside the loop, extract the last digit of the input number using the modulo operator and store it in the digita array.
Step 13: Remove the last digit from the input number by dividing it by 10.
Step 14: Set a boolean variable called flag to true to represent the initial assumption that the input number is autobiographical.
Step 15: Use a nested for loop to iterate over the digits in the digita array and count the number of occurrences of each digit.
Step 15: Compare the count of each digit with its corresponding value in the digita array.
Step 16: If any count is different from its corresponding digit value, set the flag variable to false and break out of the loop.
Step 17: If the flag variable is still true after the loop, print a message indicating that an input number is an autobiographical number.
Step 18: Otherwise, print a message indicating that the input number is not an autobiographical number.
Implementation:
import java.util.*;
public class AutobiographicalNumber
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number you want to check: ");
//reads an integer from the user to check
int n= sc.nextInt();
//calculates the absolute value of the given number
n = Math.abs(n);
//assigning the value of n into variable num
int num = n;
//the valueOf() method returns the string representation of int argument
String str = String.valueOf(n);
//creation of an array of digits
int digita[] = new int[str.length()];
for(int i = digita.length - 1; i >= 0; i--)
{
//determines the last digit of the given number
digita[i] = num % 10;
//removes the last digit
num = num/10;
}
boolean flag = true;
//an inner loop compares the iterator of the outer loop with each digit of the inner loop //if they are equal then increment the occurrence count of the digit
for(int i = 0; i < digita.length; i++)
{
int count = 0;
for(int j = 0; j < digita.length; j++)
{
if(i == digita[j])
//increments the count by 1 if the above condition is true
count++;
}
if(count != digita[i])
{
flag = false;
//breaks the execution if the condition is true
break;
}
}
if(flag)
//prints if the status is true
System.out.println(n + " is an autobiographical number.");
else
//prints if the status is false
System.out.println(n + " is not an autobiographical number.");
}
}
Output 1:

Output 2:

Complexity Analysis:
Time complexity: The time complexity of this program is O(n^2), where n is the number of digits in the input number. This is because the program uses nested loops to iterate over the array of digits and compare each digit's count with its corresponding value in the array. The outer loop iterates n times, and the inner loop also iterates n times, making the overall time complexity O(n^2).
Space Complexity: The space complexity of this program is O(n), where n is the number of digits in the input number. This is because the program creates an array of n integers to store the digits of the input number, and also creates several integer variables to store intermediate results. However, the space used by these variables is negligible compared to the array's space, making the overall space complexity O(n).