Collect all kinds of algorithm programming exceptions and fault solutions.

How do Google check Username availability so fast?

While creating a gmail account, it asks us to enter the username. When we enter the username and password, then we click the Next Button. Within couple of seconds it gives the error like "That user...

Minimize the max distance, 1D array

Given a group of numbers of length n (sorted), each number is the location of a house in a 1D line "city".
Given a number k<=n, you need to place k "supermarkets" on the 1D city.

Find duplicate codes in a list

There are N (N <= 50) codewords representing different letters, (e.g. "000110", "0", etc). And they will form a list:
(e.g "0110","00","111","

Algorithm to fix flipped faces in a mesh based on the normals and center position

In my application (based on Unity3D) meshes are loaded at runtime. In some meshes all faces are flipped (see picture 1).
I tried to implement a simple algorithm that calculates the center of all ve...

Making array equal by lcm

Given an array a1,a2,a3
.. an of size n. With value as ai = i.In one second we can take any two of them and take lcm(ai,aj). We have to find minimum time to make array equal. Can anyone help what

Will applying Path compression to Union Find always results in a flat tree, If so, Why?

The below images show a Union find Problem solved by rank with path compression. If you don't understand my handwriting then read the description below to understand what I have done.

Dart: What is the proper way to sum up items in a list of Maps?

I am trying to figure out what is the best way to sum up all the same item's quantities specifically the data is formed like below:
data = [
{Item Name: Item 2, Quantity: 1},

algorithm to place N rectilinear blocks in a ring to minimize total area

Imagine having ā€˜Nā€™ rectilinear blocks of varying sizes. 'N' can be any number (< 1000); and of different sizes, but of similar size.
'N' rectilinear blocks
I need an algorithm that will place t...

Building a spanning forest on a directed, unweighted, acyclic graph

I am trying to find an algorithm that would help me do this, but everything I find is very complicated and hard to write. Is there any simpler way to do it?

Common values in sequence around a given postion

Came up with this question but I can't figure out if this has a non brute force answer.
What is the most optimal solution for the below question?
Consider two arrays of strings,
Array1 = ['2','3','...

Sort Linked-list with down pointer

I stumbled upon an algorithm problem of which I want an optimised approach.
Here goes,
Suppose I have a linked-list which is sorted. Each element of the linked list may have a down pointer which

difference between m way tree and m way search tree

I tried finding the difference between m way tree and the m way search tree. Most resources only tells about m way search tree and end up being on B tree or B+ trees.
My doubts are:-
Is it analogo...

Comparing 2D Paths for Similarity

I want to compare two 2D paths (arrays of points) for similarity, and, if possible, get a percentage of how similar the paths are (100% is identical, 0% completely different)
A path in this case is...

Algorithmic Paradigm of the Insertion Sort

Recently I started studying sorting algorithms again and I was wondering which algorithmic paradigm category would better describe the insertion sort. I have researched the web to link the insertio...

still working on this question and have no idea what this explanation even means

Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Input: [1,2,3,4]

Generate a team from a list of unique players based on priority number. 3 Arrays of signups for different role

Im trying to create an application that would form a team of 4 people in a shooter game.
There are 3 roles for 4 players. We need 2x assault, 1x sniper and 1x medic in a team.
I would be choosing p...

How to select edge disjoint cycles with maximum combined length?

Given a graph I want to select cycles in the graph such that
They are edge disjoint, may not be vertex disjoint.
Their combined length is as much as possible.
Any tip will help.

0/1 - Knapsak and similar problem on 2D Matrix

Problem: Given a 2d matrix of item weights, their respective costs in another 2d matrix and max capacity W. Find the optimal selection such that profit is maximum(i.e sum of costs is maximum) and ...

How to find the number of cubes in a cube pattern where cube at center has 4 cubes along the arms

I have been struggling with the below hackerEarth issue since morning and wouldn't know what is wrong.
Could you point to the mistake in the code?
In a cubical pattern, lowermost and uppermost layers

Why selection best case is not O(n)

I have read many topics which people usually say that Selection sort's complexity in best case is still O(n^2). But I coundn't be convinced by those ideas.
For instance, I want to sort the array in

mergesort algorithm merge function doesnt work

I am trying to make my own version of mergesort, however, I am kind of stuck on how to merge two array back into a single array with a sequenced order.
Below is my code, after I try to run the prog...

how i can to convert a matlab script to optimize a hfss simulation?

i write a matlab code about the CFOA algorithm for using to optimize dimensions of a QUASI-YAGI (length , width , distance for directors) antenna simulated in hfss , but i have problem about how i ...

Algorithm to convert the given whole number into the custom binary representation

Problem Statement
I have two different patterns to represent whole numbers using binary digits as follows:
First (the standard decimal to binary conversion):
0 -> 000
1 -> 001
2 -> 010
3 -...

Sort by moving only beginning or end elements

Hey I've hit rock bottom trying to solve this. I am trying to sort an array by moving the terminal point elements only efficiently.
For example if the array is 2431, cut 1 paste before 2, cut 3 paste

How do path compression and union by rank complement each other?

I have been reading about union-find problem. The two main improvements are path compression and union by rank. As far as I understand union by rank is used to determine how to combine disjoint tre...

quickest way to calculate powers of 2x2 matrix

I have a 2x2 matrix M which is in general complex. What is the quickest way to multiply M by itself n times, i.e. M^n? I can think of two ways:
A. diagonalize it and multiply them
B. Break up n i...

Range minimum query, dynamic array, interval tree, treap

I need an algorithm with some data structure in Python that at every step when two new elements e1, e2 are given:
finds the insertion positions (conserving the order) of the first and the second g...

CRC32 of repeated sequence of bytes

I read Mark Adler's explanation (here and here) of how crc32_combine uses a math trick to compute the effect of feeding one zero bit into the CRC32 state machine in O(log(n)) time, with a 32x32 mat...

Algorithm to solve this Maze game

While learning flutter I have made a game Rabbit and maze which you can access from this link.
Game description: There is 5 type of tiles in the game: Plus(+) shaped have four ways(or openings), T-...

How to calculate cost to Convert an N X M binary matrix to all 0 matrix with only allowed moves as square-matrix toggle?

Given an N X M binary matrix ( every element is either 1 or 0) , find the minimum number of moves to convert it to an all 0 matrix.
For converting a matrix, one can choose squares of any size and

Translating linear SVM classification algorithm to C using MATLAB Coder

I've trained a linear SVM classifier using the Classification Learner app, and exported the model to a .mat file. I'm attempting to translate this algorithm to C using MATLAB Coder, but am getting an

Calculate width of convex polygon at specific angle

What is a way that I can calculate the width of a convex polygon given its vertices and some angle? The purpose of this is to find the cross sectional width of a polygon moving at some velocity in ...

algorithm to choose weights so that average positions will approximate target position

I have a system with a small number of particles (4-10) at fixed locations in space. I then have a single target location. I would like to assign weights to each particle so that the weighted avera...

How to thicken a polyline?

I am looking for algorithm that will let me thicken given polyline -- or algorithm that will create a second polyline, which could be thought as left/right boundary of the "thick line".
The input is

finding the value of n from the equation, nlogn=t

For each function f(n) and time t, determine the largest size n of a problem that can be solved in time t,
where f(n)=t sec.
For the above question, I have to solve for f(n)=nlogn
that means nl...

Big theta is in between big o and big omega or it is both big o and big omega?

Big theta which says it's both big o and big omega. As I understand big o is the upper bound that means for any large input the complexity should not exceed big o and the opposite for big omega. Ho...

What is amortized analysis of algorithms?

How is it different from asymptotic analysis? When do you use it, and why?
I've read some articles that seem to have been written well, like these:

Is floating-point multiplication commutative?

Floating point numbers are not exact due to its limited precision. My question is: is multiplication commutative in floating number computer calculation?
For example
double a = ..;
double b = ...;

Assembly mod algorithm on processor with no division operator

I need to implement a simple macro that finds the modulo of two numbers on a processor that doesn't have a division operator (think ARM). I could use division by repeated subtraction, but I don't k...

Get 1, 0, -1 as positive, zero, or negative for an integer with math only

I have a situation where I'm performing a calculate over a huge number of rows, and I can really increase the performance if I can eschew a conditional statement.
What I need is for a given positi...

What is tail call optimization?

Very simply, what is tail-call optimization?
More specifically, what are some small code snippets where it could be applied, and where not, with an explanation of why?

Conversion of Integer to Characters

I have been looking for the way that integers are converted to characters. I understand that there are ways using modulo and division to extract each number. I am looking for the way that programming

John Carmack's Unusual Fast Inverse Square Root (Quake III)

John Carmack has a special function in the Quake III source code which calculates the inverse square root of a float, 4x faster than regular (float)(1.0/sqrt(x)), including a strange 0x5f3759df con...