Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e866f1f

Browse files
#contests
1 parent 841abfb commit e866f1f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

‎maximum-sum-10-1.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//https://www.hackerrank.com/contests/hack-the-interview-vi-asia-pacific/challenges/maximum-sum-10-1
2+
'use strict';
3+
4+
const fs = require('fs');
5+
6+
process.stdin.resume();
7+
process.stdin.setEncoding('utf-8');
8+
9+
let inputString = '';
10+
let currentLine = 0;
11+
12+
process.stdin.on('data', function(inputStdin) {
13+
inputString += inputStdin;
14+
});
15+
16+
process.stdin.on('end', function() {
17+
inputString = inputString.split('\n');
18+
19+
main();
20+
});
21+
22+
function readLine() {
23+
return inputString[currentLine++];
24+
}
25+
26+
/*
27+
* Complete the 'performOperations' function below.
28+
*
29+
* The function is expected to return a LONG_INTEGER_ARRAY.
30+
* The function accepts following parameters:
31+
* 1. INTEGER N
32+
* 2. INTEGER_ARRAY op
33+
*/
34+
function performOperations(N, op) {
35+
let out = [];
36+
let first = 1, last = N;
37+
let sum = Math.floor( ((N-2) * (N+1)) / 2);
38+
for(let i =0;i<op.length;i++){
39+
let not_between = ( 1 < op[i] && op[i] < N);
40+
if( first == op[i] || last == op[i] || not_between) {
41+
let t = first;
42+
first = last;
43+
last = t;
44+
}else {
45+
last = op[i];
46+
}
47+
out.push(sum + first +last)
48+
}
49+
return out;
50+
}
51+
52+
function main() {
53+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
54+
const N = parseInt(readLine().trimEnd(),10);
55+
let M = parseInt(readLine().trimEnd(),10);
56+
const op = []
57+
while(M--){
58+
op.push(parseInt(readLine().trimEnd(),10));
59+
}
60+
const result = performOperations(N, op);
61+
ws.write(result.join('\n') + '\n');
62+
ws.end();
63+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /