Task 1
You are given an implementation of a function:
function solution(A);
that, for a given array A
of N
integers, returns the smallest non-negative product of two elements of A
(both elements must be from different positions in A
).
Example
For A = [1, 4, 4]
, the function should return 4
.
The attached code is still incorrect for some inputs. Despite the error(s), the code may produce a correct answer for some test cases.
Your task is to find and fix the bug(s) in the implementation. You can add or modify a maximum of two lines.
Assumptions
N
is an integer within the range[3..1,000]
.- Each element of
A
is an integer within the range[-1,000..1,000]
.
Notes
- Focus on correctness in your solution.
- Performance is not the focus of the assessment.
Copyright 2009–2025 by Codility Limited. Unauthorized copying, publication, or disclosure prohibited.
Task 2
You are given an array A
of N
positive integers.
Problem Statement
Consider the following operation:
- Remove one element from
A
and compute the product of all the remaining array elements. - If there is only one element remaining in the array, the product is equal to that element.
For example, if A = [9, 16, 4]
, we can:
- Remove
9
and get a product equal to16 * 4 = 64
- Remove
16
and get a product equal to9 * 4 = 36
- Remove
4
and get a product of9 * 16 = 144
As seen in the example above, the product might change depending on which element we remove.
Function Description
Write a function:
function solution(A);
that, given an array A
of N
integers, returns the number of different products that can be obtained by removing exactly one element from A
.
Examples
- Given
A = [9, 16, 4]
, the function should return 3.- The achievable products are 64, 36, and 144.
- Given
A = [3, 4, 2, 3, 1]
, the function should return 4. - Given
A = [1000000000, 1000000000]
, the function should return 1.
Constraints
Write an efficient algorithm to handle large input sizes.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.
