Tasks Details
easy
Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6contains the following example triplets:
- (0, 1, 2), product is −3 * 1 * 2 = −6
- (1, 2, 4), product is 1 * 2 * 5 = 10
- (2, 4, 5), product is 2 * 5 * 6 = 60
Your goal is to find the maximal product of any triplet.
Write a function:
int solution(vector<int> &A);
that, given a non-empty array A, returns the value of the maximal product of any triplet.
For example, given array A such that:
A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6the function should return 60, as the product of triplet (2, 4, 5) is maximal.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [3..100,000];
- each element of array A is an integer within the range [−1,000..1,000].
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 16:18:17 UTC,
java,
autosave
Code: 16:18:20 UTC,
cpp,
autosave
Code: 16:18:24 UTC,
cpp,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1] && count == A.size())
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Code: 16:18:45 UTC,
cpp,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Code: 16:19:15 UTC,
cpp,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
if(count == A.size())
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Code: 16:19:34 UTC,
cpp,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
if(count == A.size())
return arr[0] * arr[1] * arr[2];
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Code: 16:19:40 UTC,
cpp,
verify,
result: Passed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
if(count == A.size())
return arr[0] * arr[1] * arr[2];
else
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Analysis
Code: 16:19:46 UTC,
cpp,
verify,
result: Passed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
if(count == A.size())
return arr[0] * arr[1] * arr[2];
else
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Analysis
Code: 16:19:49 UTC,
cpp,
final,
score: 
100
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
void sort_Arr(int* arr){
for(int i = 0; i < 2; i++)
if(arr[i] > arr[i + 1])
arr[i] ^= arr[i + 1] ^= arr[i] ^= arr[i + 1];
if(arr[0] > arr[1])
arr[0] ^= arr[1] ^= arr[0] ^= arr[1];
}
int solution(vector<int> &A) {
// write your code in C++14 (g++ 6.2.0)
int arr[3] = {A.at(0), A.at(1), A.at(2)};
int M_min = A.at(0), M_max = A.at(1);
int count = 0;
if(A.size() == 3)
return A.at(0) * A.at(1) * A.at(2);
else
sort_Arr(arr);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
if(M_min > A.at(2))
M_min = A.at(2);
if(M_max > M_min)
M_min ^= M_max ^= M_min ^= M_max;
for(int i = 3; i < A.size(); i++){
if(arr[0] < A.at(i)){
arr[0] = A.at(i);
sort_Arr(arr);
}
if(M_min > A.at(i)){
M_min = A.at(i);
if(M_min < M_max)
M_min ^= M_max ^= M_min ^= M_max;
}
}
for(int i = 0; i < A.size(); i++)
if(A.at(i) < 0)
count++;
if(M_min * M_max > arr[0] * arr[1])
if(count == A.size())
return arr[0] * arr[1] * arr[2];
else
return M_min * M_max * arr[2];
else
return arr[0] * arr[1] * arr[2];
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
expand all
Performance tests
1.
0.001 s
OK
1.
0.001 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
2.
0.008 s
OK