An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

learn more… | top users | synonyms

2
votes
1answer
39 views

Function to check that two objects have “equivalent” values

I have two objects, a bet and a match. A match has 2 teams and both teams get a score at the end of the match. A bet has two scores set up before the match begins. Here I need to know if the bet ...
5
votes
2answers
72 views

Wordsearch Generator

For fun I decided to make an algorithm in Python that would create a 2D list of a specified size that it can populate with a list of words and fill the gaps with random characters. It's basically a ...
2
votes
0answers
8 views

Ordering league table with live data

I've created a small project that contains a league table from Premier League updated constantly using local node.js server. Data is fetched from local server. I'm wondering whether my solution is ...
3
votes
1answer
73 views

Maximum contiguous subarray with recursion

I've implemented the algorithm below for the maximum contiguous subarray problem and would like a review on three specific things: Making the code shorter Making the code faster Any other algorithms ...
0
votes
1answer
32 views

Remove Duplicates from Sorted List — LeetCode Solution in C

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given ...
2
votes
1answer
71 views

Mth to last element

This is a challenge from codeeval Write a program which determines the Mth to the last element in a list. Input sample: The first argument is a path to a file. The file contains the ...
3
votes
0answers
44 views

Quick sort implementations

I made a compendium of different known quick sort partitioning methods, implementations and pivot choosing methods. Any kind of improvements are welcome. Partitioning methods: ...
5
votes
2answers
48 views

Optimising a user's-choice sorting algorithm

With the help of some nice folks at Stack Overflow, I wrote an algorithm that will give a user three buttons: option a neither ...
4
votes
1answer
53 views

Heap selection sort in Java

I designed and implemented this adaptive heap sort in Java. It works as follows: Copy the range to be sorted into auxiliary array \$A\$ Create an empty run heap \$H\$ Scan \$A\$ from beginning to ...
5
votes
1answer
60 views

Permutations of a list in Haskell

I have this piece of code which given a list returns another list which combines every element with every other element (without repetitions) using a given function as a combiner. I came up with three ...
4
votes
1answer
20 views

Solving a variation of the “Partition Problem”

In my version of the Partition Problem, I have a set of weights that are all powers of three (1, 3, 9, 27 etc.). I only have one of each weight. There is some object (the weight of this object is ...
1
vote
2answers
114 views

A* algorithm in C++

I wanted to know if my A* algorithm is well-implemented and if it could be optimized in any way. ...
4
votes
2answers
88 views

Implementation of Luhn Algorithm

I've implemented the well known Luhn Algorithm in Python. It's a simple one, so it's good for beginners. ...
3
votes
3answers
2k views

Secure Encryption Algorithm

The algorithm gets the number associated with each char, then adds a random number to it and saves the numbers it added. I plan to make an ...
1
vote
4answers
38 views

Checking whether a graph is bipartite using BFS

I am solving a simple problem that checks whether a graph is two-colourable (bipartite graph). I am using BFS for this approach using C++ STL. I've replaced cin and ...
2
votes
1answer
99 views

Bogosort vs bogobogosort [closed]

A question on Stack Overflow had me look up the article on bogosort on Wikipedia. There they describe the bogosort algorithm and the bogobogosort algorithm. They say, about this last algorithm, that: ...
7
votes
2answers
172 views

Solving 'decent numbers' that follow specific division rules

HackerRank Problem Statement I have to find the largest 'decent number' having N digits. A decent number has the following properties: 3, 5, or both as its ...
3
votes
1answer
71 views

Detecting cycles

This is a challenge question from codeeval.com: Given a sequence, write a program to detect cycles within it. Input sample: Your program should accept as its first argument a path to a ...
4
votes
1answer
68 views

Trie data structure in Java

I have implemented a trie data structure in my way, though it is very similar to what major implementations I found but would like to get it reviewed by experts. I don't see the need to store ...
5
votes
2answers
55 views

HeapSort implementation

I have implemented the heap sort algorithm which passes my tests. Although, I would like to ask you if I can improve anything from readability to performance. ...
6
votes
1answer
65 views

Print permutations in an iterative way

I've written the following code to print all the permutations of a list. For example: \$[1, 2, 3]\$ has these permutations: \$[1,2,3]\$, \$[1,3,2]\$, \$[2,1,3]\$, \$[2,3,1]\$, \$[3,1,2]\$, and ...
6
votes
2answers
319 views

Algorithm to remove duplicates array leaving at most 2 elements

My interview question was that I need to return the length of an array that removed duplicates but we can leave at most 2 duplicates. For example, ...
4
votes
3answers
204 views

Algorithm to group connected graphs

I am working on an algorithm, which should group connected Arcs. The situation is described via code examples below. I have Arcs: ...
6
votes
1answer
57 views

D-ary max heap implementation

I implemented a D-ary max heap backed by a vector for resizing. I would like to know any possible improvements in performance, design, and in the code in general. ...
5
votes
2answers
406 views

Dijkstra Algorithm

I have implemented Dijkstra's Algorithm in C, using arrays instead of any structs. Here is my implementation: ...
6
votes
4answers
190 views

Converting from decimal to word

I'm still investigating the problem with decimal to word conversion. I understood the basics, but now my goal is to learn how to add in base-26 (the English alphabet) or base-52 (...
4
votes
1answer
87 views

Cohen-Sutherland 2D line clipping algorithm

Please, review the following implementation of Cohen-Sutherland line clipping algorithm (p-91): Bits.cpp ...
8
votes
5answers
555 views

Find longest lines from a file

This is a challenge question from codeeval.com: Write a program which reads a file and prints to stdout the specified number of the longest lines that are sorted based on their length in ...
7
votes
2answers
114 views

Finding longest substring containing k distinct characters

Below is my method to find the longest substring having k distinct characters. I have used Set to keep track of distinct ...
7
votes
2answers
91 views

Determine if string has all unique characters

I'm working my way through the exercises of the book Cracking the Coding Interview. I'd like to review my solution for the question: Implement an algorithm to determine of a string has all unique ...
7
votes
1answer
41 views

Draw editable angles

One of the first things I did when I learned Objective-C was to create a little canvas with editable angles. Basically, you tap to create points. Every tap connects to the previous tap. So if you tap ...
2
votes
2answers
74 views
2
votes
1answer
49 views

Efficiency of binary search and improvements

Below you can see my binary search method, and as a relatively new programmer I am looking for any suggestions on how it could be improved. ...
4
votes
3answers
42 views

Minimum number of moves for a knight

The problem is to find the minimum number of moves that a knight will take to go from one square to another on a 'n' cross 'n' chessboard. The code below is based on backtracking. It works well until ...
-2
votes
1answer
54 views

kth smallest element in min heap [closed]

I am working on to find the kth smallest element in min heap. I have code for this whose complexity is \$O(k \log k)\$. I tried to improve it to \$O(k)\$. ...
-1
votes
3answers
123 views

Algorithm of combinations (problem with time and space)

My algorithm generates combinations of elements. For example, having [A, B, C ] creates the following combinations [ A ], [ B ], [ C ], [ AB ], [ AC ], [ B, C ], [ ABC ]. Unfortunately for items too ...
5
votes
4answers
251 views

Sufficient implementation of subset sum for 45 min interview

I am preparing for interviews, and have implemented a solution to this problem from Geeks for Geeks: Given a set of non-negative integers, and a value sum, determine if there is a subset of the ...
4
votes
2answers
74 views

Lightweight implementation of the in-close algorithm from C++ to C# translation

The following C++ code is a lightweight implementation of in-close algorithm. Full details of the project can be found here. I was trying to translate the C++ code to C# but it's very difficult to ...
7
votes
2answers
96 views

Gossip algorithm in distributed systems

I have implemented the gossip algorithm in distributed systems for review. Gossip class ...
6
votes
5answers
288 views

“Critter Tracking: When does it cross its own path?” Part 2

Basic Info: This question is my second attempt at this question. It is based on a question similar to this Codility question. The input (an int array), the outpout (an integer) and the method ...
4
votes
3answers
139 views

Implementation of Sieve of Eratosthenes algorithm

I have made an implementation of Sieve of Eratosthenes algorithm in Java, and am not certain that it is as efficient as possible. I have looked at other peoples implementations on Google, and on this ...
5
votes
2answers
94 views

Text justification

This is the Text Justification challenge at LeetCode: Given an array of words and a length \$L\$, format the text such that each line has exactly \$L\$ characters and is fully (left and right) ...
5
votes
3answers
113 views

“Critter Tracking: When does it cross its own path?”

Added: Version #2 of this question Minor Update/Additonal information: This question is based on a Codility question similar to this one. The input (an int array), the outpout (an integer) and the ...
3
votes
3answers
41 views

Determining which octant has a specific point

I am interested in octants in plane geometry, which basically involves dividing a plane into 8 equal parts according to a certain point. Octants are particularly useful while implementing the ...
5
votes
1answer
123 views

Caterpillar uneaten leaves

K caterpillars are eating their way through N leaves, each caterpillar falls from leaf to leaf in a unique sequence, all caterpillars start at a twig at position 0 and falls onto the leaves ...
1
vote
0answers
28 views

Range Query - Range Update

This is the problem statement: In most of the range query-update questions, we are asked to update some range of the array with some constant_value which can be ...
3
votes
1answer
45 views

Non-recursive approach to the Rod-Cutting

I've given the Rod-Cutting Problem a go with a non-recursive approach that use a cache instead, with a bottom up approach. For anyone who hasn't heard about the Rod-Cutting problem, it's defined as ...
3
votes
1answer
51 views

Batch Gradient Descent running too slowly

Following Data Science from Scratch by Joel Grus, I wrote a simple batch gradient descent solver in Python 2.7. I know this isn't the most efficient way to solve this problem, but this code should be ...
5
votes
1answer
90 views

A* algorithm solution to “intermediate space probe” challenge

This code is my solution to this Reddit challenge: Description: NASA has contracted you to program the AI of a new probe. This new probe must navigate space from a starting location to an end ...
4
votes
2answers
91 views

Even Fibonacci numbers under 4,000,000 (Project Euler #2)

Prompt: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, ...