Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Whether a Number Belongs to the Fibonacci Sequence

Basic Acceptance 74.95% Points 10.00

You are given a non-negative integer n. Your task is to determine whether the given number belongs to the Fibonacci sequence.

The Fibonacci sequence starts with 0 and 1. Each subsequent number is calculated as the sum of the two preceding numbers. Return True if n appears in the sequence; otherwise, return False.

Examples
Example 1

Input: n = 21

Output: True

Explanation: 21 appears in the Fibonacci sequence, so the output is True.

Example 2

Input: n = 22

Output: False

Explanation: 22 does not appear in the Fibonacci sequence, so the output is False.

Example 3

Input: n = 0

Output: True

Explanation: 0 is the first number in the Fibonacci sequence, so the output is True.

Constraints
  • 0 <= n <= 1018
Companies
Samsung Intuit
Topics
Data Structures Basics Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions