General backtracking for a numbers' arrangement problem
Saw this problem on a Facebook post today: To write a code for it, we can use DFS + backtracking. We can actually make it general enough so that we can solve it for: 1/ Any set of digits 2/ Broken into any group of numbers 3/ Looking for any target The result for the puzzle above is this: 225 189 112 Code probably needs more boundary checks (overflows) as well as pruning of the search space, but for the most part it works. Code is down below, cheers, ACC. using System; using System.Collections; using System.Collections.Generic; using System.Numerics; namespace ProductNumbersTarget { class Program { static void Main(string[] args) { int[] digits = { 9, 2, 5, 2, 1, 1, 8, 2, 1 }; Array.Sort(digits); BigInteger[] numbers = new BigInteger[3]; for (int i = 0; i target) return 1; else return -1; } else if (product > target) return 1; bool productIsOver = false; ...