Question: public class SummationPuzzle { public static void main ( String [ ] args ) { solvePuzzle ( ) ; } static void solvePuzzle ( )

public class SummationPuzzle {
public static void main(String[] args){
solvePuzzle();
}
static void solvePuzzle(){
int[] digits ={7,8,9};
findSolution(digits, new int[3], new boolean[3],0);
}
static void findSolution(int[] digits, int[] result, boolean[] used, int index){
if (index ==3){
// All digits are assigned, check the equation
int a = result[0];
int b = result[1];
int c = result[2];
// Compute cbb + ba
int cbb = c *100+ b *10+ b; // cbb
int ba = b *10+ a; // ba
int abc = a *100+ b *10+ c; // abc
if (cbb + ba == abc){
System.out.println("Solution found: a ="+ a +", b ="+ b +", c ="+ c);
}
return;
}
// Try each digit in the current position
for (int i =0; i < digits.length; i++){
if (!used[i]){
used[i]= true; // Mark this digit as used
result[index]= digits[i]; // Assign this digit to the result
// Recur to the next index
findSolution(digits, result, used, index +1);
// Backtrack
used[i]= false; // Unmark this digit
}
}
}
} explain this code, especially how the i and index value change

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!