Thursday, May 6, 2021

LEETCODE TOP INTERVIEW QUESTIONS

 TWO SUM

https://leetcode.com/problems/two-sum/


1. Brute Force - 

TC = O(n**2)

AS = O(1)

vector<int> twoSum(vector<int>& nums, int target) {

        int n = nums.size();

        vector<int>res;

        for(int i = 0; i < n; i++){

            for(int j = i+1; j < n; j++){

                if((nums[j]) == (target - nums[i])){

                    res.push_back(i);

                    res.push_back(j);

                    break;

                }

            }

        }

         return res;

    }

};

No comments:

Post a Comment

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...