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

3
votes
0answers
55 views

Base-2 representation using C++

I'm preparing for a test and came across this exercise. If possible, dissect this and point out any flaws in it, keeping in mind time complexity. In base -2, Integers are represented by sequences ...
7
votes
3answers
133 views

Finding the shortest excerpt that contains all search terms

Write a function called answer(document, searchTerms) which returns the shortest snippet of the document, containing all of the given search terms. The ...
5
votes
2answers
40 views

Finding the rod pieces in the rod cut issue

I have been reading the chapter 15 of "Introduction to Algorithms" (3rd edition) by Cormen, etc. The chapter 15 is about dynamic programming, and the first example they show us is the "rod cut ...
0
votes
0answers
19 views

Implementing DES algorithm in PHP on linux [on hold]

I wrote this PHP script trying to implement DES algorithm. I'm running it on terminal, not browser, with php des.php AAAAAAAA AAAAAAAA Here the script ...
8
votes
2answers
77 views

Calculating longest increasing sub-sequence recursively

I have trouble understanding recursion, so I'm trying to implement algorithms using it. I wrote this code that calculates the length of the longest increasing sub-sequence. How can I improve this? ...
6
votes
0answers
46 views

Bounding volume hierarchy

I'm building a bounding volume hierarchy in Golang. I can tell my code works because I did some test on it and it gives the same results as the brute force implementation (brute force is to test all ...
14
votes
4answers
230 views

Numbers, pairs, and a difference

Problem statement: Write a function which returns the total number of combinations whose difference is k. For example, ...
0
votes
1answer
35 views

Calculation of image offsets for performing cropping

I am using the java.awt.Rectangle class to construct subsets of a GEOTIFF file. In order to do this I would need to specify the x,y offsets, height and width of ...
5
votes
2answers
75 views
14
votes
3answers
583 views

Enigma machine simulator - improving recursive algorithm performance

I'm new to Haskell and am confused by why my code seems to preform so poorly, and wonder if there's something I've not grasped about the coding philosophy behind the language, or the best way to take ...
5
votes
1answer
54 views

Permutation algorithm of N unique elements with low memory footprint

I actually had a real life need (outside work, no less) to come up with every single permutation of N elements (the N in my case being 12, so a total of ...
5
votes
3answers
338 views

SPOJ solution of GRAVITY

I am solving problem GRAVITY on SPOJ. Each test case is a rectangular ASCII grid of dimensions m × n, and you have to determine ...
0
votes
1answer
31 views

LeetCode Minimum Path Sum algorithm

I am attempting to solve the Minimum Path Sum algorithm problem and I have a working solution, however there was an unwritten requirement that the algorithm not exceed an unspecified amount of time ...
6
votes
1answer
67 views

Efficient top k words using MultiMap (Google Guava ext lib JAR)

Basic algorithm: Open document/huge text file Use Map and List to separate the words. \$O(K)\$ time complexity (TC) ...
2
votes
1answer
57 views

Making sequence unique with removal records

I'm working on a tool to detect all exact tandem repeats in a DNA/RNA sequence. Part of the algorithm requires finding Longest Common Extension (LCE) values, which although can be computed in constant ...
5
votes
0answers
54 views

Find all the primes less or equal than N

This is not a novel interview problem. But I want to address it with the following points: Implement the algorithm with Object-Oriented Design principles in mind. Optimize the solution, if there are ...
3
votes
2answers
37 views

One element only subarray max length

Task description You are given a list and an item, find the length of the longest consecutive subsequence of the list containing only the item. Testcases ...
4
votes
2answers
77 views

Palin Pairs (Pallindrome Counting) Code

In an array of strings 'a' having 'n' strings i have to select the Palin Pairs from the given strings .for ex for input 3 bba abb abb Output=2 I am getting correct output but want to reduce time ...
-1
votes
1answer
40 views

DDA Line Drawing Algorithm

I am using WinBGIm. ...
5
votes
1answer
39 views

Salsa20 stream cipher implementation

I have implemented the Salsa20 stream cipher as an ICryptoTransform. It runs fairly fast and has successfully encrypted and decrypted all of my tests. I would ...
4
votes
2answers
72 views

Two sum algorithm variant

I need to compute the number of target values \$t\$ in the range: \$-10000 \le t \le 10000\$ (inclusive) such that there are distinct numbers \$x, y\$ in the input file that satisfy \$t= x+y\$. I have ...
11
votes
3answers
151 views

Kosaraju in Scala

I started coding in Scala some time ago, and also learning some fundamental algorithms in CS. Here's a terribly slow implementation of Kosaraju algorithm to find strongly connected components in a ...
6
votes
1answer
146 views

Are AVL trees equal?

I was inspired by this answer and decided to implement an AVL tree with the methods equals and hashCode as if I was asked to do ...
5
votes
2answers
113 views

Java Implementation of Sieve of Eratosthenes algorithm

I previously asked about my implementation of the Sieve of Eratosthenes algorithm here. After looking at all of the feedback, I have reworked the code to make it significantly more efficient. ...
2
votes
0answers
50 views

Generate longest subsequence valid parentheses [closed]

I was trying to generate the longest parentheses given a string: Given a string containing just the characters '(' and ')', find the string of the longest subsequence valid (well-formed) ...
2
votes
0answers
33 views

Seeking 5 primes such that concatenating any two of them produces another prime

I have a program which solves Project Euler problem 60: The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be ...
1
vote
1answer
28 views

Implementation of Euclidean algorithm

I have made an implementation of the Euclidean algorithm in Java, following the pseudocode shown below. As far as my knowledge goes, I can't find a way of making this more efficient. I have looked ...
1
vote
2answers
41 views

Finding difference between adjacent integers in an array - recursive Implementation

I'm trying to do a recursive implementation of the problem mentioned below. The code works, and takes care of a corner case with only 1 element in array (in which case I should return ...
4
votes
1answer
48 views

Prefix Sum in Ruby, Genomic Range Query from Codility

I'm currently going through some lessons on Codility. I've just spent a couple of hours with GenomicRangeQuery, which is intended to demonstrate the use of prefix sums. The task description is here. ...
3
votes
1answer
77 views

Calculating the sum of array elements to the left and right

I am using C++ to code the following logic: An Array is given we have to traverse the array such that sum of array elements to its left must be equal to sum of elements to its right. BTW this is ...
3
votes
1answer
31 views

Directed graph path enumerator in Java - follow-up

I have refactored the graph path enumerator. DirectedGraphNode was not changed, so refer to the link above in case you want to have a look at it. The node type is ...
4
votes
1answer
84 views

HackerEarth Challenge Find Maximum Taste of Fruits

Problem Statement: Samu had got N fruits. Sweetness of ith fruit is given by A[i]. Now she wants to eat all the fruits , in such a way that total taste is maximised. Total Taste is ...
5
votes
2answers
119 views

Merge k Sorted Arrays

Please review my answer for this interview question: Merge k sorted arrays, using min heap. ...
12
votes
5answers
988 views

Credit card validation

I started following Harvard's CS50 (Introduction to Computer Science) on edX, and as part of their Hacker edition set 1 was the following assignment: I am supposed to write a program (in C), that ...
4
votes
2answers
69 views

Directed graph path enumerator in Java

I have this iterator class that expects in its constructor two directed graph nodes, source and target, and upon each ...
2
votes
2answers
39 views

Adaptive counting sort for integer arrays in Java

I have this adaptive counting sort for integer arrays. Basically, it maintains a doubly-linked list of nodes. Each node knows its integer \$a_i\$ and contains the counter describing the number of ...
7
votes
3answers
82 views

Fastest way to change the HSV value of all pixels in a bitmap

I want to change all the pixels in an image to have the same value, in terms of the HSV colour space. At the moment I'm running the following code: ...
-1
votes
1answer
41 views

Shortest path in dag saga: Dijkstra's algorithm [closed]

I have this Dijkstra's shortest path algorithm: net.coderodde.graph.pathfinding.AbstractWeightedPathFinder: ...
4
votes
1answer
46 views

Xonix game in JavaScript/Canvas: detection of conquered regions

I've written a Xonix clone in Javascript/Canvas. It seems to work fine. However I would like to refactor the code of detecting conquered regions which seems to be bulky (see methods ...
5
votes
0answers
40 views

Radix sorts in Java

I was curious, how in-place radix sort compares to the variant which uses an auxiliary array in order to speed up the sorting. I implemented both and tested it against ...
5
votes
1answer
54 views

Optimizing bilinear interpolation

I have spent countless hours trying to speed up my bilinear interpolation up. I even tried implementing an SSE version (a double version and a float version), but that was even slower than this ...
2
votes
2answers
61 views

Introsort in Java

Now I have this Introsort for integer arrays. Basically, Introsort is the same algorithm as Quicksort. However, in the very first invocation of that "Quicksort", it computes an integer threshold that ...
12
votes
2answers
100 views

Let's (path) find A Star

Yesterday I found myself struggling with creating an A * algorithm in Java, but this morning I finally figured it out, so I would love to hear what Code Review has to say about it! The goal here is ...
5
votes
2answers
107 views

Function that shortens a String based on a term/abbreviation mapping

I have a function that takes a description as a String and returns a shortened version of the description. The shortening is done by checking if a word matches the ...
1
vote
0answers
44 views

Generalized Missionaries and Cannibals in Java - follow-up

See the previous and initial iteration. Now I have incorporated all the points suggested by mdfst13, and have the following: StateNode.java: ...
5
votes
1answer
88 views

Generalized Missionaries and Cannibals in Java

See the next iteration. I was in the mood for some basic AI, and decided to code up an algorithm for solving "\$M\$ missionaries, \$C\$ cannibals in the boat with \$B\$ places" -algorithm: ...
6
votes
1answer
90 views

Finding the count of negative sub-arrays for a given array

I want to improve my implementation for the following problem: You are given an array of n integers. A sub-array is "Negative" if sum of all the integers in ...
4
votes
3answers
36 views

Max contiguous slice in Ruby

I need to compute the max contiguous slice in an array. I wrote this function, but I am not sure if it is correct or I am missing edge cases. I ran several cases. ...
7
votes
2answers
81 views

Selection Sort algorithm in Java

I'm relatively young in Java, and am familiar with only the basics of the language, yet. I've started studying data structures, and I've made an attempt to create a solution for the selection sort ...
1
vote
1answer
39 views

ANOVA Using Continuous CDFs

I have a procedure for conducting ANOVA, the details of which can be viewed in this white paper. I would like to be able to make the routine scalable to handle any number of groups. Below is the R ...