# Untitled

unknown
plain_text
6 months ago
2.0 kB
1
Indexable
Never
```class Solution {
public:
bool chk(vector<int>&arr){
bool pos = false;
for(int i=0; i<arr.size();++i){
if(arr[i]==1){
pos = true;
break;
}
}

if(pos){
for(int i=1; i<arr.size();++i){
if(abs(arr[i] - arr[i-1])>1){
return false;
}
}
return true;
}

return false;

}
int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {
if(chk(arr)){
int mx = *max_element(arr.begin(),arr.end());
return mx;
}

int n = arr.size();
int ans = -1;
if(arr[0]==1){
for(int i=1; i<n; ++i){
if(abs(arr[i] - arr[i-1])<=1){
continue;
}
else{
arr[i] = arr[i-1] +1;
}
}
}
else{
int pos = -1;
for(int i=0; i<n; ++i){
if(arr[i]==1){
pos = i;
break;
}
}
if(pos!=-1){
swap(arr[0], arr[pos]);
for(int i=1; i<n; ++i){
if(abs(arr[i] - arr[i-1])<=1){
continue;
}
else{
arr[i] = arr[i-1] +1;
}
}
}
else{
arr[0] = 1;
for(int i=1; i<n; ++i){
if(abs(arr[i] - arr[i-1])<=1){
continue;
}
else{
arr[i] = arr[i-1] +1;
}
}
}

}

int mx = *max_element(arr.begin(),arr.end());
return mx;
}
};```