# [C++] Very stubble difference between two solutions, yet one works in 16/16 test cases while other only works in 15/16.

• I have narrowed down the root cause to the for loop. In the working solution(2nd one below), `for ( int i = k +1; i < nums.size(); ++i)` works , but in the non-working solution (1st one below) `for ( i; i < nums.size(); ++i)` doesn't. I don't understand why however and was hoping for an explanation.

The below solution I created works in 15/16 test cases

``````   class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int k = 0;
int i = 1;
vector <int> result;
for (k; k < nums.size(); k++) {
int temp = nums[k];
for ( i; i < nums.size(); i++) {
if (( temp + nums[i]) == target) {
result.push_back(k);
result.push_back(i);
return result;

}

}
i = 1;

}

}
};
``````

The below solution I created woks in 16/16(All) test cases

``````class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int k = 0;
int i = 1;
vector <int> result;
for (k; k < nums.size(); k++) {
int temp = nums[k];
for (int i = k + 1; i < nums.size(); i++) {
if (( temp + nums[i]) == target) {
result.push_back(k);
result.push_back(i);
return result;

}

}

}

}
};``````

• Hi mrcitrusboots57,

I think your non-working solution has a bug. It has chance to get duplicated index when the value of k is the same as i. (eq. k = i = 1)
For example, if the test pattern is [0, 3, 3, 1] and target is 6, non-working solution would return [1, 1], right?
However I think [1,1] is not the expected answer.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.