Skip to content

Commit 5be4a13

Browse files
Merge pull request #76 from wakidurrahman/feat/problem-solving-02
feat: compareTripletsVariationOne
2 parents fa4ca3a + 06d21de commit 5be4a13

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/hackerrank/problem-solving/algorithms-001-001.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,56 @@ function simpleArraySum(ar: number[]): number {
2121
// Return the sum of the array
2222
return sum;
2323
}
24+
25+
// 003: compare the triplets
26+
27+
/*
28+
* Complete the 'compareTriplets' function below.
29+
*
30+
* The function is expected to return an INTEGER_ARRAY.
31+
* The function accepts following parameters:
32+
* 1. INTEGER_ARRAY a
33+
* 2. INTEGER_ARRAY b
34+
*/
35+
36+
function compareTriplets(a: number[], b: number[]): number[] {
37+
const score: number[] = [];
38+
let pointOfAlice: number = 0;
39+
let pointOfBob: number = 0;
40+
// Alice and Bob
41+
// input a = [1, 2, 3] , b = [3, 4, 1]
42+
// comparison a[i] > b[i], a[i] < b[i] , a[i] == b[i]
43+
// retrun array [1, 1] pointOfAlice and Bob score position
44+
45+
for (let i = 0; i <= 2; i++) {
46+
if (a[i] == b[i]) {
47+
pointOfAlice;
48+
pointOfBob;
49+
} else if (a[i] > b[i]) {
50+
pointOfAlice += 1;
51+
} else if (a[i] < b[i]) {
52+
pointOfBob += 1;
53+
}
54+
}
55+
56+
return score.concat(pointOfAlice, pointOfBob);
57+
}
58+
59+
function compareTripletsVariationOne(a: number[], b: number[]): number[] {
60+
let pointOfAlice: number = 0;
61+
let pointOfBob: number = 0;
62+
// Alice and Bob
63+
// input a = [1, 2, 3] , b = [3, 4, 1]
64+
// comparison a[i] > b[i], a[i] < b[i] , a[i] == b[i]
65+
// retrun array [1, 1] pointOfAlice and Bob score position
66+
67+
for (let i = 0; i < a.length; i++) {
68+
if (a[i] > b[i]) {
69+
pointOfAlice += 1;
70+
} else if (a[i] < b[i]) {
71+
pointOfBob += 1;
72+
}
73+
}
74+
75+
return [pointOfAlice, pointOfBob];
76+
}

0 commit comments

Comments
 (0)