Wrong Answer CPP O(n^2)


  • 1
    W

    I'm using following cpp code , but i am getting wrong answer when i individually execute that particular test case it gives correct expected ouput code is as follows

    class Solution {
    public:
    vector<int> twoSum(vector<int>& nums, int target) {
    int i,j;
    vector<int> solution;
    for(i=0;i<=nums.size();i++){
    for(j=i+1;j<=nums.size();j++){
    if(nums[i]+nums[j]==target){
    cout<<nums[i]<<"\t"<<nums[j]<<"\n";
    solution.push_back(i);
    solution.push_back(j);
    return solution;
    }

            }
            
        }
            
    }
    

    }

    Test case:
    [217,231,523,52,547,243,648,509,415,149,689,710,265,187,370,56,977,182,400,329,471,805,955,989,255,766,38,566,79,843,295,229,988,108,781,619,704,542,335,307,359,907,727,959,161,699,123,650,147,459,657,188,304,268,405,685,620,721,351,570,899,60,388,771,24,659,425,440,508,373,32,645,409,272,356,175,533,740,370,152,34,510,745,251,227,494,258,527,817,773,178,194,860,387,627,851,449,736,15,212,529,950,316,28,65,484,968,63,4,643,795,669,203,677,139,636,289,555,430,849,150,493,301,377,240,873,965,441,230,349,447,470]
    718
    Output: [15,132]
    Expected: [27,79]


Log in to reply
 

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