[LeetCode] Check Good Integer

3959. Check Good Integer

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool checkGoodInteger(int n) {
int ds = 0, ss = 0;
while(n) {
int d = n % 10; n /= 10;
ds += d, ss += d * d;
}
return ss - ds >= 50;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/check-good-integer/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.