Given a number n, evaluate the following expression. f(n-1)*f(n+1) - f(n)*f(n) where f(n) is the n-th Fibonacci number with n >= 1. Fibonacci Sequence is 0, 1, 1, 2, 3, 5, 8,13,… (here 0 is the 0th Fibonacci number).
Examples:
Input: n = 1
Output: -1
Explanation: f(n+1)*f(n-1) - f(n)*f(n) = 1*0 - 1*1 = -1.Input: n = 2
Output: 1
Explanation: f(n+1)*f(n-1) - f(n)*f(n) = 2*1 - 1*1 = 1.
Table of Content
[Naive Approach] Calculate Fibonacci Numbers - O(n) Time and O(1) Space
The idea is to calculate the Fibonacci numbers F(n-1), F(n) and F(n+1) iteratively. Then, use these values to evaluate the given expression.
Working of Approach:
- Initialize F(0) = 0 and F(1) = 1.
- Generate Fibonacci numbers up to F(n).
- Keep F(n-1) and F(n) during the iteration.
- Calculate F(n+1) = F(n-1) + F(n).
- Substitute the values in the given expression and return the result.
#include <cmath>
#include <iostream>
using namespace std;
int fibExpression(int n)
{
// Handle the base case n = 1.
if (n == 1)
return -1;
// Initialize F(0) and F(1).
long long a = 0, b = 1;
// Generate Fibonacci numbers up to F(n).
for (int i = 2; i <= n; i++)
{
long long c = a + b;
a = b;
b = c;
}
// a = F(n-1) and b = F(n).
long long fnMinus1 = a;
long long fn = b;
long long fnPlus1 = fnMinus1 + fn;
// Evaluate the given Fibonacci expression.
return fnMinus1 * fnPlus1 - fn * fn;
}
int main()
{
int n = 2;
cout << fibExpression(n) << endl;
return 0;
}
import java.util.*;
public class GFG {
// Handle the base case n = 1.
public static int fibExpression(int n)
{
if (n == 1)
return -1;
// Initialize F(0) and F(1).
long a = 0, b = 1;
// Generate Fibonacci numbers up to F(n).
for (int i = 2; i <= n; i++) {
long c = a + b;
a = b;
b = c;
}
// a = F(n-1) and b = F(n).
long fnMinus1 = a;
long fn = b;
long fnPlus1 = fnMinus1 + fn;
// Evaluate the given Fibonacci expression.
return (int)(fnMinus1 * fnPlus1 - fn * fn);
}
public static void main(String[] args)
{
int n = 2;
System.out.println(fibExpression(n));
}
}
def fibExpression(n):
# Handle the base case n = 1.
if n == 1:
return -1
# Initialize F(0) and F(1).
a = 0
b = 1
# Generate Fibonacci numbers up to F(n).
for i in range(2, n + 1):
c = a + b
a = b
b = c
# a = F(n-1) and b = F(n).
fnMinus1 = a
fn = b
fnPlus1 = fnMinus1 + fn
# Evaluate the given Fibonacci expression.
return int(fnMinus1 * fnPlus1 - fn * fn)
if __name__ == '__main__':
n = 2
print(fibExpression(n))
using System;
public class GFG {
// Handle the base case n = 1.
public static int fibExpression(int n)
{
if (n == 1)
return -1;
// Initialize F(0) and F(1).
long a = 0, b = 1;
// Generate Fibonacci numbers up to F(n).
for (int i = 2; i <= n; i++) {
long c = a + b;
a = b;
b = c;
}
// a = F(n-1) and b = F(n).
long fnMinus1 = a;
long fn = b;
long fnPlus1 = fnMinus1 + fn;
// Evaluate the given Fibonacci expression.
return (int)(fnMinus1 * fnPlus1 - fn * fn);
}
public static void Main()
{
int n = 2;
Console.WriteLine(fibExpression(n));
}
}
function fibExpression(n)
{
// Handle the base case n = 1.
if (n === 1)
return -1;
// Initialize F(0) and F(1).
let a = 0, b = 1;
// Generate Fibonacci numbers up to F(n).
for (let i = 2; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
// a = F(n-1) and b = F(n).
let fnMinus1 = a;
let fn = b;
let fnPlus1 = fnMinus1 + fn;
// Evaluate the given Fibonacci expression.
return fnMinus1 * fnPlus1 - fn * fn;
}
// Driver Code
let n = 2;
console.log(fibExpression(n));
Output
1
[Expected Approach] Using Cassini's Identity - O(1) Time and O(1) Space
The idea is to use Cassini's Identity, which directly simplifies the given expression to (-1)^n. So we only need to compute parity of n.
By Cassini's Identity:
- n odd -> -1
- n even -> 1
How does above formula work? The formula is based on matrix representation of Fibonacci numbers.

Let us understand with an example:
Input: n = 2
- n % 2 = 0, so n is even.
- Condition (n % 2 == 1) is false.
- Therefore, the function returns 1.
#include <cmath>
#include <iostream>
using namespace std;
int fibExpression(int n)
{
// If n is odd, return -1, otherwise return 1
return ((n % 2 == 1) ? -1 : 1);
}
int main()
{
int n = 2;
cout << fibExpression(n) << endl;
return 0;
}
public class GFG {
// If n is odd, return -1, otherwise return 1
public static int fibExpression(int n)
{
return (n % 2 == 1) ? -1 : 1;
}
public static void main(String[] args)
{
int n = 2;
System.out.println(fibExpression(n));
}
}
def fibExpression(n):
# If n is odd, return -1, otherwise return 1
return -1 if n % 2 == 1 else 1
if __name__ == "__main__":
n = 2
print(fibExpression(n))
using System;
class GFG {
// If n is odd, return -1, otherwise return 1
public static int fibExpression(int n)
{
return (n % 2 == 1) ? -1 : 1;
}
static void Main()
{
int n = 2;
Console.WriteLine(fibExpression(n));
}
}
function fibExpression(n)
{
// If n is odd, return -1, otherwise return 1
return (n % 2 === 1) ? -1 : 1;
}
// Driver Code
let n = 2;
console.log(fibExpression(n));
Output
1