Write a program to find two sum of number in the array || LEETCODE Solution || Day 1 challenge

 Given an array element and target of number which is the return two number such that the sum of the target value 


class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
        //using the nested loop 
        for(int i=0; i<nums.size(); i++){
            
            for(int j=i+1; j<nums.size(); j++){
               // if both number of sum are equal to target the return the indexs 
                if(nums[i]+nums[j]==target){
                    return {i,j};
                }
            }
        }
        return {-1,-1};
    }
};


Post a Comment

0 Comments