bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 259. 3Sum Smaller

259. 3Sum Smaller

Leetcode Solutions

Approach: Two Pointers

  1. Sort the input array nums.
  2. Initialize a variable count to store the number of triplets found.
  3. Loop through the array with index i from 0 to n - 3. a. For each i, initialize left to i + 1 and right to n - 1. b. While left < right, check if nums[i] + nums[left] + nums[right] < target. i. If true, increment count by right - left (all elements between left and right will form valid triplets with nums[i]), and move left to the right. ii. If false, move right to the left.
  4. Return the count of valid triplets.
UML Thumbnail

Approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...