-5

I want to know the time complexity of my code. How can I calculate it?

{
    int q;
    int w;
    cout <<"please enter values" <<endl; 
    cin>>q;
    for(w = 0; w<q; w++)
    {
        int p; 
        int o;
        int sum = 0;
        cin>>p;
        for(o = 0; o < p; o++)
        {
            int x; 
            int y;
            int z;
            cin>>x ;
            cin >>y;
            cin>>z;
            sum = sum + (x*z);
        }
        cout<<sum<<endl;
    }
    _getch();
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    you should show us some attempts of yours so that we can see what's your problem with it – YakovL Jan 04 '19 at 11:24
  • Possible duplicate of [How to determine simplex time complexity (ie Max flow)](https://stackoverflow.com/questions/8650426/how-to-determine-simplex-time-complexity-ie-max-flow) – Prune Jan 04 '19 at 17:08

1 Answers1

0

The first for loop will run q times, which is a time complexity of O(q).
The second (inner) for loop will run p times, thus, it has a time complexity of O(p) and since it is called once for every run of the first loop, their complexities multiply like this:

O(q) * O(p) = O(q * p)

In general, nested loops multiply and consecutive loops add.

deHaar
  • 17,687
  • 10
  • 38
  • 51