Given an array of numbers arr
and a target number target
, return the largest sum of 3 numbers that's less than the target number. If no such sum exists, return -infinity.
The array will contain at least 3 elements.
In this problem, we want to find 3 numbers whose total is smaller than the target:
When you solve problems like this, you should always write one variable using the other variables. You can subtract b and c from both sides of the equation to get this equation:
This equation means that c
depends fully on a
and b
, so you don't have to loop over c. You only have to loop over a and b.
Here's the pseudocode for the solution:
To avoid visiting different permuations of the same exact a, b, and c multiple times, we can make sure that c
is to the left of a
.
To find c
, we want to find the biggest element less than target - a - b
. Finding the biggest element less than a value (or vice versa) is the main purpose of binary search, so we'll use binary search to find c
. This takes O(logn) time.
To do binary search, we also need to sort the array at the start, but this takes O(nlogn) time and is negligible. The binary search bounds should be 0 to i-1, since c must be to the left of a.
Here's the code for this:
Time Complexity O(n2logn). The double for-loop does O(n2) iterations, and each iteration does a binary search which takes O(log n) time.
We also sort the array at the very beginning, which takes O(nlogn). This is is insignificant compared to the other O(n2logn) term.
Space Complexity O(1). We only have to store 2 variables, which is O(2) = O(1). The size of the input is never included in the space complexity, so the array does not contribute to the space complexity.
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()