Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Isabela Vlls
Short simpler solution python O(n^2)

Isabela Vlls

Nov 26, 2024

class Solution: def searchTriplets(self, arr, target): count = 0 arr.sort() for i in range(len(arr)-2): left, right = i + 1, len(arr)-1 # pointers while left < right: sum_elements = arr[i] + arr[left] + arr[right] if sum_elements < target: # if it's small then all to the left are valid count += (right - left) left += 1 else: right -= 1 return count

4

0

Comments
Comments

On this page

Problem Statement

Try it yourself