Skip to content

Instantly share code, notes, and snippets.

@agaase
Created May 13, 2017 14:18
Show Gist options
  • Save agaase/5925b1e73f66cb88cc67df9cff27ed5b to your computer and use it in GitHub Desktop.
Save agaase/5925b1e73f66cb88cc67df9cff27ed5b 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