Instead of using three loops with complexity O(n^3), you can use modular exponentiation as the third loop which will take O(log n) to compute power k for the difference of each pair of the array.

I want to add in this question how can we compute summation of differences of each pair raise to power k in less than O(n^2 log n).

15,918,516 members

That being said, you should also post your code. We are quite happy to review your solution and suggest fixes, but we don't provide solutions outright.