From 8f6c5009d5a1a2b9524bdabe644cea3f7199de44 Mon Sep 17 00:00:00 2001 From: "B.Vamsi Krishna" Date: Sat, 30 Oct 2021 08:22:41 -0700 Subject: [PATCH] Added checkGoodPairsInArray.js Given an array A and a integer B. A pair(i,j) in the array is a good pair if i!=j and (A[i]+A[j]==B). Check if any good pair exist or not. --- .../Javascript/checkGoodPairsInArray.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Algorithms/Javascript/checkGoodPairsInArray.js diff --git a/Algorithms/Javascript/checkGoodPairsInArray.js b/Algorithms/Javascript/checkGoodPairsInArray.js new file mode 100644 index 00000000..01ad53a1 --- /dev/null +++ b/Algorithms/Javascript/checkGoodPairsInArray.js @@ -0,0 +1,19 @@ +// Given an array A and a integer B. A pair(i,j) in the array is a good pair if i!=j and (A[i]+A[j]==B). Check if any good pair exist or not. + +function checkGoodPairsInArray(A, B){ + let arrASize = A.length; + // For every number traverse all numbers right to it + for(let i = 0; i <(arrASize - 1); i++){ + for(let j = (i + 1); j <(arrASize); j++){ + if((A[i] + A[j]) == B){ + return 1; + } + } + } + return 0; + } + +let A = [1,2,3,4]; +let B = 7; +let out = checkGoodPairsInArray(A,B); +console.log(out); \ No newline at end of file