Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
5
Indexable
const readline = require('readline');

const twoSum = (arr, B) => {
    let left = 0;
    let right = arr.length - 1;
    
    while (left < right) {
        const sum = arr[left] + arr[right];
        if (sum === B) {
            return [left, right];
        } else if (sum < B) {
            left++;
        } else {
            right--;
        }
    }
    
    return [-1, -1];
};

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const inputLines = [];
rl.on('line', (line) => {
    inputLines.push(line);
});

rl.on('close', () => {
    const T = parseInt(inputLines[0], 10);
    let index = 1;
    const results = [];
    
    for (let t = 0; t < T; t++) {
        const [N, B] = inputLines[index++].split(' ').map(Number);
        const arr = inputLines[index++].split(' ').map(Number);
        const result = twoSum(arr, B);
        results.push(result.join(' '));
    }
    
    console.log(results.join('\n'));
});

// To run this code, save it in a file named twoSum.js and execute it with Node.js:
// $ node twoSum.js
Editor is loading...
Leave a Comment