Happy Number Java | 202 Leetcode Java Solution

Introduction

In this blog let us try to resolve a leetcode problem!

As per leetcode

A happy number is defined by the following process:

  1. Starting with any positive integer, replace the number by the sum of the squares of its digits.
  2. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  3. Those numbers for which this process ends in 1 are happy numbers.

Let’s take an example to understand happy number:

Example 1:

Input: 19

Output: true

Explanation:

Starting with 19, we calculate the sum of the squares of its digits:

1^2 + 9^2 = 82

Next, we calculate the sum of the squares of the digits of 82:

8^2 + 2^2 = 68

Next, we calculate the sum of the squares of the digits of 68:

6^2 + 8^2 = 100

Next, we calculate the sum of the squares of the digits of 100:

1^2 + 0^2 + 0^2 = 1

As the final result is 1, the number 19 is a happy number.

Example 2:

Input: 4

Output: false

Explanation:

Starting with 4, we calculate the sum of the squares of its digits:

4^2 = 16

Next, we calculate the sum of the squares of the digits of 16:

1^2 + 6^2 = 37

Next, we calculate the sum of the squares of the digits of 37:

3^2 + 7^2 = 58

Next, we calculate the sum of the squares of the digits of 58:

5^2 + 8^2 = 89

Next, we calculate the sum of the squares of the digits of 89:

8^2 + 9^2 = 145

Next, we calculate the sum of the squares of the digits of 145:

1^2 + 4^2 + 5^2 = 42

Next, we calculate the sum of the squares of the digits of 42:

4^2 + 2^2 = 20

Next, we calculate the sum of the squares of the digits of 20:

2^2 + 0^2 = 4

As the process enters a cycle of

4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4

and does not reach 1, the number 4 is not a happy number.

Before looking into below solution it is recommended that you try to solve this problem on your own and then look at the below solution. This would help your problem solving skills!

To solve the leetCode Happy Number problem, we need to keep on calculating the sum of the squares of the digits of a given number until we either reach 1 (in which case the number is a happy number) or enter a cycle (in which case the number is not a happy number).

We can keep track of the numbers encountered during this process using a HashSet and return the appropriate result based on whether the process reaches 1 or enters a cycle.

Program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
    public boolean isHappy(int n) {
        
              
        if(n==1)
            return true;
        
        Set<Integer> nums = new HashSet<>();
        
        int sum=0;
        
        while(sum!=1 && !nums.contains(n)){
            
            nums.add(n);
            sum=0;
            
            while(n!=0){
                
                int digit= n%10;
                sum+=digit*digit;
                n=n/10;
                
            }
            
            n=sum;
            
        }
        
        return n==1;
    }
}

In the above program The isHappy function uses a HashSet to keep track of the numbers encountered during the calculation process.

It comes out of the while loop in the above 2 conditions:

  1. If a number is encountered that has already been seen, it means that the number is in a cycle and the while loop breaks.

  2. If the calculated number is 1.

If the number is 1 it is a happy number. If the number found is other than 1 it is not happy number.

Hope this has helped you resolve Happy Number problem in Java.