Answers for "ways to represent a number as sum of 1's and 2's"

0

ways to represent a number as sum of 1's and 2's

// The number of ways to represent a number with only 1s and 2s is 
// equal to (n+1)th fibonacci number 
public static int nthFibonacciNumber(int n) {
	int[] arr = new int[n+2];
  	arr[0] = 0;
  	arr[1] = 1;
  	for(int i = 2; i <= n; i++) {
    	arr[i] = arr[i - 1] + arr[i -2];
    }
  	return arr[n];
}
Posted by: Guest on March-05-2022

Code answers related to "ways to represent a number as sum of 1's and 2's"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language