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
3answers
120 views

Largest palindrome made from the product of two 3-digit numbers

I am working on an interview question in which I need to find the largest palindrome made from the product of two 3-digit numbers. Here is the question. ...
2
votes
1answer
26 views

Graph representation using adjacency list for BFS, DFS, and Karger's algorithm

I want to implement some graph algorithms for learning purposes (e.g BFS, DFS, Karger's min-cut algo) and before diving into them I want to make sure I have got the graph representation right (using ...
2
votes
2answers
30 views

Chess programming algorithm minimax to Alpha Beta

I have written some code for alpha beta for Chess programming and not sure completely if that is the correct way to do it. Can someone please tell me if I implemented the ...
4
votes
2answers
50 views

Performance of Median routine using min performs abysmally

I am writing a routine to calculate median using the minimum of the numbers and then changing the value of the minimum to a large value outside the range of numbers (assuming you know the nature of ...
7
votes
1answer
80 views

A* algorithm for hex tiles

I implemented the A* algorithm for pathfinding for a game of mine. The game uses hex rooms which are connected to each other by an int[] in the room class. Are ...
-4
votes
0answers
20 views
1
vote
2answers
34 views

A* versus Bidirectional Dijkstra's algorithm

I have added bidirectional Dijkstra's algorithm into my pathfinding "framework", and I would like to make good use of C++ programming idioms, eliminate all possible memory leaks, otherwise improve ...
6
votes
3answers
335 views

Bubble sort algorithm in JavaScript

I'm new to JavaScript (and programming) and taking a course. I was tasked with creating a bubble sort on any array using JavaScript. Of course, since this is an algorithm challenge, no sort method ...
5
votes
2answers
35 views

A S.I.N checker, using Luhn's Algorithm, and created in swing.

I have created a program to validate a Canadian S.I.N. using Luhn's Algorithm, and was wondering if there was any way to make it more efficient. From what i can tell, it is already extremely ...
4
votes
1answer
45 views

Implementing Mitchell's best candidate algorithm

I have written an implementation of Mitchell's best candidate algorithm. Mitchell’s best-candidate algorithm generates a new random sample by creating k candidate samples and picking the best of k. ...
-5
votes
0answers
30 views

Sorting a stack in ascending order [on hold]

I am solving CTCI and stumbled upon this question. After much hit and try I had to look for solution :( ...
0
votes
0answers
26 views

Schedule AI Challenge: recursive function crashes browser [on hold]

I am doing the 'schedule' AI challenge and worked a lot on producing the code below. It works great for the Easy test (10 courses), but crashes the browser on the Harder one (250 courses). I suspect ...
5
votes
2answers
84 views

Finding the number which satisfies specific conditions

I saw this question somewhere: There is an 8-digit number. The first digit from left to right determines how many zeroes are in the number. The second digit tells you how many 1s are in the number, ...
2
votes
1answer
55 views

Generic pathfinding framework in C++

I have this generic framework for finding (weighted) shortest paths. Also, the program demonstrates the performance of A* and Dijkstra's algorithms on a directed weighted graph. Both run in ...
1
vote
2answers
35 views

Reading files with code value pairs

I have a text file with about a hundred code value pairs [$code, Value]. For example: Sample file ...
3
votes
1answer
44 views

HackerEarth: Find array index with cumulative sum till there matching a given target

The problem was like this: Given an array: [1, 2, 1, 3, 4] where each index value shows the production of that particular day. So, at index ...
-3
votes
0answers
24 views

Recursion java program [closed]

Write a method printNumDots() to print an ascending sequence of integers beginning from 1, each followed by dots as many as the preceding integer. For example, printNumDots(6) shall print 1. 2.. 3... ...
2
votes
0answers
29 views

Off-line minimum using disjoint sets - C

The off-line minimum problem asks us to maintain a dynamic set T of elements from the domain (1..n) under the operation INSERT and EXTRACT-MIN. We are give a sequence S of n INSERT and m ...
0
votes
1answer
64 views

Implementation of peak finder

I implemented this peak finder for practice. I have tested it. Everything looks pretty good to me. Please let me know if there is any improvement I can make. Peak Finder Let input be an ...
0
votes
1answer
88 views

Converting from binary to unary [closed]

I have function that convert binary number to unary number based on a Markov algorithm. In what ways can I improve the code? The algorithm goes as follows: Replace all ...
-1
votes
0answers
11 views

From VBA to R: what can I use in R that does the same as ReDim in VBA? [closed]

I am currently working with the explicit finite difference method and I have a code for this method. The code is written in VBA and now our teacher wants us to write the code so that we can use it in ...
5
votes
1answer
47 views

Prefix- and z-functions in C++ (string algorithms)

Let us define prefix-function(string)[i] as the length of the largest prefix of string, which is also a suffix of ...
1
vote
1answer
49 views

Iterative tree traversal to turn tree into a dictionary and list

I am trying to iteratively turn a tree into a list. For example: c1 c11 c12 c111 c112 The tree above ...
0
votes
0answers
18 views

Travelling Salesmen Problem - Dynamic Programming

I wanted to review my TSP program for correctness in my approach, although I have not implemented one function subset which would be simple. ...
4
votes
2answers
142 views

Optimized board representation for the 4x4 sliding puzzle

Inspired by this question and my answer to it I told myself I could try an optimized version for a 4x4 puzzle. There are only \$16!\$ possible states, so the state fits into a single ...
1
vote
1answer
45 views

Natural mergesort

This is a problem from Robert Sedgwick's Algorithms book: Write a version of bottom-up mergesort that takes advantage of order in the array by proceeding as follows each time it needs to find two ...
2
votes
0answers
64 views

Nuts and bolts algorithm

I am trying to learn golang. This is a problem from Robert Sedgewick's Algorithm's book: You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts ...
2
votes
1answer
159 views

Steganography - images within images

I'm currently learning about steganography at UNI, so I decided to try it. I was looking for some feedback on the way I decided to do it. Normally, you would change the LSB of the RGB channels in a ...
5
votes
2answers
90 views

Consecutive Primes challenge at CodeEval.com and memory allocation issue

I finally solved this challenge with using recursion and figured out why the automatic scorer wasn't passing my previous solutions. I was using NSMutableArray to ...
8
votes
4answers
776 views

Simple comparison of sorting algorithms in C++

I know this has been done a million times before, but this is my implementation of bubble-sort, insertion-sort, merge-sort, heap-sort, and quicksort. Any feedback on how to make it better would be ...
1
vote
1answer
61 views

Minesweeper algorithm that works within O(1) space complexity

For minesweeper algorithm, instead of the most obvious BFS or DFS solution, I want a O(1) space solution. First of all, I'll explain how the program works. I will pass in a 2D array, with 1 ...
4
votes
3answers
89 views

Minimum coins algorithm in Ruby

I am working on a puzzle to find the minimum number of coins needed to buy a product. I am given three coins: 1 3 5 and also a number representing the price of a product: 11 The answer here ...
-1
votes
2answers
78 views

Comparing values in 2 DataTables

I'm processing 2 DataTables: SSFE: Contains the values I want to find FFE: Is larger, smaller or equally large as SSFE, but does not necessarily contain every value of SSFE The values I need to ...
6
votes
2answers
177 views

Checking validity of NxN Sudoku

Given a multi-dimensional array representing a board in Sudoku, the function should be able to return whether the sudoku is valid or not. Examples: ...
-1
votes
0answers
45 views

How to avoid nested loops

I have a program that uses a lot of nested loops because I am terrible in creating an efficient program. For example: ...
4
votes
3answers
109 views

Single value computation

I'm writing code to compute a single value answer to a given numbers in integer array such that step sum is calculated till only 1 number is left. For example: Input: ...
1
vote
1answer
104 views

Consecutive Primes challenge at CodeEval.com

I am working through a new challenge on CodeEval.com and I think I am getting a correct result but I can't get through their grader. I am not exactly sure where error is. I didn't post this on stack ...
3
votes
1answer
96 views

Finding the top k elements from a file

I was writing a program in C++11 to find top k words from a file. I think priority queues will give the optimal solution so I implemented using that. The code works and compiles. Could anyone please ...
1
vote
3answers
71 views

Adjacency matrix on undirected graph

Please review my Adjacency Matrix class. Since I use std::vector to create the matrix, I would like to store as many nodes as possible. Is it OK to use ...
0
votes
0answers
42 views

Parsing big JSON files efficiently

I have two files ...
5
votes
3answers
368 views

Optimizing Manhattan-distance method for N-by-N puzzles

By N-by-N puzzles I mean f.e. a 3x3 sliding puzzle with one blank space (so a 8-puzzle) or a 4x4 sliding puzzle (so a 15-puzzle). When I try to solve certain puzzles some take no time but others take ...
3
votes
1answer
35 views

Number of paths in a grid (lattice) graph

I am trying to solve a variant of the very popular question in which we find the number of distinct valid paths on a directed graph. The graph's nodes make up a m x n lattice (grid), and the edges ...
2
votes
0answers
18 views

Natural merge sort - follow-up

The previous iteration at Natural merge sort Now my code relies on C++14 and looks like: natural_merge_sort.h: ...
3
votes
3answers
60 views

Natural merge sort

Note: the next iteration at Natural merge sort - follow-up I have compiled natural merge sort algorithm from Java to C++. Natural merge sort sacrifices at most \$2N\$ amount of work in order to ...
7
votes
1answer
176 views

Line segment to circle collision algorithm

I've written a function (in Python 3) which computes if a line segment (constraint) and a circle (body) collide, and returns the point of intersection (closest point to the centre of the circle): ...
1
vote
2answers
96 views

HackerRank Sherlock and GCD challenge

I've written an implementation for the Sherlock GCD HackerRank Challenge. As the running time of my algorithm is something like O(kn^2), I reckon that there’s a more efficient algorithm that could be ...
7
votes
2answers
128 views

Gray codes addition — Take 2

For a while, I have tried to find ways (known or new) to do stuff with Gray codes. As some of you probably know, I already tried to implement an algorithm to add two Gray codes without having to ...
0
votes
2answers
84 views

Changing algorithm to avoid looping with iterrows

I am new to Python. I wrote a code that works but the algorithm is obviously not efficient at all. I am stuck in the loop on iterrows and I do not see how to get rid of it. First a little bit on the ...
14
votes
5answers
494 views

Given a sequence of positive integers A and an integer T, return true if a continuous sequence of A sums up to exactly T

Here is the source of the question, and my solution is below. Did I determine the worst case correctly? If you find any input where it doesn't work, please let me know. ...