Skip to main content

Check Good Integer - Solution & Explanation

EasyMathSimulation5 min read
Practice this problem

Problem Statement

You are given a positive integer n.

Let digitSum be the sum of the digits of n, and let squareSum be the sum of the squares of the digits of n.

An integer is called good if squareSum - digitSum >= 50.

Return true if n is good. Otherwise, return false.

 

Example 1:

Input: n = 1000

Output: false

Explanation:

  • The digits of 1000 are 1, 0, 0, and 0.
  • The digitSum is 1 + 0 + 0 + 0 = 1.
  • The squareSum is 12 + 02 + 02 + 02 = 1.
  • The squareSum - digitSum is 1 - 1 = 0. As 0 is not greater than or equal to 50, the output is false.

Example 2:

Input: n = 19

Output: true

Explanation:

  • The digits of 19 are 1 and 9.
  • The digitSum is 1 + 9 = 10.
  • The squareSum is 12 + 92 = 1 + 81 = 82.
  • The squareSum - digitSum is 82 - 10 = 72. As 72 is greater than or equal to 50, the output is true.

 

Constraints:

  • 1 <= n <= 109

Approach Overview

Problem Overview: You are given an integer and must determine whether it is a good integer. A number is considered good if every digit in the number is identical. For example, 7, 111, and 9999 are good integers, while 123 or 101 are not.

Approach 1: Digit Extraction with Modulo (O(d) time, O(1) space)

Extract digits one by one using modulo and division. Store the last digit using n % 10. Then repeatedly divide the number by 10 and compare each extracted digit with the stored one. If any digit differs, the integer is not good. This method works directly on the numeric representation without converting to another data type. It is memory efficient and relies on basic arithmetic operations.

Approach 2: String Comparison (O(d) time, O(d) space)

Convert the integer to a string and compare each character with the first character. Iterate through the string and return false as soon as a mismatch appears. This approach is simpler to read and implement, especially in high-level languages. The tradeoff is a small additional memory cost due to the string conversion.

Both approaches involve a single pass through the digits. The number of digits d is log10(n), so the runtime grows linearly with digit count. Problems like this frequently appear when practicing strings manipulation or math based digit processing. The digit extraction technique is also common in number theory style interview questions.

Recommended for interviews: The digit extraction approach using modulo and division is usually preferred. It shows you understand how numbers are represented and avoids unnecessary conversions. Mentioning the string-based alternative demonstrates awareness of readability tradeoffs, but the arithmetic method highlights stronger problem-solving fundamentals.

Solution

We use a variable s to record the result of the square sum minus the digit sum of n. If s is greater than or equal to 50, we return true; otherwise, we return false.

The time complexity is O(log n), where log n is the number of digits in n. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Digit Extraction with ModuloO(d)O(1)Best general solution when working directly with integers and minimizing memory
String ComparisonO(d)O(d)When readability and quick implementation matter more than strict memory usage

Video Solution

Check Good Integer | LeetCode 3959 | Weekly Contest 506 | Java | Developer CoderDeveloper Coder143 views views

Watch 6 more video solutions →

Frequently Asked Questions

Is Check Good Integer easy or hard?
Check Good Integer is categorized as an Easy problem. The logic involves basic iteration through digits and simple comparisons, making it suitable for beginners practicing loops, conditions, and number handling.
Check Good Integer Python/Java solution
Both Python and Java solutions usually follow the same pattern: extract digits using modulo and division or convert the number to a string and compare characters. Each approach runs in linear time relative to the number of digits.
How to solve Check Good Integer in O(n)?
Treat n as the number of digits in the integer. Extract digits using n % 10 and n // 10 while comparing each digit with the first extracted digit. If all digits match, return true; otherwise return false. This performs a single linear scan across the digits.
What is the best approach for Check Good Integer?
The most efficient approach iterates through the digits using modulo and division. Store the last digit and compare it with every other digit extracted from the number. This runs in O(d) time where d is the number of digits, and uses O(1) extra space.
Is Check Good Integer asked at Google/Amazon/Meta?
Digit validation problems similar to Check Good Integer appear in coding interviews at large tech companies. They are typically used as warm‑up problems to test understanding of number manipulation, loops, and basic conditional logic.
What data structure is used in Check Good Integer?
No complex data structure is required. The optimal approach works directly with integer arithmetic. A string representation of the number may be used in an alternative solution for easier iteration over digits.
What is the time complexity of Check Good Integer?
The time complexity is O(d), where d is the number of digits in the integer. Each digit is checked exactly once to verify that it matches the reference digit. Space complexity can be O(1) with arithmetic digit extraction or O(d) if using string conversion.

Ready to solve this problem?

Practice Check Good Integer with our built-in code editor and test cases.

Practice on FleetCode