Question: What is the time complexity for each class? O ( . . . . ) ? Please explain why it's like that. 1 . Problem

What is the time complexity for each class? O (....)? Please explain why it's like that.
1. Problem 1- while loop with nested for loop
public static void foo1(int[] arr){
int j =1;
int n = arr.length;
while (j < n){
for (int k =0; k < arr.length; k++){
System.out.print("*");
}
System.out.println("$ ");
j = j *2;
}
System.out.println();
}
2. Problem 2- a for loop
public static void foo2(int[] arr){
for (int i =1; i < arr.length; i *=2){
System.out.print("# ");
}
System.out.println();
}
3. two for loops
public static void foo3(int[] arr){
for (int i =1; i < arr.length; i *=2){
System.out.print("@ ");
}
System.out.println();
for (int j =0; j < arr.length; j++){
System.out.print("& ");
}
System.out.println();
}
4. Problem 4- a for loop
public static void foo4(int[] arr){
for (int j =0; j < arr.length; j++){
System.out.print("%");
}
System.out.println();
}
5. Problem 5- two nested for loops looking for two numbers that sum up to the target
public static int[] twoSum(int[] nums, int target){
for (int i =0; i < nums.length; i++){
System.out.print(nums[i]+":\t ");
for (int j = i +1; j < nums.length; j++){
System.out.print(nums[j]+",");
if (nums[i]+ nums[j]== target){
return new int[]{ i, j };
}
}
System.out.println();
}
return null;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!