Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ((arr) => {
- arr.sort((a, b) => a - b);
- let i = 0;
- let max = null;
- let res = 0;
- for (; i < arr.length && arr[i] < 0; ++i) {
- if (max == null) max = arr[i];
- else {
- res += max * arr[i];
- max = null;
- }
- }
- for (; i < arr.length && arr[i] === 0; ++i) {
- if (max != null) max = null;
- }
- if (max != null) res += max;
- for (let j = arr.length; i <= j; --j) {
- if (max == null) {
- max = arr[j];
- } else {
- res += max * arr[j];
- max = null;
- }
- }
- if (max != null) res += max;
- return res;
- })([-5,0,1,2,3,4,5]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement