forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMaxSum.js
More file actions
40 lines (29 loc) · 979 Bytes
/
MiniMaxSum.js
File metadata and controls
40 lines (29 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function miniMaxSum(arr) {
// Sort the array to get the smallest and largest elements easily
arr.sort((a, b) => a - b);
// Calculate the minimum sum (sum of the first four elements)
const minSum = arr.slice(0, 4).reduce((sum, num) => sum + num, 0);
// Calculate the maximum sum (sum of the last four elements)
const maxSum = arr.slice(1).reduce((sum, num) => sum + num, 0);
// Print the results
console.log(minSum, maxSum);
}
function main() {
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
miniMaxSum(arr);
}