The only difference is the nested for loop in the first one is assigned the value of i+1 instead of just +1(second one). Why is the first one considered better than the second one?
int[] nums = { 3, 2, 4 };
int target = 6;
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int answer = nums[i]+nums[j];
System.out.println(answer);//answer should be value of target
}
}
int[] nums = { 3, 2, 4 };
int target = 6;
for (int i = 0; i < nums.length; i++ ) {
for (int j = 1; j < nums.length; j++) {
int answer = nums[i]+nums[j];
System.out.println(answer);//answer should be value of target
}
}
question from:https://stackoverflow.com/questions/66054112/what-makes-this-approach-better-than-this-one