Home > Net >  Identifying the index of array against the target value
Identifying the index of array against the target value

Time:01-07

One of the challanging question I got in office, which i could not able to come out of it. need the help here.

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. need the help.

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);  

CodePudding user response:

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);

CodePudding user response:

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/)

CodePudding user response:

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.

  • Related