Simplest explanation for all possible solutions brute-optimized-optimal along with JAVA and CPP solution