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.
10
votes
2answers
672 views
Battleship algorithm
Im looking to improve my search algorithm in my battleship game. Code is not perfect but would appreciate any suggestions or recommendations.
Running the simulation using a 100x100 grid (10,000 ...
5
votes
1answer
41 views
Search iteratively in a ternary search tree
I was trying to write an iterative search function for my Ternary Search Tree class based on a pseudo-code, and now it seems to work, but I think it can definitely be improved.
...
2
votes
2answers
69 views
Sorting dates (DD/MM/YYYY) with insertion sort [on hold]
I'm having troubles with a coding problem. Yes, I need to write a C++ program which sorts dates. I've tried several methods. Using 2D arrays and now parallel arrays.
The problem I have with my ...
2
votes
0answers
69 views
Best team of three programmers
I have the following exercise:
Consider a weighted undirected graph G = (V, E) representing a group of programmers and their affinity for team work, such that ...
1
vote
0answers
31 views
A* pathfinder in C
Now I have this implementation of A*. Basically it is the same as the Dijkstra's algorithm in my previous post, yet I bothered to make it more cohesive by making functions for handling all the state ...
4
votes
1answer
26 views
Find a triangle in a graph represented as an adjacency list
I have a graph represented as an adjacency list. I need to find a triangle in this graph. A triangle is a triple of vertices u, ...
7
votes
2answers
57 views
Merge Sort without pointers in C
As part of an online course exercise, I was supposed to implement Merge Sort in C. Pointers were still not discussed prior to this exercise, so I have to work with arrays. Memory efficiency is not ...
4
votes
1answer
43 views
Dijkstra's shortest path algorithm in C
Now I have this C implementation of the famous algorithm:
dijkstra.h:
...
5
votes
1answer
53 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 ...
4
votes
2answers
49 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 ...
2
votes
3answers
72 views
Implementation of Brent's Algorithm to find roots of a polynomial
I made a program that contains a root-finding algorithm for polynomials as a function and contains 3 test polynomials. The algorithm is Brent's method and is based entirely off the pseudocode from ...
-3
votes
0answers
19 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
50 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 ...
5
votes
4answers
59 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?
...
10
votes
1answer
82 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
109 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
142 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
48 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 ...
-1
votes
0answers
25 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
95 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
52 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
239 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
36 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
79 views
14
votes
3answers
598 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
343 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
32 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
77 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 ...
8
votes
1answer
78 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
83 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
42 views
5
votes
1answer
41 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
167 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
50 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
82 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
85 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
1k 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 ...