4,670 questions
-5
votes
1
answer
77
views
Asymptotic time complexity where a variable has a known upper bound?
Say I have an algorithm that runs in O(N*M + N + M). Say that although M can vary, it can only be between 1-20. For asymptotic complexity is M treated as a constant? Because you can't really take the ...
4
votes
2
answers
273
views
What is the time complexity of the following algorithm:
int k = 0;
for (int a = n; a >= 1; a /= 2)
for (int b = a; b >= 1; b--)
k++;
cout << k;
I would say that the answer is O(n), but the book where I have found the exercise says that ...
0
votes
1
answer
103
views
Asymptotic analysis: Time cost of allocating space for a set of elements
What is the time cost (asymptotically) of the following method?:
void fillList(int i)
{
list = new int[i];
}
I have been told that it takes O(1) time because no initialization is done and the ...
-2
votes
1
answer
124
views
How to determine the complexity of a Turing machine from its state table?
I have an algorithm that is implemented by the following state table for a Turing machine:
𝑞1
𝑞2
𝑞3
0
0R 𝑞1
1L 𝑞3
0L 𝑞3
1
1R 𝑞1
0L 𝑞2
1L 𝑞3
_
_L 𝑞2
1S 𝑞0
_3 𝑞0
I want to determine the ...
4
votes
1
answer
152
views
Algorithm to find the number of specific continuous substrings
I am trying to solve the following algorithmic problem:
Every day, Bob goes to work and does one of 26 possible tasks. The tasks are coded with the letters of the English alphabet from a to z. ...
2
votes
0
answers
113
views
Finding the minimum cost at each point in time from a set of weighted intervals
Let’s say we have a list of intervals, each of which has a cost associated with it. Assume that there is always at least one interval active.
The solution would be a timeline of non overlapping ...
0
votes
0
answers
63
views
How can I reduce the complexity of my validations in python
I am trying to lower the complexity of my code and I use Scrutinizer to measure those values. In my code, most of the complexity is due to validations, so how can I improve them?
class gestionRegister:...
0
votes
2
answers
78
views
How to write a unit test to determine if an operation takes about linear time?
I am writing unit tests in JUnit 5 for a Java codebase, though I'm happy to take examples in any language.
I have the ability to count the exact number of times certain operations occur. For the sake ...
1
vote
2
answers
242
views
Can we get a better complexity than O(n) for cumulative sum while using multithreading?
I'm new to multithreading algorithms and I'm trying do recode cumulative sum function to get a better complexity than O(n).
Have you any hint? Or we can't get better than O(n)?
I tried to use divide ...
2
votes
2
answers
80
views
Want to know time complexity for rec Function
I tried to solve leetcode problem Select Cells in Grid With Maximum Score.
You are given a 2D matrix grid consisting of positive integers.
You have to select one or more cells from the matrix such ...
-4
votes
1
answer
107
views
Why is my time computation not the same evolution curve as the complexity? [closed]
Lets say I have a certain number of schools and a certain number of students.
For each student i want to compute a new grade from their old grade using a random coefficient.
I use as much arrays as I ...
0
votes
0
answers
56
views
How to optimize the function below so that it might run under 1 second
Below is a code that makes a network where each node of the network is a Stuart-Landau oscillator. The equations of evolution are coupled and also depend on it's neighbors. The code runs 10^6 ...
1
vote
2
answers
118
views
calculating Time complexity of a function
Need help on how to calculate this time complexity
int func(int n) {
int a = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= n - i; ++j) {
for (int k = 0; k &...
-1
votes
1
answer
156
views
is it true that the complexity of the function is O((log(log(n))^2)?
int func2(int arr[], int n) {
int i = 0, j, counter = 0;
MergeSort(arr, n / 6); // 1
while (i < n / 6) // 2
{
for (j = 6; j < n / 6; j *= j) // 3
counter++;
...
0
votes
1
answer
79
views
How to figure out time complexity of complex programs
For example:
#include <iostream>
using namespace std;
int main() {
int n, s = 0, i, j, k;
cin >> n;
for (i = 1; i <= n * n; i++) {
for (j = 1; j <= i / 2; j++) ...
0
votes
1
answer
58
views
Complexity of the linear search of the several elements in the list
I'm doing a comparison between brute force search and my custom search approach.
A problem arose in estimating the complexity of a brute-force search. As part of the search, I need to check whether ...
1
vote
0
answers
61
views
What is the time complexity of a function f(n) that is O(g(n)) where "part" of g(n) is o(h(n))?
This is in the context of graph algorithms, so I'll refer to V as the set of vertices in the graph and E as the set of edges in the graph (i.e., the input is a graph G = (V, E)). Let's say a graph ...
0
votes
1
answer
311
views
How to choose base case for substitution method for solving recurrences?
I tried to find out how to solve recurrences using substitution method, but I don't understand: how is the base case chosen when the base case is not given explicitly?
I have two problems:
T(n) = 2T(...
1
vote
0
answers
248
views
When is a Range Query Sparse Table useful compared to a Range Query Segment Tree?
Let's look at the complexities of the <preprocess, query, update an element> operations. Let's denote n the size of the initial array and q the number of queries.
Sparse table <O(nlogn) , O(...
0
votes
1
answer
61
views
Given partially sorted array of type x<y => first apperance of x comes before first of y, sort in average O(n)
i was given an assignment to find an algorithm that takes an array A such that for every x<y we have the first appearance of x coming before the first appearance of y
and sorts it in average time ...
-1
votes
1
answer
38
views
How to determine the time complexity of a recursive function that has a loop enclosed in it?
I am trying to solve LeetCode problem 143. Reorder List:
You are given the head of a singly linked-list. The list can be represented as:
L0 → L1 → … → Ln − 1 → Ln
Reorder the list to be on the ...
1
vote
1
answer
35
views
Hash Table creation runtime complexity
An insert into a hash table has a worst case complexity of O(n).
But when creating a new hash table and inserting n elements, as far as I understand this would result in n insertions into a growing ...
0
votes
0
answers
53
views
Time complexity of a divide and conquer algorithm that searches for a value in an n x m table, rows and columns are sorted
I am confused about the time complexity of this algorithm because I am thinking it could be something like T(n) = 3T(n/2) ... but since the array has a size of nxm instead of nxn and I am using ...
1
vote
1
answer
77
views
finding the time complexity of the program
How do we find the time complexity of below program? For the below program should the time complexity be O(N) or O(N*M) or O(N*M*M)?
Take-1: O(N)
to scan N elements in the input array
Take-2: O(N*M)
...
0
votes
2
answers
121
views
determine the big o running time of the method
Static void doIt (int n ) {
int i; // 1 operations
int j; ← (2 x n) // 1 operations
while loop (j > 0) { // n operations
I; ← n // (n+1) operations
...