LeetCode-in-Java

3959. Check Good Integer

Easy

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:

Example 2:

Input: n = 19

Output: true

Explanation:

Constraints:

Solution

public class Solution {
    public boolean checkGoodInteger(int n) {
        int sum = 0;
        while (n > 0) {
            int digit = n % 10;
            sum += digit * digit - digit;
            n /= 10;
        }
        return sum >= 50;
    }
}