AITC2G11 - Editorial

PROBLEM LINK:

Practice

Contest Link:

Setter: Rahul Kumar

Tester: Rahul Kumar

Editorialist: Anushwar Sharma

DIFFICULTY:

Simple

PREREQUISITES:

Basic observations, Array.

PROBLEM:

One day, Chef is planning to go climbing a mountain. The mountain has many peaks with different heights, and those peaks are the checkpoints. There are N checkpoints with their heights A_1,A_2,A_3... A_N. Chef wants to calculate the highest peak, so Chef starts moving from A_1 at every checkpoint he notes down the maximum height he has reached till then.

You have to print the Chef’s data at every checkpoint.

EXPLANATION

Our task is to iterate through the array checking every index starting from index = 0 till index = n-1 where n is the length of the array.
We take the initial index and push it into our result array, as initially, it will be the biggest value.
as we iterate to the next step, we then compare the peak value stored in our results array and then compare it with the next value of the array. If the value is greater than the peak index value, we update our peak value, store it inside our result array in the next index, and then continue our iteration. If the value is equal to or less than the peak value, we keep the value the same and push the same value as last in our array.

SOLUTION:

Editorialist's Solution
#include<bits/stdc++.h>

using namespace std;

int main() {
		int t;
		cin>>t;
		while(t--)
		{
		    int n , y=0;
		    cin >> n;
		    vector <int> res;
		    for (int i=0; i<n; i++){
		        int x;
		        cin>>x;
		        res.push_back(x);
		    }
		    
		    for(int i=0;i<n;i++){
		        if(res[i] > y){
		            cout<<res[i]<< " ";
		            y = res[i];
		        }
		        else {
		            cout << y << " ";
		        }
		        cout << "\n";
		    }
		}
	return 0;
}

Feel free to share your approach, if you want to(even if it’s the same). Suggestions are welcomed as always had been. :slight_smile: