1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
void sortElem(const std::vector<int>& A, std::vector<size_t>& indices) {
size_t sz{ A.size() };
int m1{ -1000 }, m2{ m1 }, m3{ m1 }, m4{ m1 }, m5{ m1 }, m6{ m1 };
size_t i1{ 0 }, i2{ 0 }, i3{ 0 }, i4{ 0 }, i5{ 0 }, i6{ 0 };
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m1) {
m1 = abs(A[i]);
i1 = i;
}
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m2 && i != i1) {
m2 = abs(A[i]);
i2 = i;
}
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m3 && i != i1 && i != i2) {
m3 = abs(A[i]);
i3 = i;
}
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m4 && i != i1 && i != i2 && i != i3) {
m4 = abs(A[i]);
i4 = i;
}
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m5 && i != i1 && i != i2 && i != i3 && i != i4) {
m5 = abs(A[i]);
i5 = i;
}
for (size_t i = 0; i < sz; ++i)
if (abs(A[i]) > m6 && i != i1 && i != i2 && i != i3 && i != i4 && i != i5) {
m6 = abs(A[i]);
i6 = i;
}
indices.push_back(i1);
indices.push_back(i2);
indices.push_back(i3);
indices.push_back(i4);
indices.push_back(i5);
indices.push_back(i6);
}
void multiplySum(const int elem, const std::vector<int>& bigThreeElemVec, std::vector<int>& sumVec) {
sumVec.push_back(elem * bigThreeElemVec[0] * bigThreeElemVec[1]);
sumVec.push_back(elem * bigThreeElemVec[0] * bigThreeElemVec[2]);
sumVec.push_back(elem * bigThreeElemVec[1] * bigThreeElemVec[2]);
}
int solution(std::vector<int>& A) {
const size_t range{ 3 };
if (A.size() == range) // Only 3 elements
return A[0] * A[1] * A[2];
std::vector<size_t> indices;
if (A.size() > range * 2) {
sortElem(A, indices);
bool neg{ true };
for (size_t i = 0; i < range * 2; ++i)
if (A[indices[i]] >= 0)
neg = false;
if (neg) { // all 6 elements are negative
auto it{ std::max_element(A.begin(), A.end()) };
if (*it >= 0)
A[indices[indices.size() - 1]] = *it;
}
}
else
for (size_t i = 0; i < A.size(); ++i)
indices.push_back(i);
std::vector<int> firstBigThreeElem, secondBigThreeElem;
for (size_t i = 0; i < indices.size(); i++)
if (i <= 2)
firstBigThreeElem.push_back(A[indices[i]]);
else
secondBigThreeElem.push_back(A[indices[i]]);
std::vector<int> sumVec;
sumVec.push_back(firstBigThreeElem[0] * firstBigThreeElem[1] * firstBigThreeElem[2]);
if(secondBigThreeElem.size() == range)
sumVec.push_back(secondBigThreeElem[0] * secondBigThreeElem[1] * secondBigThreeElem[2]);
for (size_t i = 0; i < secondBigThreeElem.size(); ++i)
multiplySum(secondBigThreeElem[i], firstBigThreeElem, sumVec);
if(secondBigThreeElem.size() == range)
for (size_t i = 0; i < range; ++i)
multiplySum(firstBigThreeElem[i], secondBigThreeElem, sumVec);
else if(secondBigThreeElem.size() == range -1)
for (size_t i = 0; i < range; ++i)
sumVec.push_back(firstBigThreeElem[i] * secondBigThreeElem[0] * secondBigThreeElem[1]);
return *max_element(sumVec.begin(), sumVec.end());
}
|