fork download
  1. public class Main {
  2.  
  3. static int countSequences(int n, int[] b) {
  4. int[] dp = new int[n+2];
  5. //dp[i] = number of ways to reach index i
  6. dp[1] = 1; //one way to reach 1
  7. dp[2] = 1; //can jump 2 so directly reach a[2]
  8. for (int i = 3; i <= n; i++) {
  9. // you can come to i from i-1 only normally
  10. dp[i] = dp[i-1];
  11. if(b[i-2]==2) dp[i] += dp[i-2];
  12. }
  13. return dp[n];
  14. }
  15.  
  16. public static void main(String[] args) {
  17. int n = 5;
  18. int[] b = {1, 2, 1, 1, 2}; // Example array b
  19.  
  20. System.out.println(countSequences(n, b));
  21. }
  22. }
  23.  
Success #stdin #stdout 0.08s 54664KB
stdin
Standard input is empty
stdout
2