Guest User

Untitled

a guest
Nov 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. public static void func(int pos,int[] arr,int[] aux,int n)
  2. {
  3. /*
  4. * pos is current index in the arr
  5. * arr is array
  6. * aux is temp array which will store one possible combination.
  7. * n is size of the array.
  8. * */
  9.  
  10.  
  11.  
  12. //if reached at the end, check the summation of differences
  13. if(pos == n)
  14. {
  15. long sum = 0;
  16. for(int i = 1 ; i < n ; i++)
  17. {
  18. //System.out.print("i = " + i + ", arr[i] = " + aux[i] + " ");
  19. sum += Math.abs(aux[i] - aux[i - 1]);
  20. }
  21. //System.out.println();
  22. //System.out.println("sum = " + sum);
  23. if(sum > max)
  24. {
  25. max = sum;
  26. }
  27. return;
  28. }
  29.  
  30. //else try every combination possible.
  31. for(int i = 1 ; i <= arr[pos] ; i++)
  32. {
  33. aux[pos] = i;
  34. func(pos + 1,arr,aux,n);
  35. }
  36. }
Add Comment
Please, Sign In to add comment