Home > Software engineering >  write a function that would loop through an array and get the two(or more) indexes that satisfy a co
write a function that would loop through an array and get the two(or more) indexes that satisfy a co

Time:10-02

Consider that we have an array of length n being filled with random numbers, i want to write a function that would loop through an array and get the two(or more) indexes that satisfy a condition together.

For instance:

 const arr = [12, 10, 2, 3, 17, 42, 56, 38]

write a function to get the two indexes such that if you multiplied their elements, it would bring out the largest answer. (56*42 would be the answer in this scenario)

I understand that in this case, you could just multiply the largest array element with the second largest, however a question of this nature could be much more complex and there could be much more conditions to solve for and the elements in the array could also be unknown. i am merely after the principle of the answer. If you could provide a solution to this question whilst briefly explaining how you came across your answer, it would be much appreciated.

CodePudding user response:

When you know, that your accumulation function is steady, then you can simply order the array and return the indices of the two largest elements. If you do not know, that the accumulation function is steady, then you can pairwise calculate the result of such function and, if the result is larger than your current cumulated value, remember the indices of the elements, which generated this value.

A function like this one can solve the problem with all accumulation functions passed as parameter:

const findHighestIndices = (values, accumlationFunction) => {
  let currentMaximum = -Infinity;
  let indices = [];
  for (let i = 0; i < values.length; i  ) {
    for (let j = 0; j < values.length; j  ) {
       if (i === j) continue; // we only want to compare DIFFERENT elements
       const cummulation = accumlationFunction(values[i], values[j]);
       if (cummulation <= currentMaximum) continue;
       currentMaximum = cummulation;
       indices = [i, j];
     }
   }
   return indices;
};


    const arr = [12, 10, 2, 3, 17, 42, 56, 38];

    console.log(findHighestIndices(arr, (a, b) => a * b));

When there is no order of parameters of the accumulation function, my implementation is highly optimizable, as you won't need to run the elements for both, i and j.

CodePudding user response:

Not a native javascript developer, hope this can help.

I would loop over the list the number of indices expected to statisfy the condition and test the condition. Since, you don't want to use the same index two times, pass in this case.

const arr = [12, 10, 2, 3, 17, 42, 56, 38]
var max = -Infinity;
var max_i = 0
var max_j = 0
for(var i=0; i< arr.length; i  ) {
for(var j=0; j< i 1; j  ){
if(i==j){
//pass
}
var mul = arr[i]*arr[j];
if (mul > max){max = mul;max_i = i; max_j = j}
}}

The resulting indices are max_i and max_j.

  • Related