I came across this question on an interview website - We are given 4 numbers say n1, n2, n3, n4. We can place them in any order and we can use the mathematical operators +, -, *, / in between them to have the final result as 24. Write an algorithm for this - it will take 4 numbers and return false or true whether final result 24 is possible with any combination. The same operator can be used multiple times.

One of the ways to do this would be to -

  1. Permute the operators
  2. Permute the operands
  3. Apply every permutation in 2. to every permutation in 1.

This solution would be brute force and would not be an optimal solution. I think there might be a better solution using binary search trees.

Edit
Report