diff --git a/Graphs/SumOfFibonacciNumbers.cpp b/Graphs/SumOfFibonacciNumbers.cpp index c81ba11..cf1dd74 100644 --- a/Graphs/SumOfFibonacciNumbers.cpp +++ b/Graphs/SumOfFibonacciNumbers.cpp @@ -1,39 +1,19 @@ -// https://www.interviewbit.com/problems/sum-of-fibonacci-numbers/ - -int Solution::fibsum(int A) { - // Do not write main() function. - // Do not read input, instead use the arguments to the function. - // Do not print the output, instead return values as specified - // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details - - vector vec; - - vec.push_back(1); - vec.push_back(1); - - int fib, i = 2; - - while(fib <= A){ - fib = vec[i-2] + vec[i-1]; - vec.push_back(fib); - i++; - } - - int j = vec.size()-1; - int sol = 0; - - LOOP:while(A && j >= 0){ - if(vec[j] == A){ - sol++; - return sol; - } - else if(vec[j] < A){ - sol++; - A = A - vec[j]; - goto LOOP; - } - j--; - } - - return 0; +int Solution::fibsum(int n) { + vector fib; + fib.push_back(1);fib.push_back(1); // now u have 1 and 1 at v[0] and v[1] + for(int i=2;fib[i-1]0){ + while(fib[size]>n){ // find the largest fibonaci number < n + size--; + } // now decraese the largest fibonaci number from n + n=n-fib[size]; + ans++; + } + return ans; }