1
[1,2,3,6], 3 = True <== 1+2=3
[1,2,3,6], 9 = True <== 3+6=9
[1,2,3,6], 5 = True <== 2+3=5
[1,2,3,6], 10 = False <== 1+2, 2+3, 3+6, 6+1 not equal to 10
[6,2,3,1], 10 = False <== 6+2, 2+3, 3+1, 1+6 not equal to 10
[6,3,3,1], 6 = True <== 3+3=6

JavaScript is preferred
if given sum is equal to sum of any two elements in the array, function need to return true; otherwise function need to return false.

Kumar
  • 11
  • 1
  • 4
  • 1
    What is your question? Have you thought about the problem? Have you done any research about solving it? What have you found out? – Felix Kling Oct 22 '18 at 23:30
  • Possible duplicate of [How to find the sum of an array of numbers](https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers) – Arya Oct 23 '18 at 01:19

3 Answers3

3

Here's a very simple way that will check if two numbers in an array equal a value. Note, this only allows distinct sums (a number summed with itself doesn't count).

function test(n, arr){
   return arr.some((item, i) => arr.slice(i+1).includes(n-item))
}

let arr = [1, 2, 3, 6]

console.log(test(5, arr))   // true 2+3
console.log(test(4, arr))   // true 1+3
console.log(test(12, arr))  // false because 6 + 6 doesn't count
console.log(test(10, arr))  // false no sums

If the array is all positive numbers you can add an extra test to short circuit cases that can't be true with something like:

 return arr.some((item, i) => n > item && arr.slice(i+1).includes(n-item))

If you want to allow numbers to sum with themsleves, just test against the whole array:

 return arr.some((item, i) => arr.includes(n-item))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

You can use the function some to check that at least one operation is true.

This approach follows your use cases

let check = (sum, arr) => arr.concat(arr[0]).some((n, i, a) => (n + a[i+1]) === sum);
    
console.log(check(3, [1,2,3,6]));
console.log(check(9, [1,2,3,6]));
console.log(check(5, [1,2,3,6]));
console.log(check(10, [1,2,3,6]));
console.log(check(10, [6,2,3,1]));
console.log(check(6, [6,3,3,1]));
console.log(check(7, [3,1,2,4]));
console.log(check(7, []));
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you need to check ANY two values from that array, follow this approach:

let check = (sum, arr) => {
  let inner = arr.concat(arr[0]);
  for (let i = 0, {length} = inner; i < length; i++) {
    for (let j = i + 1, {length} = inner; j < length; j++) {
      if (inner[i] + inner[j] === sum) return true;
    }
  }
  return false;
};
    
console.log(check(3, [1,2,3,6]));
console.log(check(9, [1,2,3,6]));
console.log(check(5, [1,2,3,6]));
console.log(check(10, [1,2,3,6]));
console.log(check(10, [6,2,3,1]));
console.log(check(6, [6,3,3,1]));
console.log(check(7, [3,1,2,4]));
console.log(check(4, [1,1,3,7]));
console.log(check(7, []));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
  • `check(4, [1,2,3,6])` returns false. – Mark Oct 22 '18 at 23:23
  • Maybe I misunderstood, but shouldn't it be true since 1 + 3 = 4 – Mark Oct 22 '18 at 23:25
  • I read: `if given sum is equal to sum of **any** two elements in the array, function need to return true` The given sum: 4 is equal to the sum of two element [1, 3] in the array. – Mark Oct 22 '18 at 23:28
0

Your going to have to iterate through the array of values. Not familiar with javascript so here is the code in a java function:

public boolean checkSum(){
  int sumValue = 10; //value you want to check, could be passed in as a parameter
  int arr[4] = {1,2,4,6};
  for (int n = 0; n < arr.length; n++){
    for (int x = 0; x < arr.length; x++){
      if ( (arr[n] + arr[x] == sumValue) && (n != x) ){ //need to ensure same slot is being added
        return true;
      }
    }
  }
  return false;
}

Hope this helps!

Andrew
  • 460
  • 4
  • 12