Skip to content

Instantly share code, notes, and snippets.

@agaase
Created May 13, 2017 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agaase/8010499249bd0bda6c94449f7472ae53 to your computer and use it in GitHub Desktop.
Save agaase/8010499249bd0bda6c94449f7472ae53 to your computer and use it in GitHub Desktop.
Max absolute difference between two sub contiguous arrays
//Solution to problem here - http://practice.geeksforgeeks.org/problems/max-absolute-difference/0
var findCombination = function(arr){
debugger;
var comb = [];
if(arr.length>1){
comb.push(arr);
comb = comb.concat(findCombination(arr.slice(1,arr.length)));
comb = comb.concat(findCombination(arr.slice(0,arr.length-1)));
return comb;
}else{
return arr
}
}
var sum = function(arr){
var s =0;
for(var i=0;i<arr.length;i++){
s += arr[i];
}
return s;
}
var arr = [2, -1, -2, 1, -4, 2, 8], max=0;
for(var i=1;i<arr.length-1;i++){
var arr1 = findCombination(arr.slice(0,i));
var arr2 = findCombination(arr.slice(i,arr.length));
for(var j=0;j<arr1.length;j++){
for(var k=0;k<arr2.length;k++){
var s = Math.abs(sum(arr1[j]) - sum(arr2[k]));
if(s>max){
max = s;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment