Skip to content

Instantly share code, notes, and snippets.

@Ghanshyam-K-Dobariya
Last active July 20, 2024 04:32
Show Gist options
  • Save Ghanshyam-K-Dobariya/6cf2e3340872a53a8b7e83350a62275f to your computer and use it in GitHub Desktop.
Save Ghanshyam-K-Dobariya/6cf2e3340872a53a8b7e83350a62275f to your computer and use it in GitHub Desktop.
// Executed at https://www.tutorialspoint.com/execute_nodejs_online.php
// https://codedamn.com/online-compiler/node
const I1 = [100, 20, 30, 40];
const T1 = 50;
const I2 = [];
for (let i = 1; i < 50000; i++) {
I2.push(i);
}
const T2 = -1;
const I3 = I2;
const T3 = 49000 + 50000;
function findPairSL1(ar1, target) {
let missingNumber;
for (let i = 0; i< ar1.length; i++){
missingNumber = target - ar1[i];
if (ar1.includes(missingNumber)) {
return [ar1[i], missingNumber];
}
}
return [];
}
console.time("Input 1");
console.log('Output 1 -> ', findPairSL1(I1, T1));
console.timeEnd("Input 1");
console.time("Input 2");
console.log('Output 2 -> ', findPairSL1(I2, T2));
console.timeEnd("Input 2");
console.time("Input 3");
console.log('Output 3 -> ', findPairSL1(I3, T3));
console.timeEnd("Input 3");
console.log('\n\n\n');
function findPairSL2(ar1, target) {
for (let i = 0; i< ar1.length - 1; i++){
for (let j = i + 1; j < ar1.length; j++) {
if (ar1[i] + ar1[j] === target) {
return [ar1[i] , ar1[j]];
}
}
}
return [];
}
console.time("Input 1");
console.log('Output 1 -> ', findPairSL2(I1, T1));
console.timeEnd("Input 1");
console.time("Input 2");
console.log('Output 2 -> ', findPairSL2(I2, T2));
console.timeEnd("Input 2");
console.time("Input 3");
console.log('Output 3 -> ', findPairSL2(I3, T3));
console.timeEnd("Input 3");
function findPairSL3(ar1, target) {
const keyValueMap = {};
for (let i = 0; i< ar1.length - 1; i++){
keyValueMap[ar1[i]] = ar1[i];
}
let num2;
for (let i = 0; i< ar1.length - 1; i++){
num2 = target - ar1[i];
if (keyValueMap[num2] !== undefined) {
return[ar1[i], num2];
}
}
return [];
}
console.log('\n\n\n');
console.time("Input 1 - Effective");
console.log('Output 1 -> ', findPairSL3(I1, T1));
console.timeEnd("Input 1 - Effective");
console.time("Input 2 - Effective");
console.log('Output 2 -> ', findPairSL3(I2, T2));
console.timeEnd("Input 2 - Effective");
console.time("Input 3 - Effective");
console.log('Output 3 -> ', findPairSL3(I3, T3));
console.timeEnd("Input 3 - Effective");
console.log('\n\n\n');
function findPairSL4(ar1, target) {
const keyValueMap = {};
let num2;
for (let i = 0; i< ar1.length - 1; i++){
keyValueMap[ar1[i]] = ar1[i];
num2 = target - ar1[i];
if (keyValueMap[num2] !== undefined) {
return[ar1[i], num2];
}
}
return [];
}
console.time("Input 1 - findPairSL4");
console.log('Output 1 -> ', findPairSL4(I1, T1));
console.timeEnd("Input 1 - findPairSL4");
console.time("Input 2 - findPairSL4");
console.log('Output 2 -> ', findPairSL4(I2, T2));
console.timeEnd("Input 2 - findPairSL4");
console.time("Input 3 - findPairSL4");
console.log('Output 3 -> ', findPairSL4(I3, T3));
console.timeEnd("Input 3 - findPairSL4");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment