My code is simple, mathematically should be working. But it is not..


  • 0
    Z

    class Solution {
    public:
    #include <array>
    #include <algorithm>
    vector<int> twoSum(vector<int> &numbers, int target) {
    int size = numbers.size();
    vector<int> output;

        for (int i = 0; i < size - 1; i++){
            for (int j = i + 1 ; j < size; j++){
                int sum = numbers[i] + numbers[j];
                if ((sum = target) && (numbers[i] <= numbers[j])){
                    output[0] = numbers[i];
                    output[1] = numbers[j];
                    return output;
    
                }
                else if((sum = target) && (numbers[i] >= numbers[j])){
                    output[0] = numbers[j];
                    output[1] = numbers[i];
                    return output;
    
                }
            }
        }
        
    }
    

    };


  • 0
    P

    Whats the output you are getting ? Run time exceeded?
    I faced the run time exceeded issue since my complexity was O(n^2). Your complexity too seems to be the same.


  • 1
    A

    use '==' instead of '=' in if condition.


  • 0
    R

    actually you have to return the index value not the array value and that to you have to check the condition if the index (i<j) always


Log in to reply
 

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