Answer the question
In order to leave comments, you need to log in
Problems from interviews by maximum numbers: how to solve?
There is such a task:
you need to write a function that takes an array of numbers as input (unsorted, let's say integers), also returns an array that contains:
- the maximum number A1
- three other maximum numbers A2, A3, A4, which, when multiplied, give A1
Started writing function with max selection and sorting, then I started writing nested loops, but I realized that there would be too many conditions and checks.
There is an idea that regular expressions can help here, but I don’t know yet how to implement it.
Answer the question
In order to leave comments, you need to log in
Sort in descending order and two cycles - not kosher?
const maxprod = arr => {
const a = arr.slice().sort((a, b) => b - a);
const max = a[0];
const len = a.length;
let iter = 0;
for (let i = 1; i < len - 2; i++) {
iter++;
const A2 = a[i];
const x2 = max / A2;
if (!Number.isInteger(x2)) continue;
for (let j = i + 1; j < len - 1; j++) {
iter++;
const A3 = a[j];
const x3 = x2 / A3;
if (!Number.isInteger(x3)) continue;
if (!!~a.indexOf(x3)) {
return [max, A2, A3, x3, iter]);
}
}
}
return false;
}
indexOf()
всё равно перебирает массив.[
[20,5,3,2,2], // [ 20, 5, 2, 2, 3 ]
[7,9,4,60,5,3,2,2], // [ 60, 5, 4, 3, 4 ]
[1,2,3,199], // false
[2430,2431,2431,2431,1,1,1,2,3,5,7,9,11,13,15,17,19,23], // [ 2431, 17, 13, 11, 8 ]
].forEach(test => console.log(test, maxprod(test)));
regular expressions will not help you in this task
, read about this type of tasks here
adamos ,
2. Factorize A1 (this is much faster than iterating over the entire array into combinations of three elements).it all depends on the size of the numbers. for large numbers this can be a daunting task
let's say integers
whole numbers, positive
If I understand the problem correctly, my solution is:
#include <iostream>
#include <windows.h>
#include <vector>
std::vector<int> DoTheJob(std::vector<int> v)
{
int tmp;
std::vector<int> result;
bool flagfound = false;
//sorting
for(int i=0;i<v.size()-1;i++)
{
for (int j=i+1;j<v.size();j++)
{
if(v[j]>v[i])
{
tmp=v[i];
v[i]=v[j];
v[j]=tmp;
};
};
};
//sorting_end
result.push_back(v[0]);
for(int i=1; i<v.size()-2;i++)
{
if (flagfound)
{
break;
}
for(int j=i+1;j<v.size()-1;j++)
{
if (flagfound)
{
break;
};
for(int k=j+1;k<v.size();k++)
{
if (flagfound)
{
break;
};
if(v[0]==v[i]*v[j]*v[k])
{
result.push_back(v[i]);
result.push_back(v[j]);
result.push_back(v[k]);
flagfound = true;
}
};
};
}
return result;
};
int main()
{ int q = 10;
std::vector<int> v;
for (int i=0;i<q;i++)
{
v.push_back((GetTickCount()+rand())%10);
std::cout<< v[i]<<" ";
};
std::cout<<std::endl;
std::vector<int> solution = DoTheJob(v);
std::cout << "Max element: "<<solution[0];
if (solution.size()==4)
{
std::cout<<", multipliers: "<<solution[1]<<", "<< solution[2]<<", "<<solution[3];
}
else
std::cout<< ", no multipliers.";
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question