Big Big O (IV)

Which of the following codes run(s) in \(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.
×

Problem Loading...

Note Loading...

Set Loading...