Question: Consider the following Java method that calculates sum of all elements in an array: public static int sum ( int [ ] arr ) {

Consider the following Java method that calculates sum of all elements in an array:
public static int sum(int[] arr){
int total =0;
for (int i =0; i < arr.length; i++){
total += arr[i];
}
return total;
}
a) What is the time complexity of the sum method in terms of Big-O notation? Justify your answer. (1 mark)
b) Suppose the array arr has n elements. If the array contains very large numbers, what is the best-case time complexity of the sum method? Explain briefly. (1 mark)

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!