Skip to content

Commit ede4aea

Browse files
authored
Merge pull request #297 from sir-gon/feature/count_triplets
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: C…
2 parents 6b3eab3 + bf9bfc4 commit ede4aea

File tree

1 file changed

+8
-8
lines changed
  • algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps

1 file changed

+8
-8
lines changed

algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/CountTriplets.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ static long countTriplets(List<Long> arr, long r) {
3232
}
3333
}
3434

35-
long jItem;
36-
long kItem;
35+
long prevItemCount;
36+
long nextItemCount;
37+
Long currentItemCount;
3738

3839
for (Long item : arr) {
3940
Long j = item / r;
4041
Long k = item * r;
4142

4243
aCounter.put(item, aCounter.get(item) - 1L);
4344

44-
jItem = bCounter.get(j) != null ? bCounter.get(j) : 0L;
45-
kItem = aCounter.get(k) != null ? aCounter.get(k) : 0L;
45+
prevItemCount = bCounter.get(j) != null ? bCounter.get(j) : 0L;
46+
nextItemCount = aCounter.get(k) != null ? aCounter.get(k) : 0L;
4647
if (item % r == 0) {
47-
triplets += jItem * kItem;
48+
triplets += prevItemCount * nextItemCount;
4849
}
4950

50-
Long bItem = bCounter.get(item);
51-
52-
bCounter.put(item, bItem != null ? bItem + 1L : 1L);
51+
currentItemCount = bCounter.get(item);
52+
bCounter.put(item, currentItemCount != null ? currentItemCount + 1L : 1L);
5353
}
5454

5555
return triplets;

0 commit comments

Comments
 (0)