How to solve the Happy Number problem in Java
Problem description
In this shot, we will solve an interview problem from Leetcode called Happy Number.
We will learn how to determine whether or not a number is happy. A happy number is a number that converges to a sum of after getting replaced by the sum of the square of its digits.
Examples
Let’s understand the Happy Number problem with the help of examples:
-
Suppose the number is . We have the sum of squares of digits as below:
+ = 16 + 16 = 32
+ = 9 + 4 = 13
+ = 9 + 1 = 10
+ = 1 + 0 = 1
Finally, the sum of squares of the digits converges to 1, so 44 is a happy number. The program’s output for this input would be true.
-
Now, suppose the number is 16, here we have the sum of squares of digits as below:
+ = 1 + 36 = 37
+ = 9 + 49 = 58
+ = 25 + 49 = 74
+ = 49 + 16 = 65
+ = 36 + 25 = 61
+ = 36 + 1 = 37
Here the sum of the digits’ squares is not converging to , so it would keep repeating with the pattern (37, 58, 74, 65, 61, 37, 58, 74, … so on). Therefore, is not a happy number, and the program’s output for this input would be false.
Solution approach
We first find the sum of digits using a while loop and the modulo % operator. Then, we will store the values within a java.util.Set to check whether or not the sum value has been encountered previously.
The code below provides a solution to the Happy Number problem:
import java.util.*;class Solution {public static boolean isHappy(int n) {Set<Integer> set = new HashSet<>();while (n!=1) {if(set.contains(n)) return false;set.add(n);n = sumOfDigitSquares(n);}return true;}private static int sumOfDigitSquares(int n) {int sum = 0;while (n != 0) {int digit = n % 10;sum += digit * digit;n /= 10;}return sum;}public static void main(String[] args) {System.out.printf("Is 44 happy -> %b", isHappy(44));System.out.printf("\nIs 16 happy -> %b", isHappy(16));}}
Explanation
-
Line 1 imports
java.util.*(using a wildcard as we want to import bothSetandHashSet). -
Line 3 declares a
Solutionclass. -
Line 4 defines the
isHappy(int n)method. -
Line 5 declares a
setusingjava.util.HashSetto store integers. -
Line 7 repeats a
whileloop until the value ofnequals1. -
Line 8 checks if the value of
nis already present inSet. If so, it returnsfalsedenoting the numbernis not a happy number. -
Line 10 adds
nto theset. -
Line 11 calculates the sum of the square of digits using the
sumOfDigitSquares()method and updates the value ofnwith the returned value. -
Line 12 exits the
whileloop whenn == 1. -
Line 14 returns
true, denoting the number as a happy number. -
Line 17 declares the
sumOfDigitSquares(int n)method. -
Line 18 initializes
sumto0. -
Line 19 repeats a
whileloop untilnis not equal to0. -
Line 20 gets the current digit as
n % 10. -
Line 21 updates the
sumas the current value plus square ofdigit(i.e.digit * digit). -
Line 22 updates
nton / 10(integer division). -
Lines 23 and 25 exit the
whileloop ifnequals0and returnsum. -
Line 28 declares the
main()method to verify our solution. -
Lines 29 and 30 call the
isHappy()with different inputs and print the output.
Analysis
The time complexity of this problem would be , where is the number of digits in the number input. The max value of can be , as in Java, the range of integers is up to (which has digits). More precisely, the algorithm runs in constant time .
As the memory required for this program does not depend on the input value, the space complexity of this problem would be constant, i.e., .