Big Big O (IV)

Which of the following codes run(s) in O ( 1 ) O(1) ?

A:

1
2
3
4
5
6
7
8
for (int i=0; i<n; i++){
    int sum=0;
    for (int j=0; j<n; j++){
        sum+=A[i][j];
    }
    cout<<sum;
    return;
}

B:

1
2
3
4
5
6
7
8
for (int i=0; i<n; i++){
    int sum=0;
    for (int j=0; j<n; j++){
        sum+=A[i][j];
        cout<<sum;
        break;
    }
}

C:

1
2
3
4
5
6
7
8
for (int i=0; i<n; i++){
    int sum=0;
    for (int j=0; j<n; j++){
        sum+=A[i][j];
        cout<<sum;
        return;
    }
}

D:

1
2
3
4
5
6
7
8
for (int i=0; i<n; i++){
    int sum=0;
    for (int j=0; j<n; j++){
        sum+=A[i][j];
    }
    cout<<sum;
    break;
}

This problem is a part of the set: Efficient Algorithms .
All of them A None of them C B D

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Hasmik Garyaka
Sep 16, 2017

C - return in the inner cycle. It will return from the function after first summing.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...