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,059
questions
0
votes
0
answers
16
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 ...
0
votes
0
answers
35
views
Skip list -based Map in Java
Now I have this result.
Code
com.github.coderodde.util.SkipListMap.java:
...
7
votes
2
answers
996
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
116
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
0
answers
55
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
323
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
76
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
299
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
59
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
128
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
93
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 ...
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 <...
0
votes
1
answer
49
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 ...
1
vote
2
answers
110
views
Implement document processing system
I have to implement a class with method that takes document:
...
1
vote
1
answer
145
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 ...
1
vote
1
answer
54
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 ...
2
votes
2
answers
91
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, ...
1
vote
1
answer
42
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 <...
1
vote
2
answers
130
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 ...
4
votes
1
answer
660
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 ...
4
votes
2
answers
410
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 ...
1
vote
0
answers
92
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 ...
7
votes
2
answers
387
views
Heap sort optimization
I tried to optimize heap sort algorithm. Instead of recursion, I used iteration:
...
4
votes
1
answer
74
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 ...
1
vote
0
answers
47
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:
...
0
votes
0
answers
64
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 ...
2
votes
1
answer
97
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 ...
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) ...
3
votes
1
answer
129
views
Pentomino solver in Python
When I was a child, I used to play Ubongo board game, recently I discovered Pentomino puzzle and I wanted to create a custom solver in python for it. Here is what I got:
...
1
vote
1
answer
79
views
HackerRank Algorithm Problem: Climbing the Leaderboard (Python)
Here is the Hackerrank problem which is related to dense ranking and below is my solution which didn't pass the time limit cases. Any better way to optimize this?
...
2
votes
1
answer
65
views
Creating Worst-Case Scenario for QuickSort using Middle Pivot in Java
I've implemented a solution to generate the worst-case scenario for QuickSort using the middle pivot strategy in Java. The goal is to rearrange an input array to produce the worst performance during ...
0
votes
3
answers
72
views
another Merge Sort
I've tried to implement merge sort in c with varying degrees of success, one was correctly sorting the array but leaking memory, one was neither sorting the array and leaking memory.
The following was ...
1
vote
3
answers
102
views
Another ATMs cash-out (denomination) algorithm in Java
Related to this question and this answer, I would like to have a second review from you on a modified version.
The problem I tried to solve
Some kind of "Minimum count of numbers required from ...
2
votes
2
answers
315
views
Another cash machine with special bill values in Java
Task:
An ATM dispenses banknotes with the following values/denominations: {5, 30, 35, 40, 150, 200}. The user should be able to repeatedly enter an amount divisible by 5 without leaving a remainder. ...
0
votes
0
answers
16
views
Login panel .feature (gherkin) file
I think many days ago, I randomly got introduced to cucumber developement, And it seemed like a conventional and interesting principle of software development.
At this very time, I'm concentrating on ...
0
votes
0
answers
19
views
Object-based Radix Sort via an Iteratee
I recently finished implementing a radix sort for objects where either one of the keys is numeric, or can be expressed numerically via an iteratee.
...
4
votes
0
answers
136
views
Find the longest "common sequence" in two lists
In short, the algorithm must find the longest sequence that joins together common sequences from two lists (a more formal specification is given in the code's header).
The lists are assumed to contain ...
5
votes
1
answer
1k
views
Determine if it is EU summer time in Java
I wanted to write a utility method for learning purposes to determine if a given date (year, month and day) falls within EU summer time or not, without using any of the Java library methods. Is here ...
1
vote
2
answers
131
views
Ackermann function iterative-recursive form in C
I am trying to write the Ackermann function iterative in m and recursive in n. I got to this point where the code I provided below work for all values below Ack(5,0) but it overflows after this value. ...
4
votes
2
answers
274
views
Reduce time complexity to process sub-arrays
For a given integer array of size n, find all possible sub arrays of size k which range from 1 to n.
Now find the common item which is also a minimum in all sub-arrays of size k, if no common item is ...
2
votes
1
answer
446
views
Traveling Salesman Problem for visiting cities
Implement TSP problem using best first algorithm (so it will be
backtracking, branch-and-bound, and best-first). Since you are looking
for a cycle, the start/finish city is not important. Therefore we
...
1
vote
5
answers
267
views
Sum of bitwise XOR of each subarray weighted by length
here is the problem statement
You are given an array a of length n consisting of non-negative integers.
You have to calculate the value of \$\sum_{l=1}^n \sum_{r=l}^n f(l,r)\cdot (r - l + 1)\$ where \...
2
votes
2
answers
148
views
Checking a mastermind answer as fast as possible
This is not your typical mastermind question, I am not trying to find the right answer. Merely to check the answer and provide a result as fast as possible. The function signature cannot be changed, ...
3
votes
3
answers
170
views
Bubble Sort as extension to Swift-Arrays
Task description:
Implement an extension for Array, which sorts the elements using the Bubble Sort-algorithm.
My implementation:
...
2
votes
1
answer
135
views
Find character at given index in a sorted sub strings [closed]
For a given string say dbac the possible substrings are [d,db,dba,dbac,b,ba,bac,a,ac,c]. Sort them and concatenate to a string: ...
2
votes
2
answers
65
views
Calculating the sum of all k-sized sub-arrays in an array using sliding window algorithm
I need to calculate the sum of all k-sized sub-arrays in an array using sliding window algorithm. Is that a valid sliding window algorithm? If not, why?
...
0
votes
1
answer
524
views
Leetcode: 2327. Number of People Aware of a Secret
On day 1, one person discovers a secret.
You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the ...
0
votes
1
answer
47
views
Aggregate transactions in slips
I wrote code to aggregate transactions in slips. I'm concerned with the performance of my code because it uses 3 loops that are nested so the time complexity will be cubic. This code won't scale well ...
2
votes
1
answer
68
views
Finding the kth smallest number where all (hexadecimal) digits are different
I'm mostly trying to understand why the simpler char array mask below (to track which digits have been already used) is much ...
3
votes
2
answers
173
views
Counting duplicate elements in two sorted arrays
I've been working on an assignment that involves optimizing a duplicate finding algorithm for sorted arrays, and I'd like to get your thoughts on the implementation. Here's the code I've come up with:
...