Algorithms Posts

11 posts in this category

Extract digits of a number from both ends
Let's say, we have a problem that requires us to print all the digits of a number from the left to right. A simple approach would be extracting the digits from the right to the left, storing them in a...
Algorithms
Detect chess piece movement with Bitboard
In [the previous article](/everyday/04-17-2022-data-structures-introduction-to-bitboard), we learned about Bitboard and how to use it in a simple board game. In this article, let's see how Bitboard ca...
Algorithms
Tracking letter frequency
Sometimes, you will need to count the frequency of letters in a string and do something with that count. For example, with a simple algorithm to check if a string is a permutation of another one, we c...
Algorithms
Cyclic Sort
Cyclic Sort is an easy to implement and efficient algorithm. The only caveat is that it requires an input array to be a continuous range of values. It can be applied to the problems of finding *duplic...
Algorithms
Fast and slow pointers for Linked List
In a Linked List, traversal is restricted to forward only, you can't really go back to previous nodes once you go past them. When working on a problem that requires us to do some lookup in a Linked Li...
Algorithms
Sliding Window
For the problems that require us to do something a stream of values, or a subsequence of an array, the typical approach is to use a sliding window. ![](_meta/sliding-window.png) A naive way to impleme...
Algorithms
Merge two sorted arrays with extra space
**The problem** Given two non-decreasing arrays `a` of length `m`, and `b` of length `n`. Merge the two arrays into array `a`. Since it’s an in-place merge, `a` has some extra spaces to fit the result...
Algorithms
Comparing floating-point numbers
Due to rounding errors, most floating-point numbers cannot be represented precisely in the computer. This led to some weird situations like: ```javascript 0.1 + 0.2 = 0.30000000000000004...
Algorithms
Find Palindrome
When solving palindrome problems, we need to avoid sliding window solution (a.k.a brute force), where we try to generate every substring and check to find the palindrome. The good approach to go for i...
Algorithms
Partition a number to sum of smaller ones
The array **x** is to keep track of all possible integers we can find, the array **t** is to keep the sum **t[i]** of every **x** number from **x[1]** to **x[i]**. ```javascript const n = 8;...
Algorithms