Problem · Math

Minimum Steps to a Fibonacci Number

Learn this problem
EasyVirtu Financial logoVirtu FinancialINTERNOA

Problem statement

Given an integer x, return the minimum number of steps required to change x into a Fibonacci number.

In each step, you may either increment or decrement the current number by 1.

The Fibonacci sequence is defined as follows:

  • F[0] = 0
  • F[1] = 1
  • For every i >= 2, F[i] = F[i - 1] + F[i - 2].

The elements of this sequence are called Fibonacci numbers.

Function

minimumFibonacciSteps(x: int) → int

Examples

Example 1

x = 15return = 2

The closest Fibonacci number is 13. Decrementing 15 twice reaches 13, so the minimum number of steps is 2.

Example 2

x = 1return = 0

1 is already a Fibonacci number, so no steps are needed.

Example 3

x = 13return = 0

13 is already a Fibonacci number, so no steps are needed.

Constraints

  • 0 <= x <= 1,000,000

More Virtu Financial problems

drafts saved locally
public int minimumFibonacciSteps(int x) {
    // write your code here
}
x15
expected2
checking account