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.

Filter by
Sorted by
Tagged with
3 votes
2 answers
216 views

Snail matrix in Java

Inspired by this post, I decided to solve that problem in Java. The idea is to decide on an \$n \in \mathbb{N}\$, create a square matrix \$M_n = \mathbb{N}^{n \times n}, \$ and set the integers \$1, 2,...
coderodde's user avatar
  • 28.2k
10 votes
3 answers
1k views

Create a snail matrix

Statement: Create a script that, given an integer \$n\$, print a square matrix of dimensions \$n \times n\$ with the numbers from \$1\$ to \$n^2\$, arranged in a snail pattern. Example: ...
Dorian Turba's user avatar
5 votes
1 answer
341 views

Optimizing Perfect Hash Table Implementation in C for Improved Performance

I wrote code that implements perfect hash table, according to its description in the book "Introduction to Algorithms by Thomas H Cormen", but the code does not pass time tests in contest. I ...
AndrewStrizh's user avatar
-2 votes
1 answer
54 views

I wrote a O(log N) code for largest element in an array today but is this an already existing way to find max element in an array? [closed]

Largest element in an array's most optimal approach. The code I have written has passed all test cases in https://www.naukri.com/code360/problems/largest-element-in-the-array-largest-element-in-the-...
im3shn's user avatar
  • 1
5 votes
1 answer
299 views

Leetcode: Largest Number

I was trying out leetcode's Largest Number. As per the challenge's description: Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since ...
ccot's user avatar
  • 301
4 votes
0 answers
69 views

Implementation of Kirkpatrick-Seidel convex hull algorithm and comparing against Jarvis-March

Introduction I wanted to implement the Kirkpatrick-Seidel(KPS) convex hull algorithm and chose Rust as my language. I referred to the original KPS paper and tried to implement as closely it as ...
Saphereye's user avatar
  • 233
4 votes
1 answer
64 views

Centre of mass based TSP solution (2)

I updated the code to a working state. My previous question. Even if this code working I wonder if I can write this code in a more efficient way. Maybe less for loops Etc. ...
tumr's user avatar
  • 51
8 votes
2 answers
415 views

Image Rotation and Transpose Functions Implementation in C++

This is a follow-up question for Gaussian Fisheye Image Generator Implementation in C++ and An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++. I am trying to ...
JimmyHu's user avatar
  • 4,708
4 votes
4 answers
1k views

Creating an O(n) algorithm for an array of integers

To avoid plagiarism in my university, I am going to modify the problem. Henry likes to jump from building to building. The heights of each building are given as a list of H numbers. We also number ...
Duckycodes's user avatar
7 votes
1 answer
241 views

Colorful Subgraph Dynamic Programming Solution and a Naive One

Given a graph \$G(V, E)\$ with vertices \$V\$ and edges \$E\$, where each vertex is associated with a single color from a set of colors \$C=\{1, 2, ..., k\}\$, we define the following problem: Problem ...
FluidMechanics Potential Flows's user avatar
0 votes
0 answers
38 views

Thread pooled bidirectional breadth-first search in Java for graphs with slow node expansion - part I/II: the algorithm

(This post has part II/II.) Intro This time, I have this GitHub repository. The idea is that if the graph node expansion is slow (for example, in the case where you ask a web server to return some ...
coderodde's user avatar
  • 28.2k
3 votes
1 answer
81 views

Gaussian Fisheye Image Generator Implementation in C++

This is a follow-up question for An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++ and Three dimensional gaussian image generator in C++. I am trying to make a ...
JimmyHu's user avatar
  • 4,708
0 votes
0 answers
29 views

Efficient least-significant digit (LSD) radix sort for int and long keys in Java - iteration II

This post elaborates on Efficient least-significant digit (LSD) radix sort for int keys in Java. This time, I made mild corrections to my code and provided the radix sort for ...
coderodde's user avatar
  • 28.2k
7 votes
3 answers
2k views

Shunting yard calculator in C

I'm new to C, and made this program that parses a string expression into a linked list post-fix expression, and then evaluates it, without parsing the string beforehand. Wondering if there is anything ...
expoodo's user avatar
  • 73
3 votes
1 answer
122 views

Efficient least-significant digit (LSD) radix sort for int keys in Java

(This post has a continuation post.) This one is my attempt at LSD radix sort: Code com.github.coderodde.util.LSDRadixsort.java: ...
coderodde's user avatar
  • 28.2k
3 votes
1 answer
86 views

Sorting songs with the ability to save and resume partial sorts

I listen to a lot of music (~4k h/y) and managing thousands of songs is a bit of a challenging. To improve my listening experience I want to better organize my collection. One thing I want to do is ...
Peilonrayz's user avatar
  • 43.3k
8 votes
2 answers
255 views

Filling a buffer with a repeating pattern of N bytes

My program will clear the screen to a 24bit RGB color; in other words, it will fill a buffer with a repeating pattern of 3 bytes. So I wrote patternMemcpy to perform this operation efficiently*. It ...
Vortex 2728182818's user avatar
2 votes
3 answers
679 views

Step-by-step re-arranging of a string with randomized characters

A sloppy code featuring algorithms to efficiently randomize the characters of a string "Hello C" and then print out in the console the re-arrangement process for every character. ...
Edenia's user avatar
  • 1,568
4 votes
2 answers
460 views

Leetcode: Steps to Make Array Non-decreasing

I was trying out leetcode's Steps to Make Array Non-decreasing As per the challenge's description: You are given a 0-indexed integer array nums. In one step, remove all elements nums[i] where nums[i ...
ccot's user avatar
  • 301
3 votes
1 answer
117 views

RANDEVU - Rust crate implementing a simple algorithm I invented

I've created a simple algorithm which I've implemented in Rust and published it as a crate on crates.io. https://crates.io/crates/randevu While I do think my code is pretty clean and idiomatic, I ...
TypicalHog's user avatar
1 vote
0 answers
45 views

An Updated recursive_transform_reduce Template Function with Unwrap Level Implementation in C++

This is a follow-up question for A recursive_transform_reduce Template Function with Unwrap Level Implementation in C++. To fix the issue mentioned in G. Sliepen's answer, I updated the test cases and ...
JimmyHu's user avatar
  • 4,708
2 votes
1 answer
44 views

A recursive_transform_reduce Template Function with Unwrap Level Implementation in C++

This is a follow-up question for A recursive_transform_reduce Function for Various Type Arbitrary Nested Iterable Implementation in C++ and recursive_invocable and recursive_project_invocable Concept ...
JimmyHu's user avatar
  • 4,708
1 vote
3 answers
109 views

Find min sum for indices to match equation

I want to reduce time complexity of my code: I have an array of integers arr say [1, 2, 3, 6, 67] I have an equation : a*x+b*y=z, I can use the array values in this ...
learner's user avatar
  • 149
1 vote
0 answers
68 views

Skip list -based Map in Java

Now I have this result. Code com.github.coderodde.util.SkipListMap.java: ...
coderodde's user avatar
  • 28.2k
9 votes
4 answers
2k views

Leetcode : First Missing Positive

I was trying out leetcode's first missing positive. As per the challenge's description: Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an ...
ccot's user avatar
  • 301
2 votes
1 answer
135 views

First order hidden Markov model with Viterbi algorithm in Java

Introduction A first order HMM (hidden Markov model) is a tuple \$(H, \Sigma, T, E, \mathbb{P})\$, where \$H = \{1, \ldots, \vert H \vert\}\$ is the set of hidden states, \$\Sigma\$ is the set of ...
coderodde's user avatar
  • 28.2k
1 vote
1 answer
101 views

Path Compression Algorithm in C#

Let's consider a simple graph represented by the following edges, where true indicates a visible node and false indicates a non-visible node: ...
Shahar Shokrani's user avatar
6 votes
2 answers
338 views

A simple find function with concepts

I have written the following simple find function template that does a linear search. ...
digito_evo's user avatar
2 votes
1 answer
78 views

Semi-dynamic range minimum query (RMQ) tree in Java

Introduction I have this semi-dynamic range minimum query (RMQ) tree in Java. It is called semi-dynamic due to the fact that it cannot be modified after it is constructed. However, the values ...
coderodde's user avatar
  • 28.2k
4 votes
1 answer
307 views

c++ quicksort pivots

Is this code for quicksort using the first index as a pivot correct? Also if I want to use the last or middle index as a pivot how would the whole code change? ...
Kqehzo's user avatar
  • 41
1 vote
2 answers
65 views

A recursive_find_if Template Function with Unwrap Level Implementation in C++

This is a follow-up question for A recursive_find_if_all Template Function Implementation in C++, A recursive_all_of Template Function Implementation in C++ and A recursive_all_of Template Function ...
JimmyHu's user avatar
  • 4,708
4 votes
2 answers
140 views

Beginner's attempt at TicTacToe

This is my attempt at making a basic TicTacToe game to play against another human player or a computer controlled opponent. The project was a lot harder for me than I initially thought it would be! I ...
Reslashd's user avatar
1 vote
1 answer
97 views

Bit vector in Java supporting O(1) rank() and O(log n) select() - follow-up

(This post is the continuation of Bit vector in Java supporting O(1) rank() and O(log n) select(). It resides here (version 1.0.1).) Basically, I have implemented everything harold suggested, except ...
coderodde's user avatar
  • 28.2k
2 votes
1 answer
29 views

Simplify matching values while comparing two sorted sets

This is in Star Basic but most likely quite a few languages would have similar structure. I am comparing rows in two sorted sheets. I have a comparison function which returns -1 / 0 / 1 similarly to <...
chx's user avatar
  • 376
0 votes
1 answer
51 views

Performance optimization of traveling salesman like issue

I need help optimizing the performance of this, which is essentially similar to the traveling salesman problem, with the addition that I want to make the graph complete first, with the weight of the ...
Mattis Schulte's user avatar
1 vote
2 answers
114 views

Implement document processing system

I have to implement a class with method that takes document: ...
mascai's user avatar
  • 379
1 vote
1 answer
148 views

Bit vector in Java supporting O(1) rank() and O(log n) select()

Introduction I have this GitHub repository (version 1.0.0.). It implements a rank(i) operation in \$\Theta(1)\$ time, and ...
coderodde's user avatar
  • 28.2k
1 vote
1 answer
56 views

A recursive_find_if_all Template Function Implementation in C++

This is a follow-up question for recursive_any_of and recursive_none_of Template Functions Implementation in C++. I am trying to follow the suggestion of G. Sliepen's answer to implement ...
JimmyHu's user avatar
  • 4,708
2 votes
2 answers
134 views

Map coloring algorithm functional implementation in python

I'm an autodidact working on map coloring algorithms with backtracking, see chapter 2 of this book. I tested this implementation with success but I'm still unsure whether it is tricking me somewhere, ...
deppep's user avatar
  • 23
1 vote
1 answer
45 views

recursive_any_of and recursive_none_of Template Functions Implementation in C++

This is a follow-up question for A recursive_all_of Template Function Implementation in C++ and A recursive_count_if Function For Various Type Arbitrary Nested Iterable Implementation in C++. Besides <...
JimmyHu's user avatar
  • 4,708
1 vote
2 answers
139 views

Fully generic, very efficient bidirectional Dijkstra's algorithm in Java

After finding out that my previous implementations are incorrect, I decided to give it another try. I relied on this post. (The entire project resides in this GitHub repository. Contains some unit ...
coderodde's user avatar
  • 28.2k
4 votes
1 answer
672 views

Caesar Cipher in Swift (iOS-app)

I have implemented the Caesar Cipher in Swift and incorporated it into an iOS-app. I guess it's formally correct. Please see the attached images, which show how the usage of the app is meant. Here's ...
michael.zech's user avatar
  • 4,294
4 votes
2 answers
427 views

Z-Function/ Algorithms on strings. C++

The problem: Given a string s. For each i from 1 to |s|, find the number of occurrences of its prefix of length i in the string. Input: The first line of input contains an integer q (1≤q≤10⁵) — the ...
neely's user avatar
  • 43
1 vote
0 answers
94 views

Generating group permutations: an extended Heap's algorithm in Java

Suppose we have a sequence of sequences <1, 2> <3, 4>. There, we have two groups: <1, 2> and ...
coderodde's user avatar
  • 28.2k
8 votes
2 answers
399 views

Heap sort optimization

I tried to optimize heap sort algorithm. Instead of recursion, I used iteration: ...
iskander's user avatar
  • 121
4 votes
1 answer
94 views

Generic Algorithm X implementation with dancing links (DLX)

This is a generic implementation of Knuth's "Algorithm X" using dancing links. The whole code with a "mandatory" sudoku solver can be found at gitlab. Regarding the scope of the ...
TorbenPutkonen's user avatar
1 vote
0 answers
68 views

Nodejs Websocket pulling new data from InfluxDB

I have a nodejs server with websocket which is pulling the new data from InfluxDB and send it to client. The piece of code is this: ...
KunLun's user avatar
  • 111
0 votes
0 answers
66 views

Is this iterative inorder traversal algorithm well-known?

I was concerned about the approach in Morris traversal algorithm and came up with simpler solution that uses parent pointers in nodes. The constraints were following: Time Complexity: O(n) Space ...
Max's user avatar
  • 1
2 votes
1 answer
99 views

Move timestamp to the next Monday 10:01 AM [closed]

I've been battling ChatGPT for hours now and can't get satisfied with this simple algorithm in TypeScript. These two (hopefully correct) TypeScript+ReactNative solutions should move the timestamp ...
Slazer's user avatar
  • 121
7 votes
2 answers
2k views

Gaussian elimination algorithm in C++

Given: j-g-h-i=0 a+b-c-j=0 c+i-d-e=0 e+g-f=0 And known: a=10 b=7 d=3 e=2 f=3 j=14 I want to solve this (or similar equations) ...
Tobias Grothe's user avatar

1
2 3 4 5
102