# Questions tagged [algorithm]

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.

5,006 questions
Filter by
Sorted by
Tagged with
1k views

### Implementing Sieve of Eratosthenes faster

I wrote a version of Sieve of Eratosthenes: ...
24 views

### Trying to print following pattern issue while trying to print reverse numbers [closed]

Expected Pattern : * * 1 * * 1 2 1 * * 1 2 3 2 1 * Program code : ...
179 views

### Algorithm Optimization -- Automatic Dimensionality of PCA

I have implemented (rather, edited the implementation of) a technique to automatically detect the optimal number of dimensions for PCA, based off of this paper. This was inspired by ...
180 views

### Prime factorization algorithm by using the multiprocessing module of Python

I may introduce a Python code of prime factorization which is from my personal project that I'm working on. ...
1 vote
115 views

### Algorithm design for CodeChef - Our Base is Under Attack

I was working on a CodeChef challenge: Our Base is Under Attack. I have a working solution but it is not fast enough. Could you recommend anything to make it faster? ...
92 views

### Bookstore program using standard algorithms

This is the exercise, from C++ Primer 5th edition: 10.32: Rewrite the bookstore problem from § 1.6 (p. 24) using a vector to hold the transactions and various algorithms to do the processing. Use <...
295 views

### Finding missing numbers in ranges

I have a list of (potentially overlapping) ranges, e.g [(3, 9), (8, 10), (1, 18), (1, 1000000)]. I need to process them in order and for each range calculate how ...
1 vote
100 views

### Text to image steganography

This simple implementation hides text secrets (S) in images by manipulating the least significant bits (lsb) on Pixels. How it works When encrypting I take a three pixel "chunk" for each ...
1 vote
140 views

### Iterative Random Depth First Search implementation in Java

I implemented the iterative randomized depth-first search, with my goal for it to be performant, and have code that made complete sense. It utilizes ...
101 views

### Program to generate a tree of permutations

There is a computation I'm doing with permutations (permutations of varying length of an entire interval [1,N] for some N) in python that I feel could be faster. ...
259 views

### Implementation of DFS algorith as described by Algorithms - Dasgupta, Papadimitrious, Umesh Vazirani

I just want feedback regarding C++ coding style and if in any way I can optimize my code (e.g. to use a different data structure). I'm only trying to use up to C++ 14 standard code. Any other ...
173 views

### Priority Queue implementation for top k frequent elements leetcode in Ruby

While working on the following leetcode problem: https://leetcode.com/problems/top-k-frequent-elements/ I used a Priority Queu implementation in Ruby. I have learned how to implement a Priority Queu ...
2k views

### Euler's brick generator in C++

I stumbled upon this unsolved problem of math named Euler's brick. I wrote a program in C++ to generate solutions for Euler's brick problem. It searches from ...
4k views

### Gossip algorithm in distributed systems

I have implemented the gossip algorithm in distributed systems for review. Gossip class ...
98 views

### K-Way Partitioning QuickSort

I have implemented the K-Way Partitioning algorithm which is based on the Peter Taylor's solution. Which works but i want fill the q array which is the borders of ...
1 vote
76 views

### Identifying indices of non-delimited characters in a string, supporting greater than 1 length of delimiters

The code below takes a string that potentially contains a true match for a delimiter. There can be multiple delimiters located anywhere in the string. We need to obtain the indices that are not a ...
223 views

### A selection sort implemented in Python

I'm not proficient at Python. My question is twofold: whether this selection sort algorithm implementation for number lists could be considered clean code (in the sense of being modular and ...
57 views

### Parsing shortcodes out of a string

I wrote this shortcode parsing and it runs in $O(N^2)$. Is there a way to better optimize this? ...
1 vote
154 views

### Optimization of algorithm performance

I wrote the following code. A user from Stack Overflow recommended me to write my code here in order to be reviewed for optimization. How could I optimize it? Could I have this code in a professional,...
219 views

### Algorithm Challenge: Fuzzy Search

I recently took part in an algorithm challenge to create a Fuzzy search with the following criteria: Given a set array, create a function that receives one argument and returns a new array containing ...
176 views

### Ensuring performance of sketching/streaming algorithm (countSketch)

I have implemented what is know as a countSketch in Python (page 17: https://arxiv.org/pdf/1411.4357.pdf) but my implementation is currently lacking in performance. The algorithm is to compute the ...
95 views

### Approximation search source reconstruction localization algorithm

Goal To determine the coordinates of some signal source in a 3D space, given the coordinates of four observers and the time at which each saw the signal, as well as the velocity of the signal. ...
135 views

### Convert a mapping of arbitrary pairs into a one-to-many map

The accepted solution is much cleaner and outperforms the algorithm below for small maps, it doesn't do as well with larger ones: my code takes twice as long as the accepted code for maps of 10 pairs (...
1 vote
122 views

### Convert camelCase function name to snake_case

I have the following code: ...
649 views

### Implementation of Hash Function in C language

I'm new to programming. and I'm learning a C language. Currently working on Harvard's CS50 problem set (this one: https://cs50.harvard.edu/x/2022/psets/5/speller/). In that problem, they want us to ...
912 views

### Given an integer array A and a range, find a subarray within the range with a minimum sum

Problem HackerRank Range query: You are given an array A of n integers. You have to perform ...
477 views

### Find the K most-frequent values in a vector

I am solving this problem that requires returning the most frequent K elements, and even though I have a solution, I am wondering if there's an improvement that can be made. It runs O(N) to store the ...
1 vote
231 views

### Functional Fibonacci in OCaml

I'm very new to OCaml, and as an exercise I decided to implement the nth Fibonacci algorithm (return a specific Fibonacci number, given an index) in different ways, using what I've learned so far. I ...
1 vote
61 views

### Calculate result of an election using the D'Hondt method

I have learned about list comprehensions a week or so ago and understood that they can save a lot of screen space. Though, I have been told that this is bad practice only to use them, even if it ...
131 views

### Most effective search algorithm for guessing list of strings via function returning bool if substring is in list using Python

I trying to figure out the most effective way to accomplish this task: A function, check(), contains a list of strings. Calling the function with a string as ...
1 vote
25 views

### Optimizing search algorithm for guessing list of strings via function returning bool if substring is in list [duplicate]

I trying to figure out the most effective way to accomplish this task: A function, check(), contains a list of strings. Calling the function with a string as ...
67 views

### Node graph implementation for a material system

I'm working on a material/shader graph system. I came up with a simple solution, but nodes that weren't connected were used in some situations. So, I thought of using Kahn's algorithm to execute nodes'...
348 views

### This code transforms raw data in a table into nested objects

I have a huge array that I want to group by by category and subcategory. A Book is contained in a shelf. A Shelf is contained in a Library. The output is a list of Library. I cannot change my Book ...
150 views

### Getting three numbers input interactively and process them

I'm new to Python and programming in general. Let's imagine I have a simple task: Given 3 numbers from user input, I need to count negative ones and print the result. Considering what I know about ...
36 views

### Highlighting text with custom colors based on many regex

After quite a few attempts I've got a working solution with a Highlight react component. I'm not sure the algorithm is very clean or efficient mostly when it comes ...
1k views

### Generating DNA sequences and looking for correlations

I've written a script to generate DNA sequences and then count the appearance of each step to see if there is any long range correlation. My program runs really slow for a length 100000 sequence 100 ...
1 vote
86 views

### Is there a better way to traverse a list of tree nodes efficiently

I am wondering if there is a better way to traverse a list of trees. Here is my current algorithm: Collect the parent IDs in a Map Traverse all the groups and find ones where their ID is not in the ...
8k views

### Remove specific consecutive element in array

I'm trying to reduce consecutive elements of array to one, but not for all values like: {3,0,0,0,3,3,3,0,0,0} => {3,0,3,0} but for specific one, in my example ...
176 views

### Integer hash function for concurrent map implementation golang

I am using concurrent map from this repo which only uses string as the key and it doesn't have any implementation for key as integer (int64) so I tried implementing it by just replacing all ...
135 views

### subsets in array. Algorithm (No Linq)

Task: A subset is a continuous segment of an array. Two integer arrays are given with elements from 1 to 100: of N and n (N > n). For every subset of the first array of n length find out the next ...
9k views

### Simplex method (linear programming) implementation

The up-to-date code, along some documentation, can be found here. We've implemented a version of the Simplex method for solving linear programming problems. The concerns I have are with the design we ...
135 views

I am trying to model the proof of Immerman–Szelepcsényi Theorem with Haskell since it heavily uses non-determinism. An explanation of what the point of this is can be found here. ...
7k 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" -problem: ...
64 views

### Searching for all paths between two terminal nodes in an undirected, unweighted graph using DFS in Java

I have this small program for building undirected, unweighted graphs and finding all the paths between two given terminal nodes: ...
97 views

### ASCII art smoke wisp generator

I've written a Python function that generates a smoke wisp/genie-tail shape using all 26 (and only 26) letters of the alphabet. Here are some examples of the kind of ASCII art I'm trying to produce: ...
94 views

### C# Get All Diagonals Jagged Array

How can I improve this? The idea is to store every left to right diagonals in a list Not looking for a more efficient algorithm instead something readable, LINQ perhaps? input: 3, 1 2, 5, 7 1, 5, 8, 3,...
95 views

### Get objects from list based on object's field

I have a method which takes List<Document> documents as parameters. I created unit test for it: ...
1 vote
43 views

### How to simplify Tarjan's algorithm implementation in JavaScript and make it non-recursive (i.e. iterative)?

I adapted this Tarjan algorithm in JavaScript to be a little more functional and not pollute the Vertex object with temporary metadata, and arrived here: ...
Task: implement an algorithm to find graph center $Z(G)$ given undirected tree $G$. This is my first time programming in C++ so any (elementary) feedback is appreciated. The way I did it is: Run ...