Javatpoint Logo
Javatpoint Logo

Good Number Program in Java

A special mathematical notion known as "good numbers" refers to numbers where each digit is larger than the sum of the digits to its right. In this exercise, we are charged with locating and printing all Good Numbers within the range [L, R], while omitting any numbers that include the digit 'd'. Let's investigate this issue and offer some Java solutions.

Problem Description


A range [L, R] with L R and L and R inclusive.

A 'd' digit that shouldn't be among the Good Numbers.

The aim is to locate and print every Good Number that does not include the digit "d" inside the provided range [L, R].

Method 1: Brute Force Approach

In the brute force method, we approach the problem by systematically checking each number within the given range [L, R] to determine whether it's a Good Number and if it doesn't contain the digit 'd'. Here's a step-by-step breakdown of this method:

  1. Initialization: We begin by creating two important functions, printGoodNumber(int L, int R, int d) and isValid(int n, int d).
  2. Determining whether a Number Is a Good Number: The isValid function determines if the number n, which is supplied, is a Good Number. It achieves this by going over the number's digits in order of right to left. It determines the total of the digits to its right for each one and checks to make sure the digit is bigger than the sum and not equal to 'd'.
  3. Looping Through the Range: In the printGoodNumber function, we iterate through each integer in the [L, R] range. We use the isValid function to determine whether each number-without the digit "d"-is a Good Number.
  4. Printing the Results: When a Good Number without the digit 'd' is identified, we print it to the console.


410 420 421 510 520

Approach 2: Optimized Method

In the previous approach, we checked each number individually, which can be time-consuming for a large range. An optimized approach is to generate Good Numbers without the digit 'd' directly.

  • Initialize a list to store Good Numbers.
  • Iterate through the numbers in the range [L, R].
  • For each number, generate a Good Number without the digit 'd' and add it to the list.

Optimized Approach


410 420 421 510 520

For the input L = 410, R = 520, and d = 3, this code will output the Good Numbers without the digit 3 in the range [410, 520]: 410 420 421 510 520

Time Complexity:

The time complexity is the same as in the previous approach, O((R - L) * log10(R)).

The loop iterates through the range from L to R.

Space Complexity:

The space complexity is O(N), where N is the number of Good Numbers in the range.


In this section, we examined the idea of "Good Numbers" and offered two distinct approaches for locating and printing Good Numbers in a range without the required digit "d." While the optimised technique immediately creates Good Numbers, the brute force approach verifies each number separately. You may select the best strategy for your needs based on the size of the range. These techniques can be used to address issues with certain number attributes and limitations.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA