Implemented O(n * k) brute-force algorithm