36 /** Returns the number of times second array stores sum of prefix sums from first. 37 public static int example5(int[] first, int[] second) { // assume equal-length arrays 38 int n = first.length, count = 0; for (int i=0; i

icon
Related questions
Question

Give a big-Oh characterization, in terms of n, of the running time of the following
method.

36 /** Returns the number of times second array stores sum of prefix sums from first. */
37 public static int example5(int[] first, int[] second) { // assume equal-length arrays
38
39
// loop from 0 to n-1
40
41
// loop from 0 to n-1
42
// loop from 0 to j
43
44
45
46
47 }
int n = first.length, count = 0;
for (int i=0; i<n; i++) {
int total = 0;
for (int j=0; j<n; j++)
for (int k=0; k<=j; k++)
total += first[k]:
if (second[i] == total) count++;
}
return count;
Transcribed Image Text:36 /** Returns the number of times second array stores sum of prefix sums from first. */ 37 public static int example5(int[] first, int[] second) { // assume equal-length arrays 38 39 // loop from 0 to n-1 40 41 // loop from 0 to n-1 42 // loop from 0 to j 43 44 45 46 47 } int n = first.length, count = 0; for (int i=0; i<n; i++) { int total = 0; for (int j=0; j<n; j++) for (int k=0; k<=j; k++) total += first[k]: if (second[i] == total) count++; } return count;
Expert Solution
Step 1: Code Introduction

ASOLUTION -


In the given code -

1public static int example5(int[] first, int[] second) {
2    int n = first.length, count = 0;
3    
4    for (int i = 0; i < n; i++) {
5        int total = 0;
6        
7        for (int j = 0; j < n; j++) {
8            for (int k = 0; k <= j; k++) {
9                total += first[k];
10            }
11        }
12        
13        if (second[i] == total) {
14            count++;
15        }
16    }
17    
18    return count;
19}
20

This method is used to we will returns the number of times second array stores sum of prefix sums from first


steps

Step by step

Solved in 3 steps

Blurred answer