Given a list of numbers, return the largest product that can be made using three of the numbers in the list. The only way that negatives can be involved in creating the product is if there are two large negatives. Sorting the array would put those large negatives at the front and smaller negatives at the end. If we are using negatives in the answer, we would only use the two largest negatives and the largest positive. If the answer uses only positives, all those values will be at the end of the sorted array. Thus, we can compare the result of a possible solution involving negatives and a possible solution using positives, and return the largest of those two.
By "invert", we mean take a tree and swap the positions of the children, so that the branches are swapped. Left becomes right, right becomes left. See below for an example.
I'm going to take a little departure from algos today and instead talk about setting up a simple server in JS and Go. I won't be addressing how to do so in Java, as the most common practice relies on a framework (i.e. Spring) that is basically its own ecosystem.
Given a staircase of n length, and assuming that a person can either ascend 1 or 2 stairs in a single step, determine the possible number of ways that a person can get up the stairs.
Rotating a matrix 90 degrees means that all rows becomes columns in the new matrix. It is easiest to think about the respective indexes for an element as coordinates, which means that rotating the matrix is as simple as reflecting the matrix across two planes: the horizontal and the diagonal (which is the same as flipping the coordinates so that (x,y) becomes (y,x)).
Today I will only be implementing the Queue data structure, because I am just learning Java and will be adding it in to the languages that I write my algorithms in! Wahoo! So, we will implement a simple "first in first out" (FIFO) queue in JavaScript, Go, and Java.
A binary search tree is simply a tree where the left child of a node (A) has a value that is less than A's value, and the right child has a value that is greater than A's value. These rules apply throughout the entire tree. I will not go into the advantages of a BST, nor will I discuss the importance of balancing the tree, but note that it is quite important! An unbalanced BST might as well be a regular tree.
This simple algorithm can be accomplished using a classic `while` loop, recursively dividing by 2 until we are at a number less than or equal to 2, then checking if that number is 2. If it is, the number is a power of two!
A breadth-first search is used in a tree and is categorized by checking every node at a given depth, left to right, and only once all the nodes at the current level have been checked do we descend to a deeper level.
The Caesar cipher is a basic cryptography algorithm where each letter of the alphabet corresponds to another letter that is offset by a predetermined amount. For instance, if the offset is 3, we would get the following conversions: A -> D, B -> E, ... Y -> B, Z -> C (note how we wrap back around if the cipher takes us to either end of the alphabet).
Depth-first search of a tree is a method for recursively iterating across all nodes of a tree to find if the tree contains a given value. The depth-first method descends to the lowest level of each branch before moving to a different branch.
Given an array (vector in Clojure) where each element represents the maximum jump distance (number of indexes to travel), determine if you can reach the last index in the array when starting at the 0th index.
Pascal's triangle is the triangle formed when adding the numbers immediately above to the right and above to the left. See the image below, which is taken from Wikipedia:
Quick sort is one of the better sorting algorithms out there, with an average time complexity of O(n*log(n)). Quick sort recursively rearranges items in a list based on the index (before or after the pivot point) and the value of the element (above or below the value at the pivot point).
Jump search (otherwise known as block search) is a searching algorithm for a sorted array, similar to a binary search. However, instead of comparing if the searched value is above or below the midpoint - like in a binary search - we are checking a "block" at a time and seeing if the value *should* be in that block.
Today's prompt is the straight traversal of a linked list. I will assume the prompt is: "Given a value, return the node if the value exists in the linked list, otherwise return null."
There are a multitude of sorting algorithms, each using a different approach for modifying some list such that lower values are before higher values.