fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // -1,0,3,5,9,12
  13. int arr[]={2,3,4,-8,2};
  14. int arr2[]={-5,8,3,1,4};
  15. int res = maximumSubsetOfTwoArrayExcludingAdjacent(arr,arr2);
  16. System.out.println(res);
  17. }
  18.  
  19. public static int maximumSubsetOfTwoArrayExcludingAdjacent(int[] a,int[] b){
  20.  
  21. int []dp =new int[a.length];
  22. dp[0]=Math.max(a[0],b[0]);
  23. dp[1]=Math.max(dp[0],Math.max(a[1],b[1]));
  24. for(int i=2;i<a.length; i++){
  25. dp[i] = Math.max(dp[i-1],Math.max(a[i],b[i])+dp[i-2]);
  26. }
  27.  
  28. return dp[a.length-1];
  29.  
  30. }
  31. }
Success #stdin #stdout 0.09s 54536KB
stdin
Standard input is empty
stdout
12