Check Good Integer solution and explanation
6 more videos on this problem
Check Good Integer | LeetCode 3959 | Weekly Contest 506 | Java | Developer CoderDeveloper Coder · 3:34 · 143 views
Check Good Integer | Leetcode 3959 | Explanation With Code | javacodewithsitaram · 5:18 · 59 views
LeetCode Weekly Contest 506 | Problem 3959 & 3960 Explained in JavaProof2Code · 14:22 · 50 views
LeetCode 3959 - Check Good Integer | Easy Math Problem Explained in C++Tapan Vachhani · 2:42 · 5 views
Check Good Integer | Leetcode weekly contest 506 | #3959UMANG AGRAWAL · 5:49 · 4 views
LeetCode 3959 | Check Good Integer | Easy C++ SolutionMunjal Raval · 4:42 · 4 views
LeetCode 3959 | Check Good Integer | String & Digit Check | C++ SolutionsPatel Jivan · 2:39 · 2 views
The problem
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
digitSumis1 + 0 + 0 + 0 = 1. - The
squareSumis12 + 02 + 02 + 02 = 1. - The
squareSum - digitSumis1 - 1 = 0. As 0 is not greater than or equal to 50, the output isfalse.
Example 2:
Input: n = 19
Output: true
Explanation:
- The digits of 19 are 1 and 9.
- The
digitSumis1 + 9 = 10. - The
squareSumis12 + 92 = 1 + 81 = 82. - The
squareSum - digitSumis82 - 10 = 72. As 72 is greater than or equal to 50, the output istrue.
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).
class Solution:
def checkGoodInteger(self, n: int) -> bool:
s = 0
while n:
n, x = divmod(n, 10)
s += x * (x - 1)
return s >= 50class Solution {
public boolean checkGoodInteger(int n) {
int s = 0;
for (; n > 0; n /= 10) {
int x = n % 10;
s += x * (x - 1);
}
return s >= 50;
}
}class Solution {
public:
bool checkGoodInteger(int n) {
int s = 0;
for (; n > 0; n /= 10) {
int x = n % 10;
s += x * (x - 1);
}
return s >= 50;
}
};function checkGoodInteger(n: number): boolean {
let s: number = 0;
for (; n; n = Math.floor(n / 10)) {
const x = n % 10;
s += x * (x - 1);
}
return s >= 50;
}func checkGoodInteger(n int) bool {
s := 0
for ; n > 0; n /= 10 {
x := n % 10
s += x * (x - 1)
}
return s >= 50
}Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Digit Extraction with Modulo | O(d) | O(1) | Best general solution when working directly with integers and minimizing memory |
| String Comparison | O(d) | O(d) | When readability and quick implementation matter more than strict memory usage |
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.
