Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. ((arr) => {
  2. arr.sort((a, b) => a - b);
  3. let i = 0;
  4. let max = null;
  5. let res = 0;
  6. for (; i < arr.length && arr[i] < 0; ++i) {
  7. if (max == null) max = arr[i];
  8. else {
  9. res += max * arr[i];
  10. max = null;
  11. }
  12. }
  13. for (; i < arr.length && arr[i] === 0; ++i) {
  14. if (max != null) max = null;
  15. }
  16. if (max != null) res += max;
  17. for (let j = arr.length; i <= j; --j) {
  18. if (max == null) {
  19. max = arr[j];
  20. } else {
  21. res += max * arr[j];
  22. max = null;
  23. }
  24. }
  25. if (max != null) res += max;
  26.  
  27. return res;
  28. })([-5,0,1,2,3,4,5]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement