Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void func(int pos,int[] arr,int[] aux,int n)
- {
- /*
- * pos is current index in the arr
- * arr is array
- * aux is temp array which will store one possible combination.
- * n is size of the array.
- * */
- //if reached at the end, check the summation of differences
- if(pos == n)
- {
- long sum = 0;
- for(int i = 1 ; i < n ; i++)
- {
- //System.out.print("i = " + i + ", arr[i] = " + aux[i] + " ");
- sum += Math.abs(aux[i] - aux[i - 1]);
- }
- //System.out.println();
- //System.out.println("sum = " + sum);
- if(sum > max)
- {
- max = sum;
- }
- return;
- }
- //else try every combination possible.
- for(int i = 1 ; i <= arr[pos] ; i++)
- {
- aux[pos] = i;
- func(pos + 1,arr,aux,n);
- }
- }
Add Comment
Please, Sign In to add comment