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
1answer
34 views

Function to generate all valid parentheses of n*2

For a leetcode problem to generate the valid parentheses of length 2n, I have written this solution. It is working for smaller inputs but not for larger inputs. Please suggest a way to optimise my ...
2
votes
0answers
44 views

Puzzle Packing Box Z optimisation

Explanation For this Christmas, I got this puzzle as a present, with the aim being to try and fill 25 "Z" shaped pieces into a 5x5x5 box. Once I saw this gift, I thought I would challenge myself into ...
4
votes
1answer
55 views

Prove that the graph is a valid tree in Python

I recently solved this leetcode problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges ...
2
votes
0answers
55 views

Finding subsets from specified array, avoiding duplicates

I am printing subsets from an array whose sum has been specified, while avoiding duplicates. I think there may be improvements, potentially on how using map is bad ...
-3
votes
0answers
15 views

Got wrong answer on UVa Online Judge problem: 10192 - Vacation (LCS problem) [on hold]

PS: 10192 - Vacation It is LCS to code. I have tested my code on multiple test samples, and there wasn't any diff with right answer and my answer, so I wonder what is the problem? ...
3
votes
1answer
45 views

Distinct sums of integers

I need to get the distinct sums of naturals of a natural What I came up with is: ...
3
votes
0answers
36 views

Aho-Corasick for multiple exact string matching in Java

I have this program that compares performance of two algorithm for multiple exact string matching: Given a set of patterns and the text, find in the text all occurrences of any pattern. For example: ...
5
votes
2answers
88 views

Find no of swaps needed to club identical elements of array together

I have created a solution for the problem as below: Given a string S consisting of letters 'O' and 'Z'. Calculate the minimum number of changes required such that all O's are adjacent to each ...
4
votes
1answer
38 views

Worst case runtime analysis of a string partitioning algorithm

Give all the partitionings of a string s such that every substring of the partition is a palindrome What is the time complexity of this solution? And, how can I improve my code overall? ...
6
votes
1answer
78 views

String merge sort in Java

This snippet is about sorting a set of strings using merge sort. However, this version is tailored for strings as it relies on lcp values: Given two strings \$A\$ ...
1
vote
1answer
199 views

GetHashCode() implementation [closed]

I have class A that contains B, C and D class as ...
5
votes
1answer
77 views

Returns all primes p between m <= p <= n

This is my submission for the Prime Generator on SPOJ and it was accepted. Are there any improvements/changes I can make? Input: The input begins with the number \$t\$ of test cases in a ...
3
votes
2answers
139 views

Determining if an std::string contains a numerical type

I recently came across this SO question asking for methods to determine if a std::string represents an integer. There is also this SO question for ...
7
votes
3answers
97 views

Searching the same values in an array

If I am not mistaken, this method has a complexity \$O(N^2)\$: ...
11
votes
2answers
85 views

Dynamic Programming: Fibonacci-like recurrence relation

This is a problem from my Introduction to Algorithms textbook. Consider the infinite sequence of numbers An|n>0 = (1,1,3,4,8,11,...) defined recursively as follows. $$ A_n = \left\{\begin{aligned} ...
8
votes
6answers
1k views

Custom algorithm for hashing and un-hashing password

I am attempting to create functions for getting an algorithm, hashing and un-hashing a string. As it stands, it works, and very well. But, I feel this is not secure enough and someone could easily ...
8
votes
1answer
76 views

Max element of input iterator range

I came across this SO question on why std::max_element requires a ForwardIterator (as apposed to an ...
4
votes
0answers
32 views

Heap update generic algorithms

In the standard library, there are no algorithms for element updates. This makes it unsuitable as a queue for a Dijkstra's algorithm, for example. Thus I implemented generic heap update functions with ...
4
votes
1answer
45 views

Optimal matrix chain multiplication in Java

Preliminaries Given two matrices $$ A = \begin{pmatrix} a_{11} & a_{12} & \dots & a_{1q} \\ a_{21} & a_{22} & \dots & a_{2q} \\ \vdots & \vdots & \ddots & \vdots ...
11
votes
1answer
150 views

Secret Santa with Groups on Swift

I am working on an special version of Secret Santa with internal sub-groups. Typical Secret Santa (SS) requirements: Cannot be assigned to themselves Should be random Every participant should be ...
11
votes
3answers
115 views

Braces and Brackets Autocomplete

I need to complete a sequence of braces and brackets like [ ], ( ), { }, or reject the ...
6
votes
1answer
38 views

Anagram Substring Search

I am having difficulty understanding the runtime and space complexity while implementing the said question. Following is the code: ...
8
votes
2answers
223 views

Finding indexes of numbers that sum to a target

I've taken this algorithm question, which is to find the indexes of any two numbers in an array that sum to a given number. It is quite a simple problem to solve. But it is the execution time of my ...
5
votes
2answers
223 views

Mapping & Sorting Randomly Generated Numbers

Below is the assignment I was given and the coding is what I have came up with. I would appreciate criticism on how I did and on ways to have gone differently about it. You want to number your ...
6
votes
1answer
47 views

Normalizing forces for basketball game

Recently I found the need to implement a normalization formula in a couple of places in my basketball game, so I created some static methods inside of a class to do so. ...
4
votes
2answers
38 views

Vertex cover approximation in JavaScript

I believe this runs in \$O(V+E)\$ but I wouldn't be able to explain the reasons why. I was looking for some general code review and any help on understanding the runtime. ...
8
votes
2answers
157 views

Quick Sum TopCoder (Brute Force solution)

Given a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the ...
7
votes
1answer
53 views

Implementing LSD radix sort

I would love a code review of this LSD radix sort implementation. I've rolled my own, and have implemented counting sort as well. I feel like some of the data structures I've chosen could be improved. ...
10
votes
2answers
47 views

What a crooked way to compute the next straight string (advent of code 11)

This year I have been participating in the Advent of Code series of challenges, and it just so happened that I'd be doing them in Javascript. Not my usual weapon of choice, but I do have some ...
7
votes
3answers
62 views

Solving for every variable in a number of physics formulas

I'm working on making an iOS app to solve physics problems and I'm starting by just having it solve basic kinematics problems with the following formulas (I've coded it in C instead of Swift to start ...
4
votes
1answer
53 views

Determine if all characters are unique in string: Without using Bitwise operator

I am implementing the said question on "Cracking the Coding interview" using HashSet. Please have a look and suggest how I can improve the efficiency of the ...
3
votes
0answers
28 views

Recurse on complex Perl data structure, applying needed rules

Inspired by this answer and previous a commentator of code review use: Given a complex Perl data structure, traverse/jigger/modify as follows: If ORACLE_SID is ...
2
votes
0answers
15 views

Return the Euler Cycle for a graph with es6

My algorithm for finding an Euler tour of a digraph, G⃗, if such a tour exists, starting from some vertex, v. ...
6
votes
1answer
89 views

Algorithm to find pair with max sum from two arrays

Algorithm to find pair with max sum from two arrays A[0 ... n] and B[0 ... n] A[i] + B[j] = max (A[i] + B[j], with i < j) i,j < 99999; n < 99999 My ...
4
votes
1answer
85 views

Seven-segment display GUI

I am building a GUI to display a digital clock face (four seven segment displays), and I have a selector to switch between 12/24-hour formats. I have working code to show both formats, but it is ...
4
votes
1answer
63 views

Java Quicksort Algorithm

I am trying to work on writing readable code and reducing the amount of redundant code I write. I find this hard to do especially when it comes to algorithms. So, I wrote a quicksort program to ...
4
votes
3answers
131 views

Finding prime numbers in user specified array

I have a program that searches for prime numbers in an array specified by the user. The program starts by asking how big the user wants the array to be, then asks how many threads to split the ...
0
votes
1answer
71 views

NAry Tree implementation In C#

Here I implement N-Ary tree in C#. ...
3
votes
1answer
71 views

Windowed Sieve of Eratosthenes in Java

I wanted to try and use the Sieve of Eratosthenes to find all prime numbers between some arbitrary bounds 1 <= m <= n. The simple implementation of the sieve ...
0
votes
1answer
89 views

Select values from two collections

I have a model: ...
7
votes
4answers
136 views

Goldbach’s Conjecture in Python

Input Format Input starts with an integer n (1 ≤ n ≤ 100) indicating the number of cases. The following n lines each contain a test case of a single even number x (4 ≤ x ≤ 32000). Output Format For ...
3
votes
1answer
35 views

Finding the largest not-necessarily contiguous alternating binary string after inverting any sub-string from a given string

I'm having problems solving this problem within the time limit. Alternative Thinking http://codeforces.com/contest/603/problem/A Kevin has just recevied his disappointing results on the USA ...
2
votes
2answers
61 views

Project Euler 22: Names scores alternative

Can someone tell me how to improve my following python code. Code: ...
6
votes
2answers
48 views

Bell numbers calculation in C

This is my implementation of the bell() function to calculate the n-th bell number in C. Along with it I made the factorial() ...
6
votes
3answers
66 views

Sorting networks

Yesterday I read a fascinating article on Wikipedia about sorting networks. Comparator networks are abstract devices built up of a fixed number of "wires" carrying values, and comparator modules ...
6
votes
2answers
577 views

HackerRank: ACM ICPC Team

I made a solution to this problem from HackerRank : You are given a list of N people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. ...
3
votes
1answer
43 views

My implementation of (open) Knight's Tour

This is my implementation of the (open) Knight's Tour on a 5v5 board. My original assignment for CS was to solve the Knight's Tour from any startings position (0,0 -> 4,4). The goal for myself was to ...
1
vote
3answers
96 views

Longest common subsequence (LCS) for multiple strings

How would you improve this code? Particularly the check and check_all functions? The time complexity of the algorithm of mlcs is \$O(|\Sigma|MN)\$, where \$\Sigma\$ is the alphabet, M is the number ...
4
votes
0answers
50 views

Comparing puzzle solvers in Java

I have this program that solves a \$(n^2 - 1)\$-puzzles for general \$n\$. I have three solvers: BidirectionalBFSPathFinder ...
7
votes
2answers
45 views

Function to find the minimum depth of a tree

For a leetcode problem to find the minimum depth of the binary tree, I have written the below solutions. The solution is accepted, but can I do any other changes to make the code elegent? ...