Assuming that binary search is called upon a subarray of approximately length n/2 and that there are at most three comparions at a level I came up with T(n) = T(n/2) + 3 as a recurrence relation. But: When I call binarySearch recursively isn't the cost of the slice proportional...

I had to determinate big O complexity of this piece of code. I thought the answer is nlogn but apparently its n. Can anyone help explain why that is so? void funct(int n) { for (int i = n; i > 0; i /= 2) for(int j = 0; j...

Apparently the correct answer to the following question is (C), but why are the other options not correct until we know the value of n? If n=1, all of these seem correct except (B)! Where am I going wrong? Which of the following is not O(n2)? (A) 1510 * n...

I know that every for loop is a O(log₂n), but I am not sure what 3 of them together would make? O(3⋅log₂n)? Thank you guys. for (int i = n; i > 0; i = i / 2) { for (int j = n; j > 0; j = j...

Consider the following functions: f(n) = 2^n g(n) = n! h(n) = n^logn Which of the following statements about the asymptotic behavior of f(n), g(n), and h(n) is true? (A) f(n) = O(g(n)); g(n) = O(h(n)) (B) f(n) = \Omega(g(n)); g(n) = O(h(n)) (C) g(n) = O(f(n)); h(n) = O(f(n))...

So, basically I'm implementing an algorithm to calculate distances from one source node to every other node in a weighted graph, and if a node is in a negative cycle, it detects and marks that node as such. My question regards the total time complexity of my algorithm. Assume V...

Given two discrete random variables, their (arbitrary) probability mass functions a and b and a natural-number N such that both of the variables have the domain [0..N] (therefore the functions can be represented as arrays), the probability that the functions' corresponding random variables have a given sum (i.e. P(A+B==target)) can...

I'm taking Data Analysis and Algorithms in the Summer. The question: Find a Θ-notation in terms of n for the number of times the statement x = x + 1 is executed. for i = 1 to 526 for j = 1 to n^2(lgn)^3 for k = 1 to n...

I've found the following statement: If a program P for Non Deterministic Turing Machine solves a decision problem in time limited by a polynomial p(S), where S-size of input, then it can be run on a Deterministic Turing Machine, and the solution will be found in time limited in time...

My question is almost the same that the one I asked few months ago : 2^N Combinaisons with Integers (Kernel), how to generate them? Basically, I wanted to have the 2^N combinaisons inside a Kernel, but I generalized my version, and now it's even more complicated : I don't want...

I’m needing some help understanding algorithm analysis using Big Theta Θ notation: Big O is the most commonly used asymptotic notation for comparing functions, although in many cases Big O may be replaced with Big Theta Θ for asymptotically tighter bounds. How would I analyze this algorithm step by step?...

While searching for an answer of Big O Notation I have googled a lot and have also seen many SO answers like link 1, link 2, link 3 and many more, but still I have not clearly understood some points. One of them is why do we ignore / remove...

I wanted to know if my answers are indeed correct for the following statements: 3(n^3) + 5(n^2) + 25n + 10 = BigOmega(n^3) -> T ->Grows at a rate equals or slower 3(n^3) + 5(n^2) + 25n + 10 = Theta(n^3) -> T -Grows at a rate exactly equals 3(n^3)...

Is a List in C# equal to that of an ArrayList in terms of time complexity? As it is my understanding that they're both similar in how they function, being dynamic arrays?...

Problem: You have an algorithm that divides n size problem to six subproblems with size of one quarter of the original. For dividing the algorithm makes 100 steps and for merging 75n. What's the time asymptotic complexity of an algorithm? So the formula for Master Theorem and for this problem...

How is an approximation Factor different than time-complexity? I have heard, for example, of polynomial algorithms with exponential factors, what does that mean? Does that mean it is not technically in polynomial time?

string str = "abcdefghijklmnopqrstuvwxyz"; int sum = 0; for(int i=0; i<str.length; i++) { int val = str[i]; while(val > 0) { sum = val % 62; val = val / 62; } } I know that parent loop executes n+1 time and the child loop executes (val)^(1/62) times. For parent...

What is the run-time complexity of this Code? #include <cstdio> #include <cstring> int main() { int n, i, l, j, c, ans = 25; scanf("%d", &n); char x[21], y[21]; scanf("%s", &x); l = strlen(x); for(i=1; i<n; i++) { scanf("%s", &y); c = 0; for(j=0 ;j<l; j++) if(x[j] == y[j]) c++;...

This algorithm find all prime numbers below N var f = function(n){ var primes = [2]; //1 var flag; //1 for(var i=3; i<n; i+=2){ // ( from 3 to n-1 ) / 2 flag = true; //1 var startI = 0; // 1 for(var y=primes[startI]; y<=Math.sqrt(i); y=primes[++startI]){ // ??? if(i%y...

first function:- void strange (int n,int k) { int i; if(k > n) return; for(i=k; i<n; i++) printf("?"); strange(n, k+2); return; } second function:- void weird(int n, int k) { int i; if(n <= 0 || (k <= 0)) return; k *=2; for(i=0; i < k; i++){ printf("?"); weird(n/2, -n/2...

I have the following function to prove that its time complexity is less or equal to O(xlogx) f(x) =xlogx+3logx2 I need some help to solve this....

I am trying to figure out whether the following problem is NP-hard: Given G_1,..,G_n subsets of {1..m} c_1,..,c_n non-negative integers in {0..m} Find T subset of {1..m} S.T. for all i=1..n, T intersects G_i in exactly c_i elements I tried to find reductions to NP problems such as coloring, or...

I have an algorithm and I need help finding the complexity of it (tightest possible upper bound) for(int i = 0; i < n/2; i++) for(int j = 0; j < n/4; j++) for(int k = 0; k < n; k++) x++; My analysis is that if n would not...

The running time for this recurrence relation is O(nlogn). Since I am new to algorithm how would I show that mathematically? T(n) = 2⋅T(n/2) + O(n) T(n) = 2 ( 2⋅T(n/4) + O(n) ) + O(n) // since T(n/2) = 2⋅T(n/4) + O(n) So far I can see that if...

I have a function for(int i=0;i<n; i++) { b[i]=0; for(int j=0;j<5;j++) { b[i]=a[j+i] } } I need to calculate big-O of above function. My answers is: Inner loop run 5n time => O(n). So the total complexity is O(n). I think I have a mistake in my calculations, but I...

The question comes from Introduction to Algorithms 3rd Edition, P63, Problem 3-6, where it's introduced as Iterated functions. I rewrite it as follows: int T(int n){ for(int count = 0; n > 2 ; ++count) { n = n/log₂(n); } return count; } Then give as tight a bound as...

I have the following algorithm but I dont know its' complexity. Could someone help me? Input size is n. int x = n; while (x > 0) { System.out.println("Value is" + x); x = x/5; } Thanks a lot!...

I have the formula a(n) = n * a(n-1) +1 ; a(0) = 0 How can i get the Omega, Theta or O Notation from this without the Master Theorem or did anyone have a good site to understand the explanation...

According to the theory of ADTs (Algebraic Data Types) the concatenation of two lists has to take O(n) where n is the length of the first list. You, basically, have to recursively iterate through the first list until you find the end. From a different point of view, one can...

What is the space complexity of storing a path ie. nodes of a binary tree from root to a particular leaf in an array? Basically, I am looking for space complexity of the following algorithm: public void printPath () { doPrint(root, new ArrayList<TreeNode>()); } private void doPrint(TreeNode node, List<TreeNode> path)...

/* * Program to group anagrams from the string array input */ import java.util.*; public class StringArrayAnagrams { //function to group the anagrams together public static void groupAnagrams(String[] inputArray) { Hashtable<String, ArrayList<String>> store = new Hashtable<String, ArrayList<String>>(); //to store the sorted string as keys and words from input array that...

What is the complexity of function f(n),preferably the Big-O notation, and f(n) satisfies the condition n = f(n).log(f(n)) ,f(n) > 1 .Let assume that log in base 2. I tried to isolate f(n) from the condition but could not get it done. After using excel to get the graph of...

So, lets say you have a function, X(N) that is a total black box. You don't know the growth rate of the function, you can't look it up, and you can't view the source (at the moment). Next lets examine it in the context of another function for(int i =...

I give you three short codes: First code: procedure Proc (n:integer) begin if n>0 then begin writeln('x') Proc(n-2) writeln('*'); Proc(n-2) end end Second code: procedure Proc (n:integer) begin if n>0 then begin writeln('*'); Proc(n-1) end end Third code: procedure Proc (n:integer) begin if n>0 then begin writeln('x') Proc(n/2) writeln('*'); Proc(n/2)...

I have to find the Best and worst case of an algorithm, but I don't understand the results: int chOne=1; for (int i=0; i<list.lenght; i++) if(list[i]<list[chOne]){ chOne=i; } return chOne; BC:2c+2c(n-1)=2cn WC:2c+3c(n-1)=3cn-c I don't know what is the "n-1"; and following other similar exercises, it would be (I think) BC:5c...

I am trying to prove that 4^n is not in the order of O(2^n). Is this a valid method ? 4^n >= c*2^n => 4^n/2^n >= c => 2^n >= c I got lost here......

int f2(int n) { int x, y, z = 0, i; for(x = n, i = 0; i < n; i ++, x *= n) { y = x; while (y > 1) { y /= 3; z += y; } } return z; } I get confused by the...

I have the following pseudocode: SelectionSort(A) n = A.length for j=1 to n-1 smallest = j for i=(j+1) to n if A[i] < A[smallest] smallest = i exchange A[j] with A[smallest] I think that the first for-loop test will execute n times, and the nested for-loop will execute 1 +...

Note: This is problem 4.3 from Cracking the Coding Interview 5th Edition Problem:Given a sorted(increasing order) array, write an algorithm to create a binary search tree with minimal height Here is my algorithm, written in Java to do this problem public static IntTreeNode createBST(int[] array) { return createBST(array, 0, array.length-1);...

I have 2 binary search trees T1 and T2 with same number of nodes n >= 1. For each node P we have LEFT(P) and RIGHT(P) for links between nodes and KEY(P) for value off the node. The root of T1 is R1 and root of T2 is R2. I...

I am trying to calculate the time complexity of the next function, max_list11, which finds a maximum of a list recursively: def max11(L,left,right): if left==right: return L[left] return max(L[left], max11(L,left+1,right)) def max_list11(L): return max11(L,0,len(L)-1) From what I found out, the time complexity should be O(n), since what the function does...

I tried to solve the following exercise : What is the order of growth of the worst case running time of the following code fragment as a function of N? int sum = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <=...

Why is O(2^log(n)) equivalent to O(n)? Also why is this considered as an exponential run time and not a polynomial run time?

I have a (C#) function that checks four sets of conditions and returns a bool. If any of them are true, it returns true. I'm sure I could simplify the logic but I want it to be fairly readable. The CodeMaid extension in Visual Studios and tells me the cylomatic...

Imagine 2D plane with n objects. Pick any point on the plane and find which object is the nearest. The obvious solution is to calculate the distance from selected point to all n objects and choose the shortest distance but isn't there more optimal algorithm? Something like a binary tree,...

x=0 for i=1 to ceiling(log(n)) for j=1 to i for k=1 to 10 x=x+1 I've included the answer I've come up with here: I think the time complexity is θ(n^2 log(n)), but I am not sure my logic is correct. I would really appreciate any help understanding how to do...

According to Wikipedia article on Exponential growth E.g. if a slow processor can solve problems of size x in time t, then a processor twice as fast could only solve problems of size x+constant in the same time t. My question is, how do we calculate the constant value that...

I am expected to find the Big O notation for the following complexity function: f(n) = n^(1/4). I have come up with a few possible answers. The more accurate answer would seem to be O(n^1/4). However, since it contains a root, it isn't a polynomial, and I've never seen this...

I came across the following question: Suppose I modify a given sorted list of 4n distinct numbers as follows: Keep elements in even positions (positions 2, 4, 6, ... 4n) as they are. Form n disjoint pairs (i,j) on the odd numbered positions where i = 2k+1 for some k...

I'm finding it difficult to understand why/how the worst and average case for searching for a key in an array/list using binary search is O(log(n)). log(1,000,000) is only 6. log(1,000,000,000) is only 9 - I get that, but I don't understand the explanation. If one did not test it, how...

I know that brute force approach to do this is perform DFS on all the vertices of the graph.So for this algorithm the complexity would be O(V|V+E|). But is there more efficient way to do this?

Many times I've heard that if we can reduce problem A to problem B in polynomial time, then the problem B is at least as hard as problem A. How precise is this statement? I believe we should understand it in this way: if A can be poly-time reduced to...