193 questions
1
vote
1
answer
326
views
Can someone determine time complexity of this LeetCode solution I made?
I wrote a solution to the problem linked below but I'm not sure about its time complexity. I was thinking it was quadratic but it passed within 0 ms when I submitted, so it might be linear idk. I ...
-1
votes
1
answer
243
views
Time Complexity of "in" keyword in python when using a list? Leetcode
I read a little bit everywhere that the "in" operator have a time complexity of O(n), yet I used it in a code and idk why but it's acting as if it have the time complexity O(1)
So I was ...
-4
votes
1
answer
292
views
how do i find the time complexity (big O) of this triple loop?
for (int i = 0; i < n^2; i++) {
for (int j = 1; j < i; j = 2j) {
for (int k = 0; k < j; k++) {
System.out.println("x");
}
}
}
My thoughts are that the outer loop ...
0
votes
1
answer
132
views
Can someone give an Optimized approach? [closed]
Find distinct triplet (i,j,k) such that i<j<k and sum of the arr[i]+arr[j]+arr[k] is divisible by d?
If arr = [3, 3, 4, 7, 8] and d = 5 it should give count as 3
triplet with indices (1,2,3) 3+...
1
vote
1
answer
122
views
Swift enumerators: how is it implemented for empty collection?
I'm implementing a few custom enumerators for my data model, e.g.
struct Land {
// …
let council: Council
let size: Int
}
extension Collection<MyModel> {
func lands(in council: Council, ...
0
votes
1
answer
115
views
What is the number of operation for K pair quick find algorithm?
For my code which is an implementation of the quick find algorithm i generally consider my code for understanding theoretical questions
#include <stdio.h>
#define N 10000
main() {
int i, t,...
5
votes
1
answer
282
views
Is big O notation used to denote something other than the asymptote of a worst case performance?
I've come across the notation recently am confused by what I see. My previous understanding is that big O is supposed to be used to denote an upper bound whereas lower or tight bound uses different ...
0
votes
2
answers
141
views
Reduce cognitive complexity when mapping
if(obj.getAttribute() != null) {
newobject.setAttribute(obj.getAttribute());
}
if(obj.getAttribute() != null) {
newobject.setAttribute(obj.getAttribute());
}
if(obj.getAttribute() != ...
-2
votes
1
answer
416
views
How to get the cyclomatic complexity of a ruby function? [closed]
I am looking for a function or gem that gives me the cyclomatic complexity of a function.
For example, using rubocop, If I write
def my_func(foo)
foo.details['errors'].each do |attr, message|
...
-1
votes
1
answer
130
views
What is the time-complexity of this program (c++)
Code:
#include <iostream>
#include <vector>
template<typename T>
std::vector<T> flatten(std::vector<std::vector<T>> const &vec)
{
std::vector<T> ...
0
votes
0
answers
78
views
Java, calculating complexity
I am trying to calculate complexity of a recursion method inside another methos in a simple while loop.
I dont know how to asnwer this question, I think To just apply the answer O(N*Y) while Y stands ...
0
votes
1
answer
34
views
Complexity of an example with only one for loop
I'm getting started with complexity and I wanted to know why the example given below is O(n^2) and not O(n), would you guys help me? I need it fast for an exam.
l1 = []
for e in range(0, n):
if ...
0
votes
0
answers
88
views
Order of complexity of a matrix (matlab)
I need some help answering this question : How do I find the complexity of the matrix B and f knowing that I have this code :
d = 123456;
randn('state',d); rand('state',d);
n = 1000; A = diag(1+rand(...
1
vote
0
answers
76
views
How do I reduce the cyclomatic complexity in this code?
I added the getCValue method to reduce the cyclomatic complexity, but the cyclomatic complexity still persists, how can I reduce it? Can I change this code using Regex, if I can how? Please help me.
2
votes
2
answers
309
views
Merge similar objects together based on object elements is O(n²). How to make it simpler? [closed]
Problem Description
We have a vector<A> v containing for 4 objects. Each object has the following members x, y, z and w. Two objects are considered equal if the have the same x and y. In that ...
-1
votes
1
answer
162
views
Calculating Cyclometric Complexity?
How do you properly calculate Cyclmetric Complexity?
According to Wikipedia the cyclometric complexity of the following code:
if (c1())
f1();
else
f2();
if (c2())
f3();
else
f4();
is ...
1
vote
1
answer
136
views
Confusion regarding while loop in for loop for time complexity
I'm learning about time complexity and I understand the gist of it, but there's one thing that's really confusing me, it's about understanding the time complexity of a while loop in a for loop.
This ...
0
votes
0
answers
872
views
Find all the index from an array whose sum value is equal to n
So, I needed to find all the index of elements from an array whose sum is equal to N.
For example : Here I want to find indexes whose sum should be equal to 10.
Input : int[] arr = { 2, 3, 0, 5, 7 }
...
1
vote
0
answers
48
views
Code Complexity Misunderstanding of Single Element in a Sorted Array
The problem which is on LeetCode says that
You are given a sorted array consisting of only integers where every
element appears exactly twice, except for one element which appears
exactly once.
...
0
votes
2
answers
503
views
How is the time complexity of the following while loop will be O(logn)?
I did not understand how this piece of code will generate a complexity of O(log n).how many times the statements inside the loop will work?
int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
...
0
votes
0
answers
46
views
Proving algorithm complexity
Hello I have a binary search function and I want to prove that it has Logarithmic complexity.
I have no idea how to prove this and would really use some help
Here are my execution times for the ...
0
votes
0
answers
41
views
What is the order of complexity of the algorithm developed taking the x-axis as the size of the vector? (Code provided)
Hi guys I am practicing my algorithm and complexity theory. I want to know what is the order of complexity of the algorithm developed taking the x-axis as the size of the vector? (Big O notation). ...
0
votes
3
answers
740
views
How can I calculate the complexity of a program like this
So I have been studying the complexity of algorithms, but this one I can't uderstand.
If I use a global variable to check how many times the function is called it will calculate the number 11 then ...
0
votes
0
answers
328
views
Time Complexity of my 2 pointer algorithm
For the given problem below, what would be the time complexity of my solution (below the problem). I think it is not O(n^2), but I am not sure if it is O(n).
Problem: You are given two strings s and t....
1
vote
0
answers
123
views
Runtime of the recursive algorithm?
I am learning algorithm complexities. So far it has been an interesting ride. There is so much going behind the scenes that I need to understand. I find it difficult to understand complexity in ...
2
votes
2
answers
161
views
Running Time Calculation
I am trying to learn time complexities. I have come across a problem that is confusing me. I need help to understand this:
my_func takes an array input of size n. It runs three for loops. In the third ...
0
votes
1
answer
168
views
tree traversal runtime and space analysis general approach
I am currently on 94. Binary Tree Traversal on leetcode and I am not sure how to analyze the run time and space complexity of the question. In my opinion, the time complexity for the question seems to ...
0
votes
1
answer
3k
views
Reduce Class Complexity - Cognitive Complexity C#
How can I improve the Cognitive Complexity in my code?
I have a method which has while loop and inside that lot of IF ELSE blocks, I tried to remove IF ELSE with SWITCH Statements but no improvement ...
0
votes
1
answer
59
views
How to handle a wide variety of object types for the same data
I am building an application which uses a lot of vectors and for which I am using a lot of third party and system assemblies. The result of this is that I have four ways to to represent 3d double ...
1
vote
1
answer
210
views
Python code complexity as Sublime Text 3 Linter
I like the python package radon and use it fairly often to help find/reduce complex code.
I'm normally developing in Sublime Text, and running radon beside in console, but would really like to have ...
7
votes
1
answer
380
views
Sonar integration with lizard to track code complexity failing in android studio
I am trying to integrate lizard to track the code complexity of an android app on sonarqube. The command used to get the complexity report from lizard is:
lizard src/main -x"./test/*" -x&...
1
vote
1
answer
215
views
Can you make a recursive simulation with simpy
I was trying to make a recursive realtime simulation to see it it was possible within the simpy framework.
the function was made to track into a log dictionary...
The jupyter kernel stopped working ...
0
votes
1
answer
618
views
Concise if else in Kotlin
Is there a way to remove this if else block inside snackbar by replacing it with lambda or infix or anything to make it without +2 nesting in cognitive complexity
else {
snackBarBuilder(
...
2
votes
2
answers
593
views
How can I reduce a Cognitive Complexity of its method?
I have an method to convert roman numbers to common decimal. I used here an one for cycle and a lot of "if" conditions.
SonarLint in my IDE says me that Cognitive Complexity of this method ...
0
votes
1
answer
151
views
Asymptotic complexity in python for O-Upper
I'm studying the complexity of functions in python, and I have this two there Im not sure, one because I think it's infinite loop and second is the not in method build in on python:
1- The function f1 ...
0
votes
0
answers
113
views
Counting segments without the function Count
I had the next problem: Given an array, count the number of segments of length k in which this happens; the number of positives in the left-half of the segment is bigger or equal to the right-half.
As ...
0
votes
0
answers
27
views
What will be time and complexity of given code
F(i, j) represents the maximum value the user
can collect from i'th coin to j'th coin.
F(i, j) = Max(Vi + min(F(i+2, j), F(i+1, j-1) ),
Vj + min(F(i+1, j-1), F(i, j-2) ))
As user wants ...
-3
votes
1
answer
373
views
Is O(n^log(n)) bigger than O(n!)
n is a number between 1 and +infinity and this is to know the complexity of an algorithm
I need to see if n^log(n) is bigger than n!
1
vote
0
answers
226
views
What is the time complexity of BitArray initilized to 0?
when creating a BitArray and initializing all of its value to 0 (via constructor, new BitArray(size, false);) what is the time complexity of such operation? It's not in the docs about collections.
...
1
vote
1
answer
69
views
Is this correct time complexity?
I did this as a solution to one of the leetcode problems, but I'm not sure what the complexity of my algorithm is.
public String countAndSay(int n) {
if (n == 1) return "1";
...
0
votes
1
answer
155
views
what is the code-complexity of n-x series?
If you had an algorithm with a loop that executed n steps the first time through,
then n − 2 the second time, n − 4 the next time, and kept repeating until the last time through the loop it executed 2 ...
1
vote
1
answer
258
views
Leetcode 78 - Generate power set time complexity
Leetcode 78 question potential solution:
class Solution(object):
def __init__(self):
self.map = {}
def helper(self, count, nums, vals, result):
if count == ...
0
votes
1
answer
390
views
Determining the exact number of executions of an algorithm
I have algorithm such as:
for(int i=1; i<=n; i++)
for(int j=1; j<=2*n; j=j+2)
for(int k=i; k<=j; k++)
instr;
I need to find a formula that will determine how many times the &...
0
votes
1
answer
390
views
Word Ladder runtime complexity
I am trying to solve the following problem:
Given a beginWord, say pig, and an endWord, say phi, and a list of words wordList, say [phg, phi]. We are allowed to change one character of beginword each ...
1
vote
2
answers
778
views
Time complexity of a recursive function having two nested for loop inside it
function recursion(n){
if n==0
return
for(let i=0; i < n;i++){
for(let j=0; j< n; j++){
console.log('*');
}
}
recursion(n-1);
}
recursion(100);
What is ...
0
votes
1
answer
365
views
Complexity of function with array having even and odds numbers separate
So i have an array which has even and odds numbers in it.
I have to sort it with odd numbers first and then even numbers.
Here is my approach to it:
int key,val;
int odd = 0;
int index = 0;
for(int ...
0
votes
2
answers
397
views
Time Complexity and Space Complexity of the Python Code
Can someone please help me with the time and space complexity of this code snippet? Please refer to the leetcode question- Word break ii.Given a non-empty string s and a dictionary wordDict containing ...
0
votes
1
answer
2k
views
Time complexity of variable assignation
I know that if I have a function like:
public int addOne(int a){
return (a+1)
}
The time complexity order will be O(1) since we only do one operation (the sum).
But what if I have a function that ...
1
vote
2
answers
140
views
What's complexity of this algorithm?
I'm learning how to find algorithm complexity and I can't figure out what's the complexity of this algorithm. Could someone explain to me how to get the answer?
void algorithm(int a, int b) {
...
0
votes
1
answer
58
views
How does one can determine the runtime of the following algorithm
for n in range (1, n):
for j in range(1, n+1):
k = 1
while k <= j:
sumfunc()
k *= 42
(Python code, meaning from 1 to n)
How would one determine the numbers ...