Questions tagged [linear-complexity]
16 questions
20
votes
3 answers
Time complexity $O(m+n)$ Vs $O(n)$
Consider this algorithm iterating over $2$ arrays $(A$ and $B)$
size of $ A = n$
size of $ B = m$
Please note that $m \leq n$
The algorithm is as follows
for every value in A:
// code
for every value in B:
// code
The time complexity of…
Shylajhaa
- 301
- 2
- 8
8
votes
0 answers
Shortest path that can be split into contiguous segments of 5 edges connecting 6 distinct nodes in an unweighted graph
The following problem (I'm paraphrasing) appeared in the 2019 Balkan Olympiad in Informatics:
Five friends are on a road trip in a country with $N$ cities and $M$ bidirectional roads joining them. They start in city $1$ and wish to end in city…
Andi Qu
- 221
- 1
- 6
2
votes
2 answers
Finding the most frequent element assuming $\Theta(n)$ frequency
We know [Ben-Or 1983] that deciding whether all elements in an array are distinct requires $\Theta(n \log(n))$ time; and this problem reduces to finding the most frequent element, so it takes $\Theta(n \log(n))$ time to find the most frequent…
einpoklum
- 1,025
- 6
- 19
1
vote
1 answer
Sorting rational numbers in linear time
Edit: for anyone reading, there was an error in the solution sheets. The correct answer to the third question is true, as I assumed.
I am currently studying algorithms and computational complexity at University. I have recently got through three…
Lorenzo
- 13
- 4
1
vote
1 answer
Proving that this Huffman tree construction is valid
"Abstract":
I have recently crafted a new algorithm for constructing code trees which I believe are optimal (the same thing that is constructed using Huffman's algorithm, at Wikipedia). It is based on one observation which is in form of an illegal…
cnikbesku
- 111
- 2
1
vote
1 answer
number of substrings in same position with same contents
L1 and L2 are two lists which only consist of X's and Y's. L1 and L2 have the same length.
How many sublists of L1 contain the same number of X's as the sublist of L2 in the same position? The empty sublist is not counted.
For example:
L1 = X
L2 =…
20389642219
- 13
- 2
1
vote
2 answers
Algorithm for finding a path in a directed graph that visits each node in a given subset
I was wondering about a solution for the following problem:
Given a directed graph $G=(V,E)$ and a subset of vertices $U \subseteq V$ suggest an algorithm that finds if there is a directed path that visits every node in $U$. The path can revisit…
Yarin
- 285
- 1
- 8
1
vote
1 answer
Limited tapes-version TM for pair sum
In the problem of pair-sum we are given a multiset $A$ and a number $\alpha$. We are asked to find whether there is a pair ($2$ numbers) of $A$ s.t. their sum is $\alpha$. Here all numbers are small/constant, $O(1)$, sum of $2$ small numbers…
Benicio Agüero
- 142
- 10
0
votes
1 answer
Design an algorithm with linear complexity
Let A[1 : n] be a vector of n integers such that all elements except O(n^2/3) elements are between 1 and 10n. Design an algorithm with linear complexity that sorts A.
Beyond the algorithm, what I can't fully understand is why we are provided with…
emacos
- 121
- 5
0
votes
1 answer
Show that $\text{BOOL-VAL}$ and $\text{DNF-SAT}$ is decidable in linear time
A boolean expression is valid if it is true for every valuation. The problem
$\text{BOOL-VAL}$ asks whether a given boolean expression is valid. As the question suggests I need to show that $\text{BOOL-VAL}$ is decidable in linear time. I have no…
user153634
0
votes
2 answers
Finding minimal amount of coins to reach n
According to my syllabus, this is a dynamic programming problem yet the explanation to the problem I’m supplied with is really confusing and not close to being understandable.
The problem is such:
You are required to give change equal to n (n is a…
Aishgadol
- 377
- 2
- 12
0
votes
2 answers
Find distinict elements in an array in $O(n)$ time
Given this pseudo-code that finds the number of distinct elements in the given array:
D(A) // A is an array of numbers
U_Size = 1
For i=2 to length(A)
U=?
For j=1 to U_Size
If A[j]=A[i]
Then U = FALSE
j = U_Size
…
Omri Braha
- 29
- 4
0
votes
0 answers
In-place linear sort of integers, again
I am amazed by the many discussion regarding the existence of any linear and in-place sorting algorithm, and variants, see…
Matthieu Latapy
- 685
- 4
- 20
0
votes
0 answers
How do I calculate the average number of comparisons for linear search?
In a list of 15 entries (city names) if searching for a particular name what is the average number of comparisons using linear search? Worst case it would 15 comparisons and best case it would be 1 comparison. Is the following calculation the…
martinbshp
- 101
- 1
-2
votes
3 answers
Is it possible to sort numbers in linear time and constant extra space?
The programming language is C++, and the elements of the input vector are of type int and able to take all valid int values. So it’s not possible to store extra information in the input vector. Now, is it possible to sort the vector in linear time…
Zirui Wang
- 1,028
- 6
- 13