Category "time-complexity"

Big(O) time complexity unable to find

What is time complexity of following code : int count = 0; for (int i = N; i > 0; i /= 2) { for (int j = 0; j < i; j++) { count += 1;

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) {

Fast matrix determinant calculation with specific structure

I have a k*k squared matrix with diagonal elements x>0 and all other elements y>0. The values of k, x, y are all subject to change. Now I need the determi

Having trouble computing time complexity of a given function in Python

I am learning about time complexity now, and I am working with BST (Binary Search Trees). This question needs some context and this is a follow up post to this

The time complexity of the map function

I heard the calculation amount of map function is O(1). But I can't understand the reason.

Algorithm to find the most frequent words [closed]

I can't think of an algorithm for my code. Please help me out. We have two files. The first one has text (Text length is n-words). The second

Time complexity - understanding big-Theta

I'm currently taking algorithms and data structure. After nearly two months of studying, I still find time complexity extremely confusing. I was told (by my pr

What's the time complexity of prod() in math in Python

Summary) What's the time complexity of math.prod() How to improve this code to get to the pass Details) Currently working on 'Product of Array Except Self' on L

Is there a known algorithm for finding which K elements out of N elements have a sum that is closest to a whole number?

As a small example, say I have N=6 elements { 0.03, 0.25000039, 1.391, 500.1, 0.5000001, 1.75001 } and K=3 then the combination { 0.25000039, 0.5000001, 0.

Constant space, one pass, daily coding problem

This is the Daily Coding Problem: “Given a singly linked list and an integer k, remove the kth last element from the list. k is guaranteed to be smaller

What is the time-complexity of the pseudo-inverse in pytorch (i.e. torch.pinverse)?

Let's say I have a matrix X with n, m == X.shape in PyTorch. What is the time complexity of calculating the pseudo-inverse with torch.pinverse? In other words,

Recursive and Iterative Binary Search: Which one is more efficient and why?

I have written the algorithms for recursive and iterative binary search: Recursive AlgorithmBinSrch(a, i,l,x) // Given an array a[i :l] of elementsin nondecre

Leetcode word ladder : How is this solution most efficient?

I'm doing this question in Leetcode : https://leetcode.com/problems/word-ladder/ My solution was a BFA approach, and only one nested loop: def ladderLength(self

Finding Big O of the Harmonic Series

Prove that 1 + 1/2 + 1/3 + ... + 1/n is O(log n). Assume n = 2^k I put the series into the summation, but I have no idea how to tackle this problem. Any he

String comparison time complexity

Which comparison would take longer time? a = helloworldhelloworldhelloworldb = https://www.somerandomurls.com/directory/anotherdirectory/helloworld.htmlif a !=

Nested for loop in Big Oh Complexity

for(int i = 0; i < n; i++) { for(int j = 0; j < i; j++){ // do swap stuff, constant time } } I read that single for loop is O(N) and trav

What is O(log(n!)) and O(n!) and Stirling Approximation

What is O(log(n!)) and O(n!)? I believe it is O(n log(n)) and O(n^n)? Why? I think it has to do with Stirling Approximation, but I don't get the explanation v

Squares of a Sorted Array, why sorted() method is faster than O(n) method? [closed]

I am working on leetcode algorithm problem 977. Squares of a Sorted Array. Why the submissions using built-in method sorted is faster than my

Using Big O Notation, what is the correct label for this algorithm?

I am curious. What is the correct way to describe this using Big-O Notation? var prices = [100, 180, 260, 590, 40, 310, 535, 10, 5, 3]; var biggest_profit = 0;

How does sorting with an index work in MongoDB?

I'm wondering how sorting with an index actually works in MongoDB. There are a couple articles in the MongoDB documentation, but they don't actually describe ho