Time limit exceeded

I've been stuck on this problem (https://codeforces.com/contest/1492/problem/B) for a pretty Long while. I always get "Time limit exceeded" when I submit the code.
My solution is to input the items of the array then determine the largest number in the array and diplay it along with the elements following it and so on.
How could I make my algorithm more efficient?
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int T, n;
    cin >> T;
    while (T--) {
        //inputting p[n]
        scanf_s("%d", &n);
        int* p = new int[n];
        for (int i = 0; i < n; i++) {
            scanf_s("%d", &p[i]);
        }

        while (n != 0) {
            //*itr = largest element in the array
            auto itr = find(p, p + n, *max_element(p, p + n));
            int index = distance(p, itr);

            for (int i = index; i < n; i++) {
                printf("%d\n", p[i]);
            }
            //deleting element by decreasing n:
            n = index;
        }

        delete[] p;
    }
    return 0;
}
Last edited on
@bassel27
Please don't start a new thread for the same problem:
http://www.cplusplus.com/forum/general/277131/#msg1196170

You have clearly not even bothered to read the comments in that thread.

And we still can't do anything with the link you have attempted to provide, so we can't see what on earth you are trying to do. We aren't mind readers.
My solution is to input the items of the array then determine the largest number in the array and diplay it


The quickest way to determine the largest number is as the numbers are being entered - not as a separate step.

along with the elements following it and so on.


This I don't understand. What is 'and so on'. It would help if you provided a small input sample and the expected output.

Perhaps something like:

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
#include <iostream>
#include <limits>

int main() {
	size_t T {};

	std::cin >> T;

	for (size_t n {}; T--; ) {
		std::cin >> n;

		const auto p {new int[n] {}};
		int max {std::numeric_limits<int>::min()};

		for (size_t i = 0; i < n; ++i) {
			std::cin >> p[i];
			if (p[i] > max)
				max = p[i];
		}

		std::cout << "The largest value is " << max << '\n';

		delete[] p;
	}
}


.
Last edited on
@lastchance: I don't understand you, you complain about an unnecessary new thread but keep replying to it instead of going to the original one.
Topic archived. No new replies allowed.