The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

Example 1:

Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25 Output: 1389537

Constraints:

  • 0 <= n <= 37
  • The answer is guaranteed to fit within a 32-bit integer, ie. answer <= 2^31 - 1.

Note: This problem is from LeetCode.
βœ… Compiled
❌ Executed
❌ Correct
program tribonacci
    implicit none

    integer :: n, i, j, k
    integer :: t(0:37)

    ! initialize the first four terms of the sequence
    t(0) = 0
    t(1) = 1
    t(2) = 1
    t(3) = 2

    do i=4,37
        t(i) = t(i-1) + t(i-2) + t(i-3)
    end do

    print *, "Enter n:"
    read(*,*) n

    if (n < 0 .or. n > 37) then
        print *, "Invalid input"
    else
        print *, "T", n, "=", t(n)
    end if

end program tribonacci
❌ Compiled
❌ Executed
❌ Correct
module tribonacci
implicit none
private
public :: tribonacci_sequence, tribonacci_value

contains

function tribonacci_sequence(n) result(sequence)
integer, intent(in) :: n
integer :: sequence(n)
sequence(0) = 0
sequence(1) = 1
sequence(2) = 1
do i = 3, n
sequence(i) = sequence(i-1) + sequence(i-2) + sequence(i-3)
end do
end function tribonacci_sequence

function tribonacci_value(n) result(value)
integer, intent(in) :: n
integer :: value
value = tribonacci_sequence(n)(n)
end function tribonacci_value
end module tribonacci

program main
use tribonacci
implicit none
integer :: n
do n = 0, 37
write (*,*) 'T', n, ' = ', tribonacci_value(n)
end do
end program main
🌐 Data from online sources
def height_checker(heights):
    expected = sorted(heights)
    count = 0
    for i in range(len(heights)):
        if heights[i] != expected[i]:
            count += 1
    return count

The algorithm consists of the following steps: 1. Create a copy of the given heights array and sort it in non-decreasing order to create the expected array. 2. Iterate through both heights and expected arrays simultaneously and compare the corresponding elements. 3. If heights[i] is not equal to expected[i], increment the counter. 4. Return the counter as the final answer.

This algorithm has time complexity O(n log n) due to the sorting step, where n is the number of students.

🌐 Data from online sources
#include <algorithm>
#include <vector>

int heightChecker(std::vector<int>& heights) {
    std::vector<int> expected = heights;
    std::sort(expected.begin(), expected.end());
    int count = 0;
    for (size_t i = 0; i < heights.size(); i++) {
        if (heights[i] != expected[i]) {
            count++;
        }
    }
    return count;
}

The algorithm consists of the following steps: 1. Create a copy of the given heights array and sort it in non-decreasing order to create the expected array. 2. Iterate through both heights and expected arrays simultaneously and compare the corresponding elements. 3. If heights[i] is not equal to expected[i], increment the counter. 4. Return the counter as the final answer.

This algorithm has time complexity O(n log n) due to the sorting step, where n is the number of students.