fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Codechef
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // your code goes here
  10. Scanner sc=new Scanner(System.in);
  11. int t=sc.nextInt();
  12. while(t-->0){
  13. int n=sc.nextInt();
  14. int[]a=new int[n+1];
  15. for(int i=1;i<=n;i++)a[i]=sc.nextInt();
  16. int[][]dp=new int[n+1][3];
  17. if(a[1]%2==0){
  18. dp[1][2]=1; //even
  19. }
  20. if(a[1]%2!=0){
  21. dp[1][1]=1; //odd
  22. }
  23. for(int i=2;i<=n;i++){
  24. if(a[i]%2==0){
  25. dp[i][2]=dp[i-1][2]+dp[i-2][2];
  26. dp[i][1]=dp[i-1][1]+dp[i-2][1];
  27. }else{
  28. dp[i][2]=dp[i-1][1]+dp[i-2][1];
  29. dp[i][1]=dp[i-1][2]+dp[i-2][2];
  30. }
  31. }
  32. System.out.println("Number of odd sum journey : "+dp[n][1]);
  33. System.out.println("Number of even sum journey : "+dp[n][2]);
  34. System.out.println();
  35. }
  36. sc.close();
  37.  
  38. }
  39. }
  40.  
Success #stdin #stdout 0.14s 56952KB
stdin
2
4
5 4 2 6
5
2 3 5 8 10
stdout
Number of odd sum journey : 3
Number of even sum journey : 0

Number of odd sum journey : 3
Number of even sum journey : 2