Question
I need answer for the following questions with explanations: 1. What is the time complexity of the code below? void function(int[] array) {
I need answer for the following questions with explanations:
1. What is the time complexity of the code below?
void function(int[] array) {
int sum = 0;
int product = 1;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
for (int i = 0; i < array.length; i++) {
product *= array[i];
}
System.out.println(sum + ", " + product);
}
a. O(N)
b. O(LOG N)
c. O(N^2)
d. O(4N)
2. What is the time complexity of the code below?
void printPairs(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.println(array[i] + "," + array[j]);
}
}
}
a. O(N^2)
b. O(2N)
c. O(LOG N)
d. O(N)
3. What is the time complexity of the code below?
String function(int num) {
for (int i = 1; i <= Math.min(num, 10); i++) {
return String.format("%s", i);
}
}
a. O(1)
b. O(N)
c. O(LOG N)
d. O(N LOG N)
4. What is the time complexity of the code below?
void function(int[] array) {
for (int i = 1; i < array.length/2; i++) {
System.out.println(i + " ");
}
}
a. O(N)
b. O(LOG N)
c. O(SQRT(N))
d. O(2N)
5. What is the time complexity of the code below?
void printUnorderedPairs(int[] arrayA, int[] arrayB) {
for (int i = 0; i < arrayA.length; i++) {
for (int j = i; j < arrayB.length; j++) {
if (arrayA[i] < arrayB[j]) {
System.out.println(arrayA[i] + "," + arrayB[j]);
}
}
}
}
a. O(N^2)
b. O(N)
c. O(LOG N)
d. O(N LOG N)
6. What is the time complexity of the code below?
void function(int[] array) {
int a = 0;
int i = array.length;
while (i > 0) {
a += i;
i /= 2;
}
}
a. O(LOG N)
b. O(N LOG N)
c. O(N)
d. O(N/2)
7. What is the time complexity of the code below?
void function(int num) {
for (int j = 1; j < num; i++) {
for (int i = 1; i < num; i = i * 2) {
System.out.print(i + " ");
}
}
}
a. O(N LOG N)
b. O(N^2)
c. O(N)
d. O(LOG N)
8. What is the time complexity of the code below?
void function(int num) {
for (int j = 1; j < 100; i++) {
for (int i = 1; i < num; i++) {
System.out.print(i + " ");
}
}
}
a. O(N)
b. O(N^2)
c. O(LOG N)
d. O(N LOG N)
9. What is the BEST CASE of the code below?
void function(int num) {
if (num < 10) {
System.out.print("num is less than ten");
} else {
for (int i = 0; i < num; i++) {
System.out.print(i + " ");
}
}
}
a. O(1)
b. O(N)
c. O(N LOG N)
d. O(N^2)
10. What is the time complexity of the code below?
void function(int num) {
for (int j = 1; j < 100; i++) {
for (int i = 1; i < 100; i++) {
System.out.print(i + " ");
}
}
}
a. O(1)
b. O(N^2)
c. O(N)
d. O(LOG N)
11. What is the worst case time complexity of deleting an item from an unsorted Array?
a. O(N)
b. O(1)
c. O(N LOG N)
d. O(LOG N)
12. What is the worst case time complexity of retrieving an element at a given index from an unsorted Array?
a. O(N)
b. O(1)
c. O(LOG N)
d. O(N^2)
13. What is the slowest time complexity?
a. O(N!)
b. O(2^N)
c. O(N^2)
d. O(N LOG N)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Lets go through each question one by one 1 Time Complexity ON N ON Both loops iterate through the array once independently of each other so the time c...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