Advertisement
CLooker

Untitled

Jan 14th, 2018
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)
  2.  
  3. Sample Input
  4. 1 2 3 4 5
  5.  
  6. Sample Output
  7. 10 14*/
  8. function miniMaxSum(arr) {
  9.   const maxAndMin = arr.reduce((newArr, n, index) => {
  10.     return newArr.concat(arr.filter((num, ind) => index !== ind).reduce((total, curr) => total += curr, 0));
  11.   }, []).reduce((maxAndMin, n, index) => {
  12.     index === 0 ? (maxAndMin.max = n, maxAndMin.min = n) : null;
  13.     n > maxAndMin.max ? maxAndMin.max = n : null;
  14.     n < maxAndMin.min ? maxAndMin.min = n : null;
  15.     return maxAndMin;
  16.   }, {
  17.     min: null,
  18.     max: null
  19.   });
  20.   console.log(`${maxAndMin.min} ${maxAndMin.max}`);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement