Given an array with numbers, how can I write a recursive function that finds indexes in an array when 2 elements are added to the target?

Here's a tip: Given an array of integers, return the indices of the two numbers so that they are added to the specific target.

You can assume that each input will have exactly one solution.

Example: Given nums = [2, 11, 15, 7], target = 9,

Since nums [0] + nums [1] = 2 + 7 = 9, return [0, 1].

Here is my solution, but it does not seem to give the expected result:

var sumTarget = function(array, target) {
  var result = [];
  var copy = array.slice();
  var firstValue = array.shift();
  if (array.length === 0) {
    return result;
  }
  for (var i = copy.indexOf(firstValue) + 1; i < copy.length; i++) {
    if (firstValue + copy[i] === target) {
      Array.prototype.push.apply(result, [copy.indexOf(firstValue), i]);
    }
  }

  return sumTarget(array, target);
};
Run code
+4
source share
3 answers

Something like that:

https://jsfiddle.net/rqp93gpy/2/

function getIndexes(arr, target, offset) {
  var result = [], i;
  if (arr.length <= 1) return [];

  if (offset === undefined) offset = 0;

  for (i = 1; i < arr.length; i++) {
    if (arr[0] + arr[i] === target) {
      result.push([offset, offset + i]);
    }
  }
  return result.concat(getIndexes(arr.slice(1), target, offset + 1));
}

console.log(JSON.stringify(getIndexes([2, 11, 15, 7, 6, 3, 4, 8, 9, 5, 7], 9),
                           null, 4));

output:

[
    [
        0,
        3
    ],
    [
        0,
        10
    ],
    [
        4,
        5
    ],
    [
        6,
        9
    ]
]
+2
source

, , , , , . 1,5 repl.it, 2,5 Opera, 8 Firefox 25 Chrome.

:

  • , .
  • , - (LUT), , .
  • ,

,

var arr = [4,6,12,5,8,4,3,9,19,5,21,13,8,15,7,23,6,11,10,15,1,12,19,31,14,6,3,16],
    tar = 12;

function getIndexes(arr, target){
var   odds = {},
     evens = {},
   results = [],
makeResult = (a,b) => !!b ? a.forEach( e => b.forEach( f => results.push([e,f])))
                          : a.reduce((p,c,i) => {makeResult([p],a.slice(i)); return c});
arr.forEach((e,i) => e < target ? e%2 == 1 ?  !!odds[e] ?  odds[e].push(i) : ( odds[e] = [],  odds[e].push(i))
                                           : !!evens[e] ? evens[e].push(i) : (evens[e] = [], evens[e].push(i))
                                : false);
var oko = Object.keys(odds),
    oke = Object.keys(evens);
target%2 == 1 ? oko.length <= oke.length ? oko.forEach( e => evens[target-e] && makeResult( odds[e], evens[target-e]))
                                         : oke.forEach( e =>  odds[target-e] && makeResult(evens[e],  odds[target-e]))
              : (oko.forEach( e => (e <= target/2 &&  odds[target-e]) && (e < target/2 ? makeResult( odds[e],  odds[target-e])
                                                                                       : makeResult( odds[e]))),
                 oke.forEach( e => (e <= target/2 && evens[target-e]) && (e < target/2 ? makeResult(evens[e], evens[target-e])
                                                                                       : makeResult(evens[e]))));
return results;
}
document.write('<pre>' + JSON.stringify(getIndexes(arr, tar), 0, 2) + '</pre>');

, , makeResults .

https://repl.it/CIxd/1 vs https://repl.it/CIxr

0

Vote for this:

    function sumTarget(ar, t) {
        var res = [];
        for (var i = 0, n = ar.length; i < n-1; i++) {
            for (var j = i + 1; j < n; j++) {
                if (ar[i] + ar[j] == t) {
                    res.push({ num1: i, val1: ar[i], num2: j, val2: ar[j] });
                }
            }
        }
        console.log(JSON.stringify(res));
    }
    sumTarget([2, 11, 15, 7], 9);

I hope this helps in your class;)

0
source

All Articles