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

1
vote
0answers
18 views

Solving the rod-cutting problem using dynamic programming

I've written what I believe is a valid dynamic programming solution to a variation of the rod cutting problem. In this problem, the goal is to make as few cuts as possible on a rod of length n. I ...
-3
votes
0answers
21 views

Dividing a set of numbers in to two sets such that sum of elements in each set must be equal [on hold]

i have made a java program for following problem statement - "Dividing a set of numbers in to two sets such that sum of elements in each set must be equal " input: array of integers output: ...
6
votes
1answer
94 views

An algorithm that finds the number of “lined up columns”

I found this question in an old exam in computer science. I solved it in a good way, but I'm not convinced that my solution is the best solution. I believe that there's a better way to solve it. ...
5
votes
1answer
68 views

Beat detection algorithm implementation

What is the quality of the code I've written? Is it easy to read or is it a low-quality piece of code? Also, is there any way I can improve the algorithm itself (beside changing C parameters)? ...
3
votes
3answers
91 views

Pouring water between two jugs to get a certain amount in one of the jugs

I wrote a solution for a jug problem (given two jugs of water of different sizes find the steps needed to get specific amount of water in one of the jugs). I'm hoping for some input on my code. How ...
-3
votes
0answers
23 views

C Not Iterating over array correctly [on hold]

I am trying to implement insertion sort in C, but it is not stepping through the array correctly. I'm attempting to implement the pseudocode from Cormen's Algorithms book. It looks like the first ...
-1
votes
0answers
41 views

Why does this implementation of Dijkstra's algorithm work in O(n^2)?

Here is the code I use for implementing Dijkstra's algorithm. Consider a graph with \$n\$ vertices and \$m\$ edges. Shouldn't it run in \$O(n^2 m)\$ ? Someone may say that there are \$n\$ vertices and ...
3
votes
2answers
94 views

Hacker Rank - Lonely Integer

This is the problem statement for Lonely Integer. There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the number that occurs only once. Input Format ...
3
votes
2answers
172 views

Determining if a pair exists in a large sorted array

I'm doing practice interview questions from here. Here is the one I am currently on - if you are given a very large sorted int array, then check of there exists a ...
2
votes
3answers
376 views

Finding duplicates in two sorted arrays

This is an interview question from here. Specifically, the second asked for a function that took in two sorted arrays of integers with no duplicate values within a single array and which returned an ...
2
votes
1answer
37 views

Increase efficiency and velocity autosuggestion algorithm

In my project, for inserting an autosuggestion function (also in presence of little insertion errors, like Google), I've created this class: ...
-1
votes
0answers
20 views

A* pathfinding does not work properly [closed]

Can someone look into my A* pathfinding algorithm please? It doesn't find a way properly on harder paths, though does fine going around single non-walkable tiles. When path is impossibile to find it ...
2
votes
2answers
134 views

PrettyPrint a Binary Tree (Follow Up)

I have improved the Pretty Printing of binary tree that I implemented previously. In this follow up I have made changes such that every tree is printed (not just a Complete binary tree). Here is my ...
3
votes
3answers
171 views

PrettyPrint a Binary Tree

I was going through this tutorial for Pretty Printing a binary search tree and decided to implement my own version. Here is my implementation. This version works only for trees that are complete ...
-1
votes
0answers
53 views

Running average of an array of numbers [closed]

I have implemented this Java code from the Pseudo code but I want to know if I can improve the implementation to make it better. Algorithm 4.1. PrefixAverages1(X) Input: X, a 1-D numerical array ...
3
votes
2answers
78 views

Merge Sort Implementation: Space Usage

I have made a merge sort algorithm but am unsure of the 'Space Usage' of the algorithm. ...
7
votes
2answers
111 views

Mastermind code taking hours to calculate

I'm trying to implement Knuth's Five-guess Mastermind algorithm in my own version of Mastermind, but when running step 6 it takes my code hours to actually run through everything to get the neccesary ...
3
votes
1answer
64 views

Find files with similar contents

I'm not normally a C programmer but thought I'd take a shot at making something useful. This is a C utility I wrote to judge how similar several files are using the algorithm I think ...
2
votes
2answers
91 views

Find Missing Numbers in an int list

I have an alternative algorithm to the same problem as Finding missing items in an int list. My implementation includes optional min and max bounds to selectively fill in the list. The linked ...
-1
votes
1answer
50 views

Merge sort optimization and improvement

How to optimize this merge sort code to make it run faster? And how to call merge_sort function without user input by declaring necessary array in the code? ...
4
votes
4answers
121 views

Quick Sort - implementation

Can anything be improved in this code? ...
2
votes
1answer
44 views

Is this an optimal implementation of merge sort?

I am taking an algorithm course and we are to implement merge sort that handles list of elements with even or odd number of elements and handles duplication, so I wrote this function: ...
0
votes
1answer
46 views

Least common ancestor in a binary tree

I saw this implementation of Least common ancestor and thought that it was very inefficient as it used Queues. I have tried to implement it using a normal tree ...
5
votes
3answers
124 views

Bresenhams line algorithm optimization

Based on these guidelines I optimised Bresenhams line algorithm, it now fits for all kinds of line cases like images a, b, c, d, e, f, g, h show in those guidelines. This is my test.txt input file ...
15
votes
5answers
2k views

Linq me a FizzBuzz

I got this requirement recently: Write some code that prints out the following for a contiguous range of numbers: the number 'fizz' for numbers that are multiples of 3 'buzz' for numbers ...
3
votes
1answer
46 views

All balanced parentheses for a given N

Write a function (in Haskell) which makes a list of strings representing all of the ways you can balance N pairs of parentheses: Example: ...
0
votes
1answer
51 views

“Can you answer these queries I” challenge

I have tried to solve the following problem on SPOJ.com: You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ) A query is ...
2
votes
3answers
95 views

Stack implementation using vectors and templates with no overflow version 1.3

Below is the code for stack along with one or two extra options. Kindly let me know if there are any concerns/critical issues. Below are the links for previous versions. Version 1.0 Version 1.1 ...
-1
votes
1answer
74 views

Finding the distance from each array element to the nearest zero entry

I wrote a program, that gets a certain array and replaces each value in the array with the distance from it to the closest zero value. Time complexity is 4n => O(n). (please correct me if I'm wrong, ...
3
votes
2answers
81 views

Implementation of Dijkstra's algorithm to find the shortest path in graph

This is my implementation of Dijkstra's algorithm in C++. Please clarify Whether the class design is proper or if it needs to be improved. I am just hard coding the vertex based on the priority of ...
4
votes
2answers
158 views

Python Find All Adjacent Subsets of Set of Coins Which Have a Tails Minority

Given a sequence of heads and tails I want to find how many significant subsequences are in this sequence where the number of heads is not less than the number of tails. I want to achieve this in ...
2
votes
1answer
79 views

Binomial heap in Java

I have this implementation of a binomial heap providing insert, decrease-key and extract in logarithmic time. MinPriorityQueue.java: ...
2
votes
2answers
67 views

Four fours to get any number

I decided to solve a problem from Code Golf called the four fours. Problem description: The Four fours puzzle is a popular recreational mathematical puzzle that involves using exactly four 4s ...
1
vote
2answers
87 views

Counting a cycle

An interview question I got - Each element of an int array points to the another element, eventually creating a cycle. Starting at ...
2
votes
0answers
23 views

Implementation of the closest pair algorithm

I just started learning Go. To start playing with it, I've implemented the fast power algorithm: Here is the pseudo-code I used (source): ...
4
votes
1answer
76 views

Finding the minimum number of required deletions to have a non-repeating string

I wrote code for the following problem: Given a string, print out the number of deletions required so that the adjacent alphabets are distinct. Please suggest different methods by which I can ...
3
votes
1answer
60 views

Fast incremental backup algorithm using RSYNC

I'm running a simple Bash script that uses rsync to do an incremental backup of my web server every hour. What I'm looking for is an efficient algorithm to delete ...
3
votes
3answers
130 views

Recurring dates within a range

I'm using this code to find out the recurring dates, within a given range. Is this the correct way to find out the recurring dates within a range of dates? ...
1
vote
1answer
95 views

Summation of Arithmetic Progression Modulo Series

Here is the link to the problem: Aladin on Kattis.com Problem Aladin was walking down the path one day when he found the strangest thing: N empty boxes right next to a weird alien machine. ...
5
votes
2answers
88 views

Project Euler #5 (LCM 1 - 20) Follow up

I'm actually spending time on it, unlike the first time and going through, understanding and implementing the several suggestions I've landed on two different approaches. Approach 1: Employing Java 8 ...
3
votes
3answers
75 views

Zero pad function trim extra leading zeros

Here is a zero pad function I have: ...
4
votes
4answers
546 views

Project Euler #5 (LCM of 1-20)

Challenge: 2520 is the smallest number divisible by 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by 1-20? Solution: ...
6
votes
4answers
132 views

Python 8-Puzzle and solver

I wrote a simple 8-Puzzle and solver and am interested in seeing how it might be improved. Please let me know if any style or design changes might enhance the readability/performance of my code. ...
4
votes
1answer
56 views

TDD and use case: cook dish with substitutions

This is code for a class that takes available ingredients and returns left over ingredients after making a dish. Cesars: 2 carrot 4 ice burg 1 chicken 1 beans Russian: 2 carrots 2 beans 2 chicken ...
3
votes
0answers
38 views

Fast power in Go

I just started learning Go. To start playing with it, I've implemented the fast power algorithm: Any suggestions or criticisms regarding the coding style? ...
4
votes
3answers
106 views

Stack implementation using vectors and templates with no overflow version 1.2

Below is the code for stack along with one or two extra options. Kindly let me know if there are any concerns/critical issues. Below are the two link for previous versions. Original Version 1.1 ...
4
votes
5answers
512 views

Reversed Binary Numbers

Here is my code for "Reverse Binary Numbers" in C. Problem Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and ...
0
votes
1answer
44 views

Linear regression with visualization

I have created a small script that: Creates a lot of random points. Runs a small brute force search to find a rect that has a low error, that is a good fit for the data. Runs a linear regression on ...
9
votes
1answer
79 views

Minesweeper analyze goes to N-Queensland

As @rolfl recently solved the N-Queens problem, I figured that it was time also for me to solve it. My approach is significantly different from @rolfl's. I quickly realized that the N-Queens problem ...
4
votes
1answer
49 views

Random bits with a specified density (Bernoulli Trials)

I'm sure this is a fully 'solved' problem and expecting cross references but I can't find this on Google. I'm probably missing a keyword. Here is a naive function for populating a ...