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,075
questions
5
votes
1
answer
585
views
Karatsuba algorithm in C
This is the Karatsuba algorithm I wrote:
...
2
votes
1
answer
173
views
Leetcode: Number of Islands - BFS (Queue vs Recursion)
I was playing around with leetcode's Number of Islands.
As per the challenge's description:
Given an m x n 2D binary grid grid which represents a map of '1's
(land) and '0's (water), return the ...
1
vote
0
answers
23
views
Can I use only mutex and condition variable Instead of the Banker's Algorithm?
I've been studying the Banker's Algorithm and was curious if I could implement a similar resource management system using only Mutex and Condvar. The code I wrote is a synchronization program that ...
5
votes
2
answers
548
views
C standard input escape sequence replacer program
I've been recently trying to learn C from the K&R Book The C Programming Language.
Currently I'm working on my skills on program control flow, which includes case statements.
To train my skills ...
5
votes
0
answers
83
views
Dial's heap in Java for integer priority queues
(The entire project is here.)
Intro
I have this priority queue data structure for non-negative integer priority keys. I recall that it is called Dial's heap.
Code
Implementation
...
5
votes
1
answer
361
views
Fast and precise summation of random numbers
I'm trying to first formulate this challenge.
In short, we want to sum up an stream of random numbers with the following objective:
The objective is "simply" to sum up as many sequences as ...
3
votes
2
answers
81
views
Optimizing a Function to Check Pronic Numbers in JavaScript
I've written a function in JavaScript to check whether a given number is a Pronic number. A Pronic number, also known as an oblong number, rectangular number, or ...
1
vote
0
answers
42
views
A machine learning model for predicting bit strings in Java
I have this GitHub repository (BitPredictor.java). Basically, I tried to harness a machine learning model for predicting bit strings. I have implemented it to the best of my understanding and have ...
3
votes
2
answers
353
views
Write a Swift-function, which computes the Digital Root
Task: Write a function, which computes the Digital Root of a given number n.
Here's the solution, which I developed:
...
2
votes
0
answers
63
views
Rust implementation of a BTree
I'm studying rust, and I decided to implement a BTree as a way of learning the language.
Can anyone give me suggestions on the code?
As the language has no inheritance, and we must replace it with ...
1
vote
0
answers
31
views
Clique Connect: minimum spanning tree
Problem Statement
You are given a weighted undirected graph G with N vertices, numbered 1 to N. Initially, G has no edges.
You will perform M operations to add edges to G. The i-th operation (1≤i≤M) ...
2
votes
0
answers
125
views
Rust implementation of an algorithm I invented (RANDEVU) #2
I came up with an algorithm and created a Rust implementation of it.
I've already posted it for code review previously but created another post since I've made many changes to it, including the ones ...
8
votes
1
answer
112
views
Point in polygon (with holes) algorithm
I am working on a CAD addon, where I needed a function() which tells me if the point is inside a polygon or not. I have already found some good solutions for it, but none of them worked for me ...
6
votes
1
answer
309
views
Codeforces: D2. Counting Is Fun (Hard Version)
The code works okay for the following problem.
Problem
An array 𝑏 of 𝑚 non-negative integers is said to be good if all the elements of 𝑏 can be made equal to 0 using the following operation some (...
4
votes
0
answers
73
views
Image Rotation with Shear Transformation in C++
This is a follow-up question for Image Rotation and Transpose Functions Implementation in C++ and An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++. I am trying ...
3
votes
1
answer
164
views
Snail matrix in Java - version III (generalizing)
Once again, in the previous version,
Alexander Ivanchenko helped me with his awesome answer.
Now, I have improved the toString() and, also, generalized the class ...
4
votes
1
answer
321
views
Snail matrix in Java - version II
(This post is the continuation of Snail matrix in Java. Also, this post has an continuation.)
As previously, a snail matrix is any square matrix \$M_n = \mathbb{N}^{n \times n}\$ with cell elements ...
6
votes
2
answers
857
views
Snail matrix in Java
(This post has an continuation: Snail matrix in Java - version II.)
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 ...
14
votes
4
answers
2k
views
Create a snail matrix
Statement:
Create a script that, given an integer \$n\$, create a square matrix of dimensions \$n \times n\$ with the numbers from \$1\$ to \$n^2\$, arranged in a snail pattern.
Example:
...
5
votes
1
answer
377
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 ...
-2
votes
1
answer
57
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-...
5
votes
1
answer
322
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 ...
4
votes
0
answers
74
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 ...
4
votes
1
answer
77
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.
...
8
votes
2
answers
438
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 ...
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 ...
7
votes
1
answer
244
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 ...
0
votes
0
answers
41
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 ...
3
votes
1
answer
84
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 ...
0
votes
0
answers
31
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 ...
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 ...
3
votes
1
answer
125
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:
...
3
votes
1
answer
89
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 ...
8
votes
2
answers
258
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 ...
2
votes
3
answers
682
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.
...
4
votes
2
answers
477
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 ...
3
votes
1
answer
163
views
RANDEVU - Rust crate implementing a simple algorithm I invented
I've created a simple algorithm, implemented it in Rust, and published it as a crate on crates.io.
https://crates.io/crates/randevu
My code seems pretty clean and idiomatic to me, but I'd like to know ...
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 ...
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 ...
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 ...
1
vote
0
answers
77
views
Skip list -based Map in Java
Now I have this result.
Code
com.github.coderodde.util.SkipListMap.java:
...
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 ...
2
votes
1
answer
144
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 ...
1
vote
1
answer
104
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:
...
6
votes
2
answers
348
views
A simple find function with concepts
I have written the following simple find function template that does a linear search.
...
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 ...
4
votes
1
answer
308
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?
...
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 ...
4
votes
2
answers
141
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 ...
1
vote
1
answer
98
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 ...