C++ || Optimal || O(nlogn) || Brute Force || O(n^2) || Explanation
Brute Force \n# Approach\n1) Iterate from i = 0 to i < size of nums and inside it we iterate from j = i + 1 to j less than size of nums.\n2) We check weather th
# Brute Force \n# Approach\n1) Iterate from i = 0 to i < size of nums and inside it we iterate from j = i + 1 to j less than size of nums.\n2) We check weather the integer at i$$th$$ postion and j$$th$$ position is less than the target.\n3) If it is greater we increase the count.\n4) At the end we return the value of count.\n\n# Complexity\n- Time complexity: **O(n^2)**\n- Space complexity:  **O(1)** \n\n# Code\n```\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums, int target) {\n        int count = 0;\n         for(int i = 0; i < size(nums); i++){\n             for(int j = i+1; j < size(nums); j++){\n                 if(nums[i] + nums[j] < target){\n                     count++;\n                 }\n             }\n         }\n        return count;\n    }\n};\n```\n\n# Optimal \n\n# Approach\n1) First we sort the vector nums in ascending order.\n2) Initialize two variables $$left = 0$$ and $$right = size(nums) - 1$$.\n3) Iterate in while loop until the value of left is less than right.\n4) If addition of $$nums[left]$$ and $$num[right]$$ is less than target we increase the value of count as $$count = count + right - left$$ because when the addition of element left and right is less than target it is obvious that the elements present between left and right will also be less when added with current left. Then we increase value of left.\n5) Else we decrease the value of right.\n6) Finally count is returned.\n\n# Complexity\n- Time complexity: **O(nlogn)**\n- Space complexity:  **O(1)** \n\n# Code\n```\nclass Solution {\npublic:\n    int countPairs(vector<int>& nums, int target) {\n        sort(nums.begin() , nums.end());\n        int count = 0;\n        int left = 0;\n        int right = size(nums) - 1;\n\n        while(left < right){\n            if(nums[left] + nums[right] < target){\n                count += right - left;\n                left++;\n            }else{\n                right--;\n            }\n        }\n        return count;\n    }\n};\n```\n\n\n\n\n
['C++']