I came up with a suitable solution to a HackerRank problem that failed because the execution took longer than 10 seconds on lists that were of very large size.
The problem: Given a list arr
and an integer d
, find all combinations of length 3 whose sum is divisible by d
My solution:
[
sum(p) % d
for p in combinations(arr, r=3)
].count(0)
I simply cannot find a way to optimize this (for time) any more. Can this be further optimized for speed?