✅Two Pointers - O(nlogn) Solution || Fully Explained || Simple and easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \nSimilar problem - 1. Two Sum\n\nAfter sorting.\n\n Let\'s say that the left pointer is 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimilar problem - [1. Two Sum](https://leetcode.com/problems/two-sum/)\n\n***After sorting.***\n\n* Let\'s say that the left pointer is at index`i`and the right pointer is at index`j`(where`i < j`).\n* If`(arr[i] + arr[j]) < target`The number of pairs of elements whose sum is less than the target value and that include the element at index`i`is`j - i`.\n* This is because there are`j - i`elements to the right of index`i`, and any of these elements can be paired with the element at index`i` to form a pair whose sum is less than the target value, as if we travel from`j`to`i`the values are decreasing.\n* Therefore, the total number of pairs of elements whose sum is less than the target value is the sum of`j - i`for all values of`i`from`0`to the length of the array minus 1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nWe use a sliding window approach to find all of the pairs of elements whose sum is less than the target value. The sliding window is a technique that is used to scan a sequence of elements. In this case, the sliding window scans the array of elements.\n\nWe start with the left pointer at the beginning of the array and the right pointer at the end of the array. We then calculate the sum of the elements pointed to by the left and right pointers. If the sum is less than the target, then we increments the number of pairs. Otherwise, we decrements the right pointer.\n\nWe continue iterating until the left pointer is equal to the right pointer. At this point, the sliding window has scanned the entire array, and all of the pairs of elements whose sum is less than the target value have been found.\n\nThe approach is as follows:\n\n1. Sort the array in ascending order.\n2. Initialize the number of pairs to 0.\n3. Initialize the left and right pointers to 0 and the length of the array minus 1, respectively.\n4. Iterate while the left pointer is less than the right pointer:\n    * Calculate the sum of the elements pointed to by the left and right pointers.\n    * If the sum is less than the target, then increment the number of pairs.\n    * Otherwise, decrement the right pointer.\n5. Return the number of pairs.\n\n\n# Complexity\n- Time complexity: O(nlogn)\n\nThis is because the sorting of the array takes O(n log n) time, and the rest of the algorithm takes O(n) time.\n\nHere is a breakdown of the time complexity of the different steps in the algorithm:\n\n* Sorting the array: O(n log n)\n* Worst case complexity for the loop iterating over the array: O(n)\n* Determining if the sum of the two elements is less than the target: O(1)\n* Incrementing the number of pairs: O(1)\n\nThe sorting step is the most expensive step, so the overall time complexity is O(n log n).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n* Space complexity: O(1)\n* As we changing the original input, so we can also say that the space complexity is O(n).\n\nThe space complexity of the algorithm is O(1), because the algorithm does not use any additional data structures. The sorting algorithm used in the code, Collections.sort(), is a stable sort, which means that the relative order of elements with equal values is preserved. This is important for this algorithm, because we need to ensure that each pair of elements is only counted once.\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int countPairs(List<Integer> nums, int target) {\n      int n = nums.size();\n\n      // Sort the List.\n      Collections.sort(nums);\n\n      // Initialize the number of pairs.\n      int countPairs = 0;\n\n      // Initialize the left and right pointers.\n      int low = 0;\n      int high = n - 1;\n\n      // Iterate while the left pointer is less than the right pointer.\n      while (low < high) {\n        // Calculate the sum of the two elements.\n        int sum = nums.get(low) + nums.get(high);\n\n        // If the sum is less than the target, then increment the number of pairs.\n        if (sum < target) {\n          countPairs += high - low;\n          low++;\n        } else {\n          // Otherwise, decrement the right pointer.\n          high--;\n        }\n      }\n\n      // Return the number of pairs.\n      return countPairs;\n    }\n}\n\n\n```
['Two Pointers', 'Java']