padfoot1717's blog

By padfoot1717, history, 3 years ago, In English

I am trying to solve this problem, but don't know why i am getting WA judgement and unfortunately the test cases on which my code is failing are big, therefore i am not able to find the logical error in my solution.

In my approach I firstly find the maximum element(it's first occurence) in the array. Let's call the maximum element mx and it's index in the array as ind. Now as per me this should be the maximum number of marks on the wall during the entire process. Now firstly I move towards the left of ind. If I find any number(m[i]) which is smaller than the current max(mx) then I update mx by mx-1 and add (mx-m[i]) to the answer. After this I start moving towards right of ind. Here we will simply add mx-m[i] to the final answer(here mx is the original maximum element in the array). Below i am pasting my code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()                                         //i guess the code is clean enough for anyone to understand.
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	vector<int>m(n);
	for(int i=0; i<n; i++){
		cin>>m[i];
	}
	int mx=*max_element(m.begin(),m.end());
	int ind=max_element(m.begin(),m.end())-m.begin();
	ll sum=0;
	int curr=mx;
	for(int i=ind-1; i>=0; i--){
		if(m[i]<curr){
			curr--;
			sum+=(curr-m[i]);
		}
	}
	for(int i=ind+1; i<n; i++){
		sum+=(mx-m[i]);
	}
	cout<<sum;
	return 0;
}

Kindly help me to find out the error in my solution.

  • Vote: I like it
  • +12
  • Vote: I do not like it

| Write comment?