I am working on a code challenge problem -- "find lucky triples". "Lucky triple" is defined as "In a list lst
, for any combination of triple like (lst[i], lst[j], lst[k]) where i < j < k
, where lst[i] divides lst[j]
and lst[j] divides lst[k]
.
My task is to find the number of lucky triples in a given list. The brute force way is to use three loops but it takes too much time to solve the problem. I wrote this one and the system respond "time exceed". The problems looks silly and easy but the array is unsorted so general methods like binary search do not work. I am stun in the problem for one day and hope someone can give me a hint. I am seeking a way to solve the problem faster, at least the time complexity should be lower than O(N^3).
A simple dynamic programming-like algorithm will do this in quadratic time and linear space. You just have to maintain a counter c[i]
for each item in the list, that represents the number of previous integers that divides L[i]
.
Then, as you go through the list and test each integer L[k]
with all previous item L[j]
, if L[j]
divides L[k]
, you just add c[j]
(which could be 0) to your global counter of triples, because that also implies that there exist exactly c[j]
items L[i]
such that L[i]
divides L[j]
and i < j
.
int c[] = {0}
int nbTriples = 0
for k=0 to n-1
for j=0 to k-1
if (L[k] % L[j] == 0)
c[k]++
nbTriples += c[j]
return nbTriples
There may be some better algorithm that uses fancy discrete maths to do it faster, but if O(n^2) is ok, this will do just fine.
In regard to your comment:
Why DP? We have something that can clearly be modeled as having a left to right order (DP orange flag), and it feels like reusing previously computed values could be interesting, because the brute force algorithm does the exact same computations a lot of times.
How to get from that to a solution? Run a simple example (hint: it should better be by treating input from left to right). At step i
, compute what you can compute from this particular point (ignoring everything on the right of i), and try to pinpoint what you compute over and over again for different i's
: this is what you want to cache. Here, when you see a potential triple at step k
(L[k] % L[j] == 0
), you have to consider what happens on L[j]
: "does it have some divisors on its left too? Each of these would give us a new triple. Let's see... But wait! We already computed that on step j
! Let's cache this value!" And this is when you jump on your seat.
Full working solution in python:
c = [0] * len(l)
print c
count = 0
for i in range(0,len(l)):
j=0
for j in range(0, i):
if l[i] % l[j] == 0:
c[i] = c[i] + 1
count = count + c[j]
print j
print c
print count
Read up on the Sieve of Eratosthenes, a common technique for finding prime numbers, which could be adapted to find your 'lucky triples'. Essentially, you would need to iterate your list in increasing value order, and for each value, multiply it by an increasing factor until it is larger than the largest list element, and each time one of these multiples equals another value in the list, the multiple is divisible by the base number. If the list is sorted when given to you, then the i < j < k requirement would also be satisfied.
e.g. Given the list [3, 4, 8, 15, 16, 20, 40]
:
Start at 3
, which has multiples [6, 9, 12, 15, 18 ... 39]
within the range of the list. Of those multiples, only 15
is contained in the list, so record under 15
that it has a factor 3
.
Proceed to 4
, which has multiples [8, 12, 16, 20, 24, 28, 32, 36, 40]
. Mark those as having a factor 4.
Continue through the list. When you reach an element that has an existing known factor, then if you find any multiples of that number in the list, then you have a triple. In this case, for 16
, this has a multiple 32
which is in the list. So now you know that 32
is divisible by 16
, which is divisible by 4
. Whereas for 15
, that has no multiples in the list, so there is no value that can form a triplet with 3
and 15
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With