121,491 questions
-1
votes
0
answers
62
views
Understanding Time Complexity and Space Complexity in Recursive Algorithms [closed]
I am trying to analyze and optimize recursive algorithms, particularly with regards to their time and space complexity. I have written a recursive solution for the Fibonacci sequence and a merge sort ...
0
votes
0
answers
45
views
How can I sync a frame counter between two computers with potentially high ping?
I have a game with a frame counter which increments at a fixed rate, and my goal is for two instances of the game on different computers to sync this number so that they will have the same number at ...
-3
votes
0
answers
58
views
Can we solve the following optimization problem by dynamic programming [closed]
Suppose we have an 𝑚*𝑛 grid, let 𝑖=0,...,𝑚−1 and 𝑗=0,...,𝑛−1. For each grid point we have an array 𝑆𝑖𝑗 of size 𝑁 that serves as local score function. We want to to find a way that assigns ...
Advice
0
votes
2
replies
60
views
slicing html text based on length of plain text
I have a problem where I need to cut off text based on a max length. But the inputted string could be html a la <p>hello</p>. the html tags do not count towards the max length.
for example ...
4
votes
1
answer
151
views
LZSS - handling the compression of data that contains token-like strings
I've been working on implementing some compression algorithms in C/C++ and JavaScript, I've started by building out LZSS. So far everything seems good. I check my dictionary buffer for matches, and if ...
Advice
1
vote
8
replies
131
views
Making a palindrome out of a string of random letters (or any given value type inputs)
Found this code exercise on W3R. Been trying to figure out the relationship between the outer and inner loop (i and j), but can't seem to wrap my head around it. If anyone could walk me through this ...
-4
votes
0
answers
140
views
How do you make an algorithm that'll find the shortest way to sort a sequence of numbers by applying function that converts to closest lower prime? [closed]
Imagine you get sequence of integers:
[a1 a2 a3 ... an]
where:
1 <= a <= 10^6
1 <= n <= 10^6
You are given a function f(x) that can change a number to the closest lower prime number, or ...
2
votes
1
answer
117
views
Why does my Python solution for selecting 2 points from each interval give incorrect results for overlapping intervals?
I’m trying to solve the following Leetcode problem:
You are given a 2D integer array intervals where intervals[i] = > [starti, endi] represents all the integers from starti to endi
inclusively.
A ...
0
votes
1
answer
207
views
How to implement a list with an efficient "index of" operation?
I'm interesting in possible implementation approaches for a quite special variant of a list, with the following requirements:
Efficient inverse lookup ("index of"): "give me an index ...
Advice
1
vote
3
replies
76
views
Python library recommendation for the implementation of a neural network modification algorithm
I want to implement in python some algorithms from a paper that allow for a pre-trained neural network to be modified (adding or removing neurons or layers) conserving (theoretically) the outputs of ...
6
votes
2
answers
299
views
Efficient algorithm to count contiguous subarrays that can form arithmetic progressions
I'm working on a problem where I need to count, for each possible common difference D, the number of contiguous subarrays whose elements can be rearranged to form an arithmetic progression with common ...
0
votes
0
answers
73
views
Why does alpha and best score/move get updated before a beta cutoff in Negamax with Alpha-Beta Pruning?
In the code block below is my current Negamax implementation, it is currently barebones. My question is why do Blocks 1 & 2 have to occur for a node that is expected to incur a beta-cutoff (i.e. ...
0
votes
2
answers
75
views
(Shell Sorting) Applying Ciura's gap sequence (or some other optimal sequence formula)
I've been trying to teach myself various sort methods just for the sake of being able to use them in the future and right now I've been teaching myself about shell sorts. I understand the way shell ...
0
votes
0
answers
60
views
How do I properly insert 3 into my nested list tree?
I am working on a program that is supposed to build a tree using nested lists in Racket. I believe I am very close, but I am stuck on one section of my insertion testing process.
The syntax for my ...
Advice
0
votes
3
replies
70
views
Is there a Julian date algorithm without any restriction on the date?
The context is implementing conversion functions between Gregorian calendar dates and Julian dates.
Most (nearly all?) such implementations have restrictions on the dates - typically requiring that ...
0
votes
2
answers
108
views
Why doesn't a Wallace tree produce numbers with more than 2n final product bits?
I am trying to understand Wallace Trees.
The algorithm goes
multiply each bit of one number a by each bit of the other (b), which is accomplished as a simple AND gate, where the partial product of ...
2
votes
1
answer
201
views
Accurate computation of the inverse gamma function with the standard C math library
The inverse of the gamma function over the reals is multivalued with an infinite number of branches. This self-answered question is about the principal
inverse of the gamma function, Γ0-1(x), whose ...
-3
votes
1
answer
171
views
Monotonic Stack Algorithm: Is the Average Time Complexity θ(n) or O(n)? [closed]
The algorithm I've written for an assignment is closely related to this monotonic stack approach
https://www.geeksforgeeks.org/dsa/next-greater-element/
Best case:
n pushes → Time complexity: O(n)
...
2
votes
1
answer
160
views
Minimum number of sign flips for prefix sum positivity
Problem:
We are given a list of integers of length n. What is the minimum number of elements whose sign you need to flip such that every prefix sum is non-negative? (A prefix sum is the sum of the ...
2
votes
1
answer
144
views
Quick Sort in GNU COBOL produces invalid result
I am trying to implement Quick Sort in GNU COBOL (version 3.2.2) using a recursive subprogram.
The sorting logic is standard and works perfectly in other languages (C, Python), but when I run it in ...
1
vote
1
answer
319
views
Trouble implementing the Cooper–Harvey–Kennedy algorithm for finding immediate postdominators in C++
I'm trying to implement the Cooper–Harvey–Kennedy algorithm from this paper in C++. The original is used to find dominators, however in my case I need to find the postdominators. For this, I am aware ...
9
votes
2
answers
382
views
Finding a maximum "score" of a permutation
Consider an array of a permutation P of length n (n > 2), namely some order of the integers 1 through n.
Example of P with length n = 7,
[6,5,2,3,7,1,4]
A "score" of a permutation is ...
-2
votes
1
answer
181
views
Why does my Quick Sort implementation sometimes cause stack overflow on large arrays with duplicates? [closed]
I’m trying to implement an in-place Quick Sort in Python. I have two slightly different versions of my partitioning logic, and I’m confused because both seem correct on small arrays, but the second ...
6
votes
3
answers
398
views
Find any single 2D point within radius, but fast
I've got a list P of 2-dimensional points, 100 ≤ |P| ≤ 100000, and a list C of circle centers, 100 ≤ |C| ≤ 100000.
All coordinates (x,y) ∈ P or (x,y) ∈ C are integral, 0 ≤ x,y ≤ 4095.
P and C may ...
2
votes
1
answer
136
views
How to find minimum number of days for a schedule according to a preference via graph?
I am tasked with a question as follows:
We have n players and we want to hold a wrestling tournament with exactly n * (n - 1) / 2 matches. In each day a player can only play at most one match. However,...
-2
votes
1
answer
96
views
Algorithm to calculate how much blend of points is a specific 2D point [closed]
I have a Set of 2D points that I want to create a set of areas from, similar to a Voronoi Diagram.
The difference is that I would like these areas to be blended, like in the following image.
I wouldn'...
2
votes
1
answer
122
views
Fast vectorized maximal independent set greedy algorithm [closed]
I need a really fast vectorized maximal independent set algorithm implemented in pytorch, so I can use it for tasks with thousands of nodes in reasonable time.
I cannot use networkx, it is way too ...
-2
votes
1
answer
119
views
Arabic Word Normalization and Search Ignoring Prefixes and Suffixes in quran app [closed]
I’m developing a Qur’an search application that allows users to search for Arabic words and find their occurrences across the Qur’an.
To improve accuracy, I added a feature that removes Arabic ...
3
votes
3
answers
190
views
Swapping corresponding negative and positive elements in an array while keeping constant order in their respective groups
The problem:
Given an array in which the number of negative elements is equal to the number of positive elements (arranged in arbitrary order). Swap the first negative and the first positive, then ...
2
votes
2
answers
142
views
Why is the worst-case binary selection sort time complexity considered O(n^2)?
Wikipedia and other textbooks reference binary selection sort's asymptotic worst-case time complexity to be O(n^2), usually justifying this with any extra computation caused by swaps. I don't ...
7
votes
3
answers
249
views
Swapping two array elements with two other specific array elements without using an if-statement
I have an array x of n >= 4 elements, at indices 0..n-1. I want to swap the elements at indices j and k, where 0 <= k < j < n with the elements at indices 0 and 1 (it doesn't matter ...
-3
votes
1
answer
98
views
How does Google Fonts browser optimize their font preview experience? [closed]
Analysis of Google Fonts
Here is me scrolling through the Greek fonts
As you scroll, you'll see it progressively calls the css2 url like:
https://fonts.googleapis.com/css2?family=STIX%20Two%20Text%...
2
votes
1
answer
126
views
Calculating a light volume that comes in through a window
I have a room that is a box. There is a single rectangular window on one of the walls of the room. A directional light source is shining into the room through the window. The light source has a ...
-3
votes
1
answer
154
views
I need a Python implementation of my Longest Possible Common Subsequence (LPCS) algorithm [closed]
Please be merciful - I've never asked a question here (I've answered a few) and am a total Python noob.
I developed an algorithm in SQL Server to compute the Longest Possible Common Subsequence (LPCS) ...
5
votes
3
answers
223
views
Efficient ways to check if two binary trees are the same
I implemented a recursive solution in Python to check if two binary trees are identical, which traverses nodes recursively and compares values and structure. The time complexity is O(n), space O(h).
...
8
votes
1
answer
238
views
Does std::find still guarantee first element with std::execution::par?
Parallel policy states that order of iterator evaluation is not guaranteed. However, std::find*, std::search and std::mismatch all say that they return first iterator matching condition. How do those ...
3
votes
2
answers
520
views
Find employees with overlapping wortktime hours
Given StartTime and endTime for N employees, an employee can form a team if his working hour overlaps with other employees' (both startTime and endTime inclusive).
Find the maximum team size.
Example:
...
4
votes
1
answer
309
views
How to make Beam Stack Search optimal and complete
I was reading the paper Beam-Stack Search: Integrating Backtracking with Beam Search by Rong Zhou and Eric A. Hansen and I was attempting to implement it in Java (see PathFinding.java repository in ...
0
votes
0
answers
95
views
Data structure indexing data by key value pairs
I have a bunch of data which is indexed by key/value pairs. For example I might have x=1, y=2, z=3 => data="1,2,3" and x=1,y=2 => data="1,2" etc.
The operations I need to ...
0
votes
0
answers
72
views
Weird Results in Robot Pathfinding Algorithm Testing Using MATLAB
Recently, I developed a MATLAB-based simulation to evaluate my robot pathfinding algorithm. The robots operate on a network of unidirectional tracks, where each robot computes a single path from its ...
7
votes
3
answers
299
views
How to find the longest elementary path in a graph?
The problem is to find the longest sequence of numbers from 1 to 100 such that each number is either multiple or divisor or the previous, and with no repetition. For example, 50 25 5 35 7 63 21 is a ...
1
vote
3
answers
109
views
How to check search time limits efficiently in a C++ UCI chess engine?
I’m writing a chess engine in C++ that communicates via the UCI protocol, and I need an efficient way to check whether the allocated search time has been exceeded without adding too much overhead. The ...
1
vote
1
answer
110
views
Why is my sliding window algorithm not showing results?
The current problem I'm having is that when the code runs it shows me "None" in the terminal.
/usr/local/bin/python3.12/Users/jaredmccarthy/Desktop/2025/ejercicios_leetcode.py
None
Process ...
1
vote
1
answer
81
views
Binary Tree Level Order Traversal problems
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
What's the problem with the following implementation?
How to fix it?...
2
votes
0
answers
115
views
JIRA Lexorank in depth
I'm researching about maintaining order in a list like JIRA, and I come across Jira Lexorank. I've already understood it's core concept, but there are 2 questions that I struggle to find the in depth ...
5
votes
2
answers
253
views
Generate all permutations which contains a number at least once given length, and number of digits
Okay, here's the problem I have. I'd like to do something like this:
V(n,k) generates all n-sized permutations in which each permutations contains all digits from 0 to k-1 in lexicographic order.
V(2,...
-2
votes
2
answers
150
views
Performance of frequently reordering a short list [closed]
I have an application where I have a Rule object that has a list of Filter predicates. I will be comparing many Events to the predicates, and want to know if they all match. Most Events will not match ...
7
votes
2
answers
1k
views
Lexicographically minimal grid path algorithm
I'm trying to solve the following problem: Minimal Grid Path (CSES). Here is the content of the problem:
You are given an n x n grid whose each square contains a letter.
You should move from the ...
-2
votes
3
answers
878
views
Solve CSES Mountain Range efficiently
This is CSES Mountain Range problem: https://cses.fi/problemset/task/3314/
There are n mountains in a row, each with a specific height. You begin your hang gliding route from some mountain. You can ...
4
votes
2
answers
400
views
Number of lexicographical swaps to make the array non decreasing
A lexicographical swap is defined as:
At each step, pick the earliest possible inversion (i, j) (smallest i, then smallest j > i with a[i] > a[j]) and swap them.
Repeat until the array is sorted....