time-complexity

Reduce Time complexity of the following program

送分小仙女□ 提交于 2019-12-24 11:40:41
问题 import java.util.Scanner; class Special_Pairs{ private static Scanner scan; public static void main(String [] args) { byte t; int n; scan = new Scanner(System.in); t=scan.nextByte(); int[] a=new int[100000]; while(t>0) { int i,j,count=0; n=scan.nextInt(); for(i=0;i<n;i++) { a[i]=scan.nextInt(); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(((a[i]&a[j])==0)||((a[j]&a[i])==0)) { count++; } } } t--; System.out.println(count); } } } Help me reduce time complexity of this program Question : You have

Proof time complexity

心已入冬 提交于 2019-12-24 09:29:08
问题 I'm trying to determine the complexity of this two functions, where D in an integer and list is a list of integers: def solve(D, list): for element in List: doFunc(element, D, list) def doFunc(element, D, list): quantityx = 0 if(D > 0): for otherElement in list: if otherElement == element: quantityx += 1 return quantityx + (doFunc ((element+1), (D-1), list)) return 0 Intuitively, I think it has a O(n²) where n is the quantity of elements of list, but I'd like to proof it in a formal way. 回答1:

Why is the time complexity O(n^2) in this code?

蹲街弑〆低调 提交于 2019-12-24 08:18:04
问题 I just didn't get it, why the time complexity is O(n^2) instead of O(n*logn)? The second loop is incrementing 2 each time so isn't it O(logn)? void f3(int n){ int i,j,s=100; int* ar = (int*)malloc(s*sizeof(int)); for(i=0; i<n; i++){ s=0; for(j=0; j<n; j+=2){ s+=j; printf("%d\n", s); } free(ar); } 回答1: By incrementing by two, rather than one, you're doing the following N*N*(1/2) . With big(O) notation, you don't care about the constant, so it's still N*N. This is because big(O) notation

Time complexity of the code containing condition

白昼怎懂夜的黑 提交于 2019-12-24 07:57:27
问题 foo(int n) { int s=0; for(int i=1;i<=n;i++) for(int j=1;j<=i*i;j++) if(j%i==0) for(k=1;k<=j;k++) s++; } What is the time complexity of the above code? I am getting it as O(n^5) but it is not correct. 回答1: The complexity is O(n^4) . Innermost loop will be executed i times for each i . ( i multiples of i within 0..i*i ) It will be like the inner loop will run for j = 0 1 2...i i+1 ...2*i ....3*i .... 4*i .... 5*i... i*i x x x x x x \------/\--------/\-------/ \------/ These x denotes the

Time complexity of the code containing condition

无人久伴 提交于 2019-12-24 07:57:23
问题 foo(int n) { int s=0; for(int i=1;i<=n;i++) for(int j=1;j<=i*i;j++) if(j%i==0) for(k=1;k<=j;k++) s++; } What is the time complexity of the above code? I am getting it as O(n^5) but it is not correct. 回答1: The complexity is O(n^4) . Innermost loop will be executed i times for each i . ( i multiples of i within 0..i*i ) It will be like the inner loop will run for j = 0 1 2...i i+1 ...2*i ....3*i .... 4*i .... 5*i... i*i x x x x x x \------/\--------/\-------/ \------/ These x denotes the

Java programming task efficiency [duplicate]

一个人想着一个人 提交于 2019-12-24 04:16:30
问题 This question already has answers here : Maximum product prefix string (3 answers) Closed 4 years ago . I am trying to learn to write more efficient code. Any chance that some of you genius developers could help me out and let me know where my code is going wrong? How can I make it more efficient? I just completed a task on Codility.com, and I passed all the tests, but my code wasn't efficient enough when larger strings were passed in. Task description: A prefix of a string S is any leading

Time complexity of iterating over a k-layer deep loop nest always Θ(nᵏ)?

一笑奈何 提交于 2019-12-24 01:44:44
问题 Many algorithms have loops in them that look like this: for a from 1 to n for b from 1 to a for c from 1 to b for d from 1 to c for e from 1 to d ... // Do O(1) work In other words, the loop nest is k layers deep, the outer layer loops from 1 to n, and each inner layer loops up from 1 to the index above it. This shows up, for example, in code to iterate over all k-tuples of positions inside an array. Assuming that k is fixed, is the runtime of this code always Θ(n k )? For the special case

two loops but Theta(n)?

心已入冬 提交于 2019-12-24 01:43:28
问题 i = n while (i >= 1) { for j = 1 to i { Function() <- (O(1)) } i = i/2 } The answer is Theta(n) but I do not understand why this is Theta(n). From my understanding, the inner loop will execute n + n/2 + n/4 +...+1 so the total will be O(n) and the outer loop will be executed logn time so the answer should be nlogn. But why this is Theta(n) instead of Theta(nlogn)? 回答1: Your calculations are partially correct. You are simply missing out on a small detail in which you have already calculated

Time complexity for all Fibonacci numbers from 0 to n

人盡茶涼 提交于 2019-12-24 00:43:08
问题 I was calculating the time complexity of this code that prints all Fibonacci numbers from 0 to n. According to what I calculated, the fib() method takes O(2^n) and since it is being called i number of times, so it came out to be O(n*2^n) . However, the book says it is O(2^n) . Can anyone explain why the time complexity here will be O(2^n) ? Here is the code: void allFib(int n){ for(int i = 0 ; i < n ; i++){ System.out.println(i + ": " + fib(i)); } } int fib(int n ){ if(n <= 0) return 0; else

Time complexity of a recursive function with two calls

China☆狼群 提交于 2019-12-24 00:42:13
问题 Consider this code: def count_7(lst): if len(lst) == 1: if lst[0] == 7: return 1 else: return 0 return count_7(lst[:len(lst)//2]) + count_7(lst[len(lst)//2:]) note: the slicing operations will be considered as O(1). So, my inutation is telling me it's O(n*logn), but I'm struggling proving it scientifically. Be glad for help! 回答1: Ok, mathematically (sort of ;) I get something like this: T(n) = 2T(n/2) + c T(1) = 1 Generalizing the equation: T(n) = 2^k * T(n/2^k) + (2^k - 1) * c T(1) = 1 n/2^k