Recursion: Fibonacci Numbers HackerRank Solution


Recursion: Fibonacci Numbers HackerRank Solution
Source : https://www.hackerrank.com/challenges/ctci-fibonacci-numbers



Source : https://www.hackerrank.com/challenges/ctci-fibonacci-numbers


Solution


// Karthikalapati.blogspot.com
import java.util.Scanner;
// Time Complexity: O(n)
// Space Complexity: O(1)
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
System.out.println(fibonacci(n));
}
private static int fibonacci(int n) {
int prev = 0;
int curr = 1;
for (int i = 2; i <= n; i++) {
int next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
}

No comments:

Post a Comment