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
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; }...
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;...
(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; }
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)
Give the time complexities (Big-O notation) of the following running times expressed as a function of the input size N. a) N12+ 25N10+ 8 b) N + 3logN + 12n√n c) 12NlogN + 15N2 logN
1. Analyze the running time of the following algorithm and write it using ( notation. You must show detailed calculation/derivation of your running time along with table to get marks. int sum = 0; for (int i=n; i)=1; i=i/2) { for (int j=1; j<=i; j*=2) { sum+=i*j; } }
Consider the following algorithm BAR that gets a positive integer and works as follows: BAR(int n): - if (n <=0 ), return 0; - else, return (n - BAR(BAR(n-1))); (A.) What will be the output of BAR(3)? (B.) Write an algorithm that has the same functionality as BAR, so that the runtime of BAR(n) is O(n)?