Fibonacci Modified HackerRank Solution

Fibonacci Modified HackerRank Solution

In this Fibonacci Modified HackerRank solution, Implement a modified Fibonacci sequence using the following definition:

Given terms  and  where , term  is computed as:

Given three integers, , , and , compute and print the  term of a modified Fibonacci sequence.


Return .

Function Description

Complete the fibonacciModified function in the editor below. It must return the  number in the sequence.

fibonacciModified has the following parameter(s):

  • int t1: an integer
  • int t2: an integer
  • int n: the iteration to report


  • int: the  number in the sequence

Note: The value of  may far exceed the range of a -bit integer. Many submission languages have libraries that can handle such large results but, for those that don’t (e.g., C++), you will need to compensate for the size of the result.

Input Format

A single line of three space-separated integers, the values of , , and .


  •  may far exceed the range of a -bit integer.

Sample Input

0 1 5

Sample Output



The first two terms of the sequence are  and , which gives us a modified Fibonacci sequence of . The  term is .

Fibonacci Modified HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in JavaScript

function fibonacciModified(t1, t2, n) {
    // Write your code here
    for(let i=2;i<n;i++){
        let temp=t2;
    return t2;

Problem Solution in C#

public static BigInteger fibonacciModified(int t1, int t2, int n)
    if (n <= 2) return n == 2 ? t2 : t1;

    var fiboMap = new Dictionary<int, BigInteger>();
    fiboMap.Add(0, (BigInteger)t1);
    fiboMap.Add(1, (BigInteger)t2);

    for (int i = 2; i < n; i++) {
        var seq2 = fiboMap[i - 2];
        var seq1 = fiboMap[i - 1];

        var calc = BigInteger.Pow(seq1, 2);
        calc = BigInteger.Add(calc, seq2);
        fiboMap.Add(i, calc);

    return fiboMap[n - 1];

Problem Solution in Java

static BigInteger fibonacciModified(int t1, int t2, int n) {

    if(n == 1)
        return BigInteger.valueOf(t1);
    if(n == 2)
        return BigInteger.valueOf(t2);

    return fibonacciModified(t1,t2,n - 1).pow(2).add(fibonacciModified(t1,t2,n - 2));
Solve original Problem on HackerRank here. Checkout more HackerRank Problems