New answers tagged algorithm
0
votes
Efficient algorithm to count contiguous subarrays that can form arithmetic progressions
Each pair of consecutive values in the sequence has a certain absolute difference d (non-zero because all values are distinct). This means that the pair MUST be part of a contiguous subsequence with ...
Advice
0
votes
0
replies
0
views
Python library recommendation for the implementation of a neural network modification algorithm
You could use Pytorch or Tenserflow, thay have functions to do all what you need.
In my opinion, if your network architecture is big use Pytorh , otherwise use Tenseflow.
1
vote
Why does my Python solution for selecting 2 points from each interval give incorrect results for overlapping intervals?
You are currently considering intervals in order of their start values, but it is the intervals that end first that decide when values must be chosen. Sort the intervals by the end value instead like ...
Advice
1
vote
0
replies
0
views
Python library recommendation for the implementation of a neural network modification algorithm
Pytorch is generally the go to library to implement new architectures. It's very good for prototyping which is perfect for research. So you should start with Pytorch imo. Also, writing an algorithm ...
Advice
0
votes
0
replies
0
views
Python library recommendation for the implementation of a neural network modification algorithm
ai.stackexchange might also be a good place to ask.
Advice
3
votes
0
replies
0
views
I need a different approach to solve problems
Look for ways to split the problem into several smaller problems., then solve those one by one.
You should also learn how to use spell check.
1
vote
(Shell Sorting) Applying Ciura's gap sequence (or some other optimal sequence formula)
Shell sort works the same no matter what gaps you use.
The only thing that changes is which gaps you make between elements.
Think of it like this:
- Shell sort = “insertion sort, but you start by ...
1
vote
(Shell Sorting) Applying Ciura's gap sequence (or some other optimal sequence formula)
One idea could be to provide a stream of increasing gaps as second argument to the shellSort function. Then the caller can choose which gap sequence to provide. The sort function will then consume as ...
0
votes
Idea for improving heapsort
The typical heapsort builds a max-heap. Then it repeatedly calls ExtractMax and stores the extracted item at the location that was freed up by the extraction. That is, if the heap's size is Count, ...
0
votes
Traversal of a DAG in Python, looking for all paths from root
You're right that recursive functions in Python can cause problems, and the usual tail-recursion tricks won't work here either (since your function isn't directly returning the result of a recursive ...
Advice
0
votes
0
replies
0
views
Is there a Julian date algorithm without any restriction on the date?
Use case: astronomy, calculate the appearance of the night sky in ancient times. It's true you don't need an exact date for that, but you need something - typically any day of the year would do. ...
0
votes
recursion versus iteration
You need to check if your problem is non deterministic multi loop. If not, use simple loops. Otherwise, check if it fits deep first or breadth first approach.
Most problems allows both approaches. In ...
Advice
0
votes
0
replies
0
views
Is there a Julian date algorithm without any restriction on the date?
What is the use case for having a date before day o which is earlier than 4000BC - does anything have a precision that needs a day?
0
votes
Splitting a string by a character
A quite elegant and solution posted by @Andfernan with small optimization. I traded costly input string copy and (what I though was costly erase*) for one more size_t and some position calculations
...
0
votes
How to determine if two sentences talk about similar topics?
Before, cross encoder with Bert was popular. A cross-encoder works by feeding both sentences into the Transformer at the same time so the model can examine them jointly and decide how similar they are....
0
votes
Algorithm that returns the path of one cylce in undirected graph
@ivaylo-strandjev I believe you walk up u to v (not v to u), then add edge u-v. u will have a parent that you saw if you saw v twice, and there will be no need to use s. Otherwise, please provide a ...
2
votes
Accepted
Why doesn't a Wallace tree produce numbers with more than 2n final product bits?
I should only be seeing at most 32 bits since 16 x 16 should yeild at most 32 bits
The extra bits produced at the top are guaranteed to be zero. If you implement the algorithm as you described (by ...
Advice
0
votes
0
replies
0
views
Is there a Julian date algorithm without any restriction on the date?
One way is described here.
The general idea is to exploit the (near) symmetry of the calendar around special days, such as January 1, in the year 0.
The core of one conversion is (in C99) :
double ...
2
votes
Why doesn't a Wallace tree produce numbers with more than 2n final product bits?
No b-ary(e.g. 2-ary/binary) multiplier multiplying numbers with x and y digits produces a product with more than x+y digits.
With an n-digit multiplier (x=y=n), you can even add in two more n-digit ...
6
votes
Accurate computation of the inverse gamma function with the standard C math library
In the 1990s the Dutch mathematician Nico Temme observed that in numerical computations involving the gamma function, it is often advantageous to represent the gamma function as the product of a "...
1
vote
Accepted
Trouble implementing the Cooper–Harvey–Kennedy algorithm for finding immediate postdominators in C++
The issue was my assignment of the postorder. I needed to use push_back in the postorder function instead and reverse after assigning.
for (u32 i = 0; i < m_nodes.size(); ++i) {
m_nodes[...
0
votes
Minimum number of sign flips for prefix sum positivity
In order to prove that your algorithm works, it helps to expand the problem definition a bit.
Let F(A,m) be the minimum number of sign flips required in the list of integers A in order to make every ...
0
votes
Which is the fastest way to get the absolute value of a number
If you programming language has functions like max - min, it's easy to do it in that way
def abs(a, b):
return max(a, b) - min(a, b)
Some languages has a function named "sign", that ...
4
votes
Monotonic Stack Algorithm: Is the Average Time Complexity θ(n) or O(n)?
The definition @Lajos Arpad gave of Theta is completely wrong without the additional context that we are talking about average-time complexities.
Theta indeed means that its a tight bound, as he said. ...
3
votes
Quick Sort in GNU COBOL produces invalid result
I suspect you are trashing all of your work variables. Working-Storage Section is common to all invocations of a program, while Local-Storage Section gets a freshly initialized copy for each call. ...
0
votes
Zonk! computer strategy decider design and logic
Of the 46,656 possible rolls of six 6-sided dice, there are only 462 unique outcomes. That is, if order doesn't matter ([3,5,2,4,6,1] is the same as [1,2,3,4,5,6]). 462 is a tractable number.
So build ...
0
votes
Greedy Algorithm for Finding Min Set of Intervals that Overlap All Other Intervals
I think your algorithm is greedy because it is just trying to modify the greedy objective only.
For this problem the greedy objective would be to:
1. Look for the interval with the smallest end time ([...
1
vote
Fibonacci One Recursive Call
… use only ONE recursive call.
The ratio between successive Fibonacci numbers F(i) is approximately ½(1 + ⎷5) and, after the first few terms, the difference between F(i) and F(i−1) • ½(1 + ⎷5) is ...
0
votes
Meta Programming Puzzle: Counting Plus-Signs on a Cartesian Plane
You can try to append a bunch of coordinate points to an array and make two other arrays around this as loci and then look for intersects by expanding a variable about the matches in each array.
3
votes
Finding a maximum "score" of a permutation
I adopted a Divide And Conquer strategy which can be described as follows.
Suppose we want to find an optimally scoring permutation of A = {1, ... , n}. Choose usize and vsize so that usize + 2 + ...
2
votes
Finding a maximum "score" of a permutation
You have indeed identified a pattern that leads to a minoration of the score you have just defined.
We are going to show that the best permutation of N integers achieve at least a score of 2*floor(N/...
1
vote
Why does my Quick Sort implementation sometimes cause stack overflow on large arrays with duplicates?
Not really an answer but a long comment.
Typically its not large or small arrays. As noted in the comments above, its basically the low and high values not reducing by 1 for some situation/input/...
1
vote
Finding all empty triangles
I could not comment galath's answer, so I'm improving his answer here. There is no need for a priority queue and we can save a log factor this way. The complexity becomes O(N^3).
for each pair of ...
1
vote
Fast 2D signed distance
The "Jump Flood" algorithm is an efficient way to compute an SDF of a 2D silhouette. This means you will render a 1-bit mask of your polygon to a buffer, then run as many "ping pong&...
-1
votes
Why is the worst-case binary selection sort time complexity considered O(n^2)?
What the insertion sort + binary search algorithm provides is reducing the time complexity of catching the smallest element , it becomes O(log(n)) instead of O(n), but the shifting operations are ...
0
votes
How to Check if Python List Contains Elements of Another List Duplicates Not Ignored
here is the code which runs in O(n)
x = [1,2,3,4,4]
y = [5,6,7,4,4,3,2,1]
num_to_count = {}
for i in x:
num_to_count[i] = num_to_count.get(i, 0) + 1
for i in y:
if i in num_to_count and ...
4
votes
Find any single 2D point within radius, but fast
TL;DR: Order both sets by Morton number of coordinates to support range searches (a circle's enclosing square is no bad approximation (max. norm circle…)) and avoid handling a lot of circles/points ...
3
votes
Find any single 2D point within radius, but fast
little to no performance gain against the naive O(|C|×|P|) approach of trying each point with each circle and exiting when a match is found.
Is it purely the implementation's fault?
If you are asking ...
11
votes
Find any single 2D point within radius, but fast
If radius is rather small relative to field size, a cell-based approach usually works nicely and doesn't require complex calculations and data structures.
Make a 2D array with dimensions close to ...
-3
votes
Is it normal for quicksort to take 5 hours for a 100,000,000 element array?
Don't know about Java ( because I have not coded in Java since 1998 ).
However using an Intel i7-1355U (mobile i7 CPU, 1.7Ghz base speed) with 16GB of memory, I was able to sort 200 million DWORDs ( 4 ...
0
votes
Convert a string Id to unique Guid (or from md5 to Guid)?
There are RFC standards for creating deterministic UUIDs, specifically v3 (MD5) and v5 (SHA-1). You need your own code for this (or a software library) since it doesn't come out of the box in .NET.
I ...
4
votes
Accepted
How to find minimum number of days for a schedule according to a preference via graph?
you can model this exactly as a DAG on matches and then run a topological sort + longest-path DP.
make one node per match (unordered pair {i,j} with i<j). for each player p, their wish list a1 > ...
0
votes
Fast integer sqrt using Math.Sqrt
can I count on this being perfectly deterministic across platforms for all possible inputs
Square root is one of the 5 basic operations that are required to be correctly rounded by IEEE-754, so you ...
0
votes
What is the best algorithm for overriding GetHashCode?
In modern (2025) .NET, most of the situations where you would have needed to implement GetHashCode() can be avoided by using a record type or ValueTuples, since they have value-based equality ...
0
votes
Algorithm to implement kinetic scrolling
In a realistic physics simulation, it might take a large amount of time to stop if the speed was high enough. In some cases, it might be desired to limit that duration. Here I use maxDuration for that....
0
votes
Design an efficient algorithm to sort 5 distinct keys in fewer than 8 comparisons
I know that this is an old question, and it's long since been answered, but I thought I'd throw a commented implementation in C into the mix of answers here in case it's of benefit to others.
#include ...
0
votes
Longest Increasing Subsequence (LIS) with two numbers
Sorry for this answer 13 years but it might be useful for someone. Who ends up here somehow.
I suppose the problem you are trying to solve is given a list of pairs p_i where p_i = (a_i, b_i) you want ...
2
votes
Accepted
Algorithm to calculate how much blend of points is a specific 2D point
You're on the right track with the Voronoi diagram. It looks like what you want to do is to calculate the Delaunay triangulation of your point set.
Then, any point inside the convex hull with be in ...
Top 50 recent answers are included
Related Tags
algorithm × 121490java × 15328
python × 13799
c++ × 12704
data-structures × 9308
arrays × 8206
sorting × 8101
javascript × 6717
math × 6155
c × 5840
c# × 5350
graph × 4934
time-complexity × 4877
recursion × 4366
graph-theory × 3722
dynamic-programming × 3333
string × 3282
big-o × 3255
performance × 3106
tree × 2866
optimization × 2836
complexity-theory × 2256
php × 2248
search × 2231
language-agnostic × 1948