0

I encountered a challenging question in my office, which I could not solve.

const array = [2, 7, 11, 15], target = 9;

In the above I have an array and target as 9, as well this target can change any of value as 18,26 like so. The result should show the indexOf array which used for get the target. For example at present it is 9, so the result should be [0,1] (2+7). If the target is 26 then result should be [2,3]. How to achieve this?

For my try the first attempt is working. but rest of them not. What can I try next?

My code:

const array = [2, 7, 11, 15], target = 9;
const result = [];
const outPut = array.reduce((c,v,i,a) => {
  if(c !== target && c < target) {
    result.push(a.indexOf(v));
  }
  return c + v;
}, 0);

console(result);  
halfer
  • 19,824
  • 17
  • 99
  • 186
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • 3
    You want to find a subset of the array whose elements sum up to the target? Does it have to be exactly two elements? Are the elements always sorted? What if the sum is not possible? Can an element be chosen more than once? – Wyck Jan 06 '23 at 15:15
  • @Wyck - yes you are correct. nothing sets it can be return as 0 or false – 3gwebtrain Jan 06 '23 at 15:21
  • https://en.wikipedia.org/wiki/Subset_sum_problem – 0stone0 Jan 06 '23 at 15:27
  • Does this answer your question? [Return all subsets whose sum is a given value (subset sum problem)](https://stackoverflow.com/questions/53659151/return-all-subsets-whose-sum-is-a-given-value-subset-sum-problem) – 0stone0 Jan 06 '23 at 15:27

3 Answers3

1

Here's the brute force solution:

  • Get all the subsets of the array.
  • Compute the sum of each subset.
  • Filter the subsets to those whose sum is the target.

const array = [1, 2, 7, 8, 9, -2];
const target = 9;

const getAllSubsets = array => array.reduce(
  (subsets, value) => subsets.concat(subsets.map(set => [...set, value])),
  [[]]
);

const subsets = getAllSubsets(array);
const result = subsets.filter(
  subset => subset.reduce((partialSum, element) => partialSum + element, 0) == target
);
console.log(result);

This example produces all the subsets of [1, 2, 7, 8, 9, -2] that sum to 9.

Output:

[ [ 2, 7 ], [ 1, 8 ], [ 9 ], [ 1, 2, 8, -2 ], [ 2, 9, -2 ] ]

You only need to make two small changes to make this work with indices instead of the actual values:

  • get all subsets of array.map((_, i) => i) instead of array to get the indices
  • sum using array[element] instead of element.

const array = [1, 2, 7, 8, 9, -2];
const target = 9;

const getAllSubsets = array => array.reduce(
  (subsets, value) => subsets.concat(subsets.map(set => [...set, value])),
  [[]]
);

const subsets = getAllSubsets(array.map((_, i) => i));
const result = subsets.filter(
  subset => subset.reduce((partialSum, element) => partialSum + array[element], 0) == target
);
console.log(result);
Wyck
  • 10,311
  • 6
  • 39
  • 60
0

The problem with this approach is that you might end up adding the wrong elements; for example, if target was 13, your code would first add 2 and 7 and then not return the correct result because it wouldn't then consider adding 11 since that exceeds the target. Instead, you should use a two-pointer technique (see https://www.geeksforgeeks.org/two-pointers-technique/)

-1

const array = [2, 7, 11, 15];

let result = [];

const getResult = (target) => {
  for(let i =0; i < array.length;i++){
  let requiredValue = target - array[i];
  if(array.indexOf(requiredValue) > -1) {
    result = [array.indexOf(array[i]), array.indexOf(requiredValue)].sort();
  } 
  if(array.indexOf(requiredValue) < -1) {
    result = [0]; 
  } 
}
}
getResult(9)
console.log(result);
getResult(18)
console.log(result);
getResult(26)
console.log(result);

Here is my solution:

const array = [2, 7, 11, 15];
const target = 26;
let result = [];

for(let i =0; i < array.length;i++){
  let requiredValue = target - array[i];
  if(array.indexOf(requiredValue) > -1) {
    result = [array.indexOf(array[i]), array.indexOf(requiredValue)].sort();
  }
}

console.log(result);

it works for me. any one find the issue, pls comment. Change the target and play.

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247