Floyd Cycle-Finding Algorithm

Floyd’s Cycle-Finding Algorithm Basic Algo Floyd Cycle Algorithm states that: Traverse linked list using two pointers. Move one pointer(slow_p or tortoise or walker) by one and another pointer(fast_p or hare or runner) by two. If these pointers meet at the same node then there is a loop. If pointers do not meet then linked list doesn’t have a loop. This is also called tortoise and hare approach. ...

Find Duplicate Number in array containing n+1 numbers between 1 and n

Problem Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space. OR You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. ...

Happy Number Problem

Happy Number Problem Write an algorithm to determine if a number is “happy”. Definition 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. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy