/** Returns the sum of the integers in given array. */ public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j = 0; j

icon
Related questions
Question

Give a big-Oh characterization, in terms of n, of the running
time of following methods. (Please show me the steps to solve )

1 /** Returns the sum of the integers in given array.
public static int example1(int[] arr) {
2
3
int n = arr.length, total = 0;
for (int j=0; j<n; j++)
total += arr[j];
return total;
4
5
6
7
8
9 /** Returns the sum of the integers with even index in given array. */
10 public static int example2(int[] arr) {
11
int n = arr.length, total = 0;
}
12
13
14
15 }
for (int j = 0; j<n; j+= 2)
total += arr[j];
// loop from 0 to n-1
return total;
// note the increment of 2
Transcribed Image Text:1 /** Returns the sum of the integers in given array. public static int example1(int[] arr) { 2 3 int n = arr.length, total = 0; for (int j=0; j<n; j++) total += arr[j]; return total; 4 5 6 7 8 9 /** Returns the sum of the integers with even index in given array. */ 10 public static int example2(int[] arr) { 11 int n = arr.length, total = 0; } 12 13 14 15 } for (int j = 0; j<n; j+= 2) total += arr[j]; // loop from 0 to n-1 return total; // note the increment of 2
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer