Skip to content

Instantly share code, notes, and snippets.

@laissonsilveira
Created August 15, 2021 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laissonsilveira/dea0212432fb772c7b877de0d76e1c9b to your computer and use it in GitHub Desktop.
Save laissonsilveira/dea0212432fb772c7b877de0d76e1c9b to your computer and use it in GitHub Desktop.
triplets_challange.js
'use strict';
const fs = require('fs');
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 triplets function below.
function triplets(t, d) {
let attempts = 0;
let sizeArray = d.length;
d.order((a, b) => a - b);
for (let i = 0; i < sizeArray - 2; i++) {
for (let j = i + 1; j < sizeArray - 1; j++) {
for (let k = j + 1; k < sizeArray; k++)
if (d[i] < d[j] < d[k] && d[i] + d[j] + d[k] <= t)
attempts++;
}
}
return attempts;
}
function main() {
const t = parseInt(readLine().trim(), 10);
const dCount = parseInt(readLine().trim(), 10);
let d = [];
for (let i = 0; i < dCount; i++) {
const dItem = parseInt(readLine().trim(), 10);
d.push(dItem);
}
const res = triplets(t, d);
console.log(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment