Question
Given the following pieces of code, choose the Big O complexity. (If there is more than one option, circle the tightest asymptotic bound.) Give a
Given the following pieces of code, choose the Big O complexity. (If there is more than one option, circle the tightest asymptotic bound.)
Give a 1-2 sentence explanation of your reasoning.
a)
p = 1;
for (int j=0; j < n; j++) {
if (a[j] != 0) {
p = p * a[j];
}
} return p;
where n is a positive integer and array a[] has all float values. i) O(1) ii) O(n) iii) O(n2)
iv) O(log n) b)
bool isUndirected (int n, float **a) {
bool result = true;
for (int i=0; i < n; i++) {
for (int j = 0; j < n ; j++) {
if ( (i != j) && (a[i][j] != a[j][i])) {
result = false;
}
}
} return result;
}
where n is a positive integer and the 2D matrix a[ ][ ] has all float values. i) O(1) ii) O(n) iii) O(n2)
iv) O(log n) c) float p1 = 1;
for (int i=0; i < n; i++) {
if (a[i] < 0) {
p1 = p1 * a[i];
}
}
float p2 = 1;
for (int j=0; j < n; j++) {
if (a[j] > 0) {
p = p * a[j];
}
} float pp = (abs(p1) > p2)? abs(p1) : p2;
return pp;
i) O(1) ii) O(n) iii) O(n2)
iv) O(log n)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started