# Happy Number I - Floyds cycle detection

Write an algorithm to determine if a number n is happy.

A **happy number** is a number defined by the following process:

Starting with any positive integer, replace the number by the sum of the squares of its digits.

Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.

Those numbers for which this process **ends in 1** are happy.

Return true *if* n *is a happy number, and* false *if not*.

```
import java.util.LinkedList;
class Solution {
public boolean isHappy(int n) {
int slow = n;
int fast = n;
//while loop is not used here because initially slow and
//fast pointer will be equal only, so the loop won't run.
do {
//slow moving one step ahead and fast moving two steps ahead
slow = square(slow);
fast = square(square(fast));
} while (slow != fast);
//if a cycle exists, then the number is not a happy number
//and slow will have a value other than 1
return slow == 1;
}
//Finding the square of the digits of a number
public int square(int num) {
int ans = 0;
while(num > 0) {
int remainder = num % 10;
ans += remainder * remainder;
num /= 10;
}
return ans;
}
}
```

## Related Problems

Given an array nums of size n, return *the majority element*.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return *the maximum amount of water a container can store*.

Given a string num that contains only digits and an integer target, return **all possibilities*** to insert the binary operators *'+'*, *'-'*, and/or *'*'* between the digits of *num* so that the resultant expression evaluates to the *target* value*.

Note that operands in the returned expressions **should not** contain leading zeros.

Given an integer x, return true* if *x* is a ***palindrome***, and *false* otherwise*.