0
$\begingroup$

I'm writing an algorithm which computes the solution for a pretty complex (for me) problem which I found online. I couldn't find a smart solution, so I decided to use the brute force.

BUT

as expected I get a really slow code. I would like to make it faster.

The core of the algorithm is this triple for-loop:

for(int i = a; i < n; i++)
    for(int j = i+1; j < n; j++)
        for(int k = j+1; k < n; k++)

But I know it can be made faster.

Each loop performs a commutative check on the three elements indexed by the indexes i, j and k. So if you do check(i,j,k) you can skip check(i,k,j), check(j,k,i), check(j,i,k) and so on.

I thought about this issue as a matrix (although this is 2D): matrix

Thank you very much

$\endgroup$
  • $\begingroup$ This question may be better suited for Stack Overflow (though on either site, it would be helpful to provide more context and maybe an example) $\endgroup$ – David M. May 19 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.