Big-O notation.
Consider the following function.
int func1(int n) {
int sum = 0, i;
for(i = 0; i<n; i++;) {
sum += i;
return sum;
}
Express the running time of func1 as a function of n using big-O notation.
Write a function that has the same functionality as func1, but runs in O(1) time.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Since there is 1 loop over n. So, it is O(n)
The function with O(1) complexity is
int func1(int n) {
int sum = 0, i;
sum=(n)*(n-1)/2;
return sum;
}
Kindly revert for any queries
Thanks.
Big-O notation. Consider the following function. int func1(int n) { int sum = 0, i; for(i...
Find Big-O notation for the following algorithm:
int function9(int n) { int ij for (i-0; in; i++) for (0; j<n; j++ if (j1) break return j; }
int function9(int n) { int ij for (i-0; in; i++) for (0; j
Consider the following piece of code: sum = 0; for (i=1; i<= f(n); i++) sum +=i; where f (n) is a function call. Give a tight big-oh bound on the running of this piece of code as a function of n, on the assumption that (a) The running time of f (n) is O(n), and the value of f (n) is n! (b) The running time of f (n) is O(n), and the value of f (n) is n (c)...
Consider the following loop nest: int sum = 0; for(int j = 0; j < N * N; j += 2) for(int i = 2*N; i > 0; i--) sum++; What is the Big-O behavior? Group of answer choices O(1) O(log N) O(N) O(N log N) O(N2) O(N3) 2.Consider the following loop nest: int sum = 0; for(int j = 1; j < N; j *= 2) for(int i = 0; i < N; i += 2) sum++; What is...
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) for(int i=n-1; i >=0; i--){ for(int k=0; k < i*n; k++){ // do something that takes O(1) time } }
Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; } Which of the following characterization, in terms of n, of the running time of the above code (F1) is correct? Θ(n3/2) · O(1/n) · O(n) · Ω(n2) Consider the following code: Void F1 (int n) { int a; for(int i = 0; i < n; i += 2) a = i; }...
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) int *a = new int [10]; // new is O(1) int size = 10; for (int i = 0; i < n; i ++) { if (i == size) { int newsize = 3*size/2; int *b = new int [newsize]; // new is O(1) for (int j = 0; j < size; j ++)...
(c) int sum(int n) un { int sum=0; for (int i=0; i<n; i++) for(int j=0; j<i/2; j++) for(int k=0; k<min(j,5); k++) { sum=sum+1; } return sum; }
Big-O notation for each
public static double accumulate (double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum; } public static double innerProduct(double[] a, double[] b) { // assume a. length == b.length double sum = 0; for (int i = 0; i < a.length; i++) sum += a[i] * b[i]; return sum; } public static int twoSum(int[] a) { int count = 0; for (int i = 0;...
Here again is the function from the previous question. Using big-oh notation, what is the best-case runtime for this function? int is_sorted( int *array, int n) { int i; for (i = 0; i < n - 1; i++) if (array [i] > array [i + 1]) return 0; return 1; }
Please specify Time and Space Complexities in terms of the Big-O notation. for (int j = 1; j < n; j = 2 * j) sum += j; Question 8 options: O(n^2) O(n log n) O(log n) O(n) O(1)