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.
4
votes
1answer
36 views
Find minimum in rotated sorted array
The given code find the minimum element in a sorted array rotated by some fixed distance.
Eg: [1, 2, 3, 4, 5, 6, 7] -> [3, 4, 5, 6, 7, 1, 2]
The elements in the ...
3
votes
1answer
23 views
Function that shortens a String based on a term/abbreviation mapping with special cases
This is a update of this question since the requirements for this task changed.
I have a function that takes a description as a String and returns a shortened ...
-3
votes
0answers
17 views
Listing product with the fixed numbers [on hold]
My app has a feature to list the fixed number of products(say 5) in the screen.
...
6
votes
1answer
44 views
Return Y integer with same bit sets as X & having | X - Y | minimum value
I happened to come across one question:
Given an integer 'x' (Only positive), write an algorithm that returns
integer 'y' such that it has the same number of bits set as 'x' and is
not equal ...
4
votes
4answers
57 views
Recursive binary search in Python
I have implemented a recursive binary search in Python and tried to implement some verification to my code. Other than that, is there any optimization I am missing?
...
9
votes
0answers
58 views
IDA*: Iterative Deepening A* implementation
I created a generic version of IDA*. I am attempting to create the following code with exceptional quality and performance. I'm hoping to try and create short code tutorials regarding. However, the ...
4
votes
1answer
103 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 ...
7
votes
3answers
137 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
44 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
23 views
Implementing DES algorithm in PHP on linux [closed]
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
3answers
93 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
50 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
235 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
77 views
14
votes
3answers
592 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
341 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
76 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
38 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
81 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
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
76 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
165 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
148 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
34 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
29 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
81 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
998 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
40 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
85 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
43 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
50 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
101 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
112 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 ...