Created
April 17, 2014 22:28
-
-
Save rvbsanjose/11015097 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the JavaScript language, have the function ArrayAddition(arr) take the array of numbers stored in arr | |
// and return the string true if any combination of numbers in the array can be added up to equal the largest | |
// number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the | |
// output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the | |
// same elements, and may contain negative numbers. | |
// Input = 5,7,16,1,2 Output = "false" | |
// Input = 3,5,-1,8,12 Output = "true" | |
function ArrayAddition( arr ) { | |
var largest = arr.sort( function ( x, y ) { return x - y; }).pop(); | |
var sum; | |
var times = 0; | |
while ( times < arr.length-1 ) { | |
sum = arr[0]; | |
var index = 1; | |
while ( index < arr.length ) { | |
for ( var j = index; j < arr.length; j++ ) | |
sum += arr[j]; | |
if ( sum == largest ) | |
return 'true'; | |
index += 1; | |
sum = arr[0]; | |
} | |
var temp = arr[times+1]; | |
arr[times+1] = arr[0]; | |
arr[0] = temp; | |
times += 1; | |
} | |
return 'false'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment