* This is optimized version of Fibonacci Program. Without using Hashmap and recursion. * It saves both memory and time. * Space Complexity will be O(1) * Time Complexity will be O(n) *
* Whereas , the above functions will take O(n) Space. * @author Shoaib Rayeen (https://github.com/shoaibrayeen) **/ private static int fibOptimized(int n) { if (n == 0) { return 0; } int prev = 0, res = 1, next; for (int i = 2; i < n; i++) { next = prev + res; prev = res; res = next; } return res; } }