Question
public class Lab02P4Wrapper { public static int[] productExceptSelf(int[] nums) { /*ADD YOUR CODE HERE*/ int len = nums.length; if(len == 0) return null; //Dummy Return
public class Lab02P4Wrapper { public static int[] productExceptSelf(int[] nums) { /*ADD YOUR CODE HERE*/ int len = nums.length; if(len == 0) return null; //Dummy Return //Creating 3 arrays. int leftProduct[] = new int[len]; int rightProduct[] = new int[len]; int answer[] = new int[len]; /* finding product from left to right */ leftProduct[0] = 1; for (int i = 1; i < len; i++) { leftProduct[i] = leftProduct[i-1] * nums[i-1]; } /* finding product from right to left */ rightProduct[len - 1] = 1; for (int i = len - 2; i >= 0; i--) { rightProduct[i] = rightProduct[i+1] * nums[i+1]; } /* finding the final product */ for (int i = 0; i < len; i++) { answer[i] = leftProduct[i] * rightProduct[i]; } return answer; } public static void main(String[] args) { /* Testing */ int nums[] = {1, 2, 3 , 4}; int answer[] = productExceptSelf(nums); System.out.print("Answer: "); for (int i = 0; i < answer.length; i++) System.out.print(answer[i] + " "); //Dummy Return } }
For the problem solved above, answer the following items:
What is the running time of the algorithm implemented? Explain your thought process behind the implementation
Describe in detail the running time breaking down each part of the code implemented into its respective runtimes
Does this code have a more optimal solution than the one implemented? If so, explain why, how and what would you change from your code to optimize said solution
If your code does not meet the minimum runtime requirements, add below a pseudocode of your implementation for the optimal solution. It does not have to follow any specific syntax, it's pseudocode. (If it does meet the minimum requirements, ignore this question)
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