Array Addition One Coderbyte Solution

0

Have the function ArrayAdditionI(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.

Array Addition I Coderbyte Solution In Java

import java.util.Arrays;
import java.util.Scanner;

public class ArrayAditionOne {
String ArrayAdditionI(int[] arr) {
Arrays.sort(arr);

int highestNum = arr[arr.length - 1];
int sum = 0;
for (int i = 0; i < arr.length - 1; i++){
sum += arr[i];
for (int j = 0; j < arr.length - 1; j++){
if (i != j) {
sum += arr[j];
if (sum == highestNum) {
return "true";
}
}
}
for (int k = 0; k < arr.length - 1; k++) {
if (i != k) {
sum -= arr[k];
if (sum == highestNum) {
return "true";
}
}
}
sum = 0;
}

return "false";
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
ArrayAditionOne c = new ArrayAditionOne();
System.out.print(c.ArrayAdditionI(new int[] {4, 6, 23, 10, 1, 3}));
}
}

Array Addition I Coderbyte Python


def ArrayAdditionI(arr):
from itertools import combinations
max_of_arr = max(arr)
arr.pop(arr.index(max_of_arr))
for i in range(1, len(arr) + 1):
for lst in combinations(arr, i):
if max_of_arr == sum(lst):
return "true"
return "false"
# keep this function call here
# to see how to enter arguments in Python scroll down
print ArrayAdditionI(raw_input())

Array Addition I Coderbyte JavaScript

Using the JavaScript language, have the function ArrayAdditionI(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. 


function ArrayAdditionI(arr) {

arr.sort(function(a,b){return a - b});
var maxNum = arr.pop();
var tot = 0;
for (var i = 0; i < arr.length; i++){
tot += arr[i];
for (var j = 0; j < arr.length; j++){
if (i != j) {
tot += arr[j];
if (tot == maxNum) {
return true;
}
}
}
for (var k = 0; k < arr.length; k++) {
if (i != k) {
tot -= arr[k];
if (tot == maxNum) {
return true;
}
}
}
tot = 0;
}
return false;
}


Explanation:

To get the largest number I am going to sort the array in ascending order which leaves the largest number at the end of the array. I can get largest number using pop() function. To get the total I am going to use two nested loops. The outer loop goes through every number in the array. The inner loop then adds all the other numbers in the array and then compares the total to the largest number. If a match is found then return true else return false. 

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !