Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Happy Number problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Happy Number problem solution Write an algorithm to determine if a number n is happy.

  1. A happy number is a number defined by the following process:
  2. Starting with any positive integer, replace the number with the sum of the squares of its digits.
  3. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  4. Those numbers for which this process ends in 1 are happy.
  5. Return true if n is a happy number, and false if not.
Leetcode Happy Number problem solution

Leetcode Happy Number problem solution in Python.

class Solution(object):
    def isHappy(self, n, prev=[]):
        nums = sum([int(x)**2 for x in list(str(n))])
        if nums in prev:
            return False
        return self.isHappy(nums, prev+[nums]) if nums != 1 else True

Happy Number problem solution in Java.

class Solution {
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while(n != 1){
            if(set.contains(n)){
                return false;
            }
            set.add(n);
            n = sum(n);
        }
        return true;
    }
    
    public int sum(int n){
        int sum = 0;
        while(n > 0){
            sum += (n%10) * (n%10);
            n = n/10;
        }
        return sum;
    }
}

Problem solution in C++.

class Solution {
public:
    map<int, int> terminate;
    bool isHappy(int n) {
        int temp = 0;
        if(n==1){
            return 1;
        }
        while(n!=0){
            temp += pow(n%10, 2);
            n = n/10;
        }
        if(terminate[temp] == 0){
            terminate[temp] = 1;
        } else{
            return 0;
        }
        return isHappy(temp);
    }
};

Problem solution in C.

int key[1000],i = 0;
int count = 0,temp;
while(i<1000)       //Initialize
	key[i++]=1;

for(;n;n/=10)            
	{
		temp=n%10;
		count+=temp*temp;
	}
if(count==1) return true;

n=count;
while(key[n])
{
	
	int x=n;
	count=0;
	for(;n;n/=10)
		{
			temp=n%10;
			count+=temp*temp;
		}
	if(count==1) return true;
	else
	{
		key[x]=0;
		n=count;
	}
}
return false;

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes