How to calculate time complexity of this code?

What is the time complexity for my code with showing your steps? I tried to figure it out by doing O(T*(n+n+n)) = O(T*n). Am I correct? And any suggestions to make this code more efficient? (Problem here: shorturl.at/nouEK)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int T, n;
cin >> T;
while (T--) { //O(T)
    //inputting p[n]
    scanf_s("%d", &n);
    int* p = new int[n];
    for (int i = 0; i < n; i++) { //O(n)
        scanf_s("%d", &p[i]);
    }

    vector <int>indeces;
    vector <int>afterIndex;

    while (n != 0) { //o(n)
        //*itr = largest element in the array
        auto itr = find(p, p + n, *max_element(p, p + n));
        int index = distance(p, itr);
        indeces.push_back(index); //push_back index
        afterIndex.push_back(n - index - 1); //push_back number of elements after the index
        //deleting element by decreasing n:
        n = index;
    }

    int i2 = 0;
    int i = indeces[i2];
    while(i2!=indeces.size()){ //let's say it's o(n)
        cout << p[i] << endl;
        i++;
        if (i > indeces[i2] + afterIndex[i2]) {
            i2++;
            if (i2 == indeces.size()) {
                break;
            }
            i = indeces[i2];
        }
    }


    delete[] p;
}
return 0;
Last edited on
Don't double-post.
http://www.cplusplus.com/forum/general/277131/#msg1196170

Delete your post or green-tick it to finish.
Last edited on
Topic archived. No new replies allowed.