Given a number K, the task is to find the sum of numbers at the Kth level of the Fibonacci triangle.
Input: K = 3 Output: 10 Explanation: Fibonacci triangle till level 3: 0 1 1 2 3 5 Sum at 3rd level = 2 + 3 + 5 = 10 Input: K = 2 Output: 2 Explanation: Fibonacci triangle till level 3: 0 1 1 Sum at 3rd level = 1 + 1 = 2
- Till Kth level, i.e. from level [1, K-1], count of Fibonacci numbers already used can be computed as:
cnt = N(Level 1) + N(Level 2) + N(Level 3) + ... + N(Level K-1) = 1 + 2 + 3 + ... + (K-1) = K*(K-1)/2
- Also, we know that the Kth level will contain K Fibonacci numbers.
- Therfore we can find the numbers in the Kth level as Fibonacci numbers in the range [(cnt + 1), (cnt + 1 + K)].
- We can find the sum of Fibonacci numbers in a range in O(1) time using Binet’s Formula.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all the numbers present at given level in Pascal's triangle
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Sum of all the numbers present at given level in Modified Pascal’s triangle
- Program to print Fibonacci Triangle
- Maximum of all the integers in the given level of Pascal triangle
- Sum of Fibonacci Numbers
- Non Fibonacci Numbers
- GCD and Fibonacci Numbers
- Even Fibonacci Numbers Sum
- Find the sum of first N odd Fibonacci numbers
- Generalized Fibonacci Numbers
- Alternate Fibonacci Numbers
- Sum of squares of Fibonacci numbers
- Prime numbers and Fibonacci
- The Magic of Fibonacci Numbers
- C Program for Fibonacci numbers
- Program for Fibonacci numbers
- Sum of Fibonacci Numbers in a range
- Sum of all the numbers in the Nth row of the given triangle
- Sum of Fibonacci Numbers with alternate negatives