-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_maxsum.js
57 lines (44 loc) · 1.23 KB
/
min_maxsum.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// boiler plate for js
'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++];
}
/*
* Complete the 'miniMaxSum' function below.
*
* The function accepts INTEGER_ARRAY arr as parameter.
*/
// actual code for problem statement
function miniMaxSum(arr) {
// Write your code
// wee have to find min sum and max sum
//it is simple
//- for finding the min sum remove the max element from array and add all the remaining element
// -for max sum remove the minimum element and fdo the sum of remaining element
let max=-Infinity
let min=Infinity;
let sum=0
let sumarr=[]
for(let i=0;i<arr.length;i++){
sum=sum+arr[i]
max=Math.max(arr[i],max)
min=Math.min(arr[i],min)
}
sumarr.push(sum-max,sum-min)
console.log(sumarr.join(" "))
}
function main() {
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
miniMaxSum(arr);
}