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
answer <= 2^31 - 1
.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
Enter n:
At line 18 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b5d2b85e960 in ??? #1 0x7b5d2b85f4d9 in ??? #2 0x7b5d2bab317b in ??? #3 0x7b5d2baac684 in ??? #4 0x7b5d2baad2aa in ??? #5 0x59f39b34c303 in MAIN__ #6 0x59f39b34c4a1 in main
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
temp.f95:22:7: 22 | value = tribonacci_sequence(n)(n) | 1 Error: Syntax error in VALUE statement at (1) temp.f95:14:4: 14 | do i = 3, n | 1 Error: Symbol βiβ at (1) has no IMPLICIT type temp.f95:11:9: 11 | sequence(0) = 0 | 1 Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 1 temp.f95:27:5: 27 | use tribonacci | 1 Fatal Error: Cannot open module file βtribonacci.modβ for reading at (1): No such file or directory compilation terminated.
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.
#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.