0

Given array A consisting of N integers, you have to find the maximum value of the following expression: |Ai−Aj|+|i-j|

Help me optimize the code int main() { //write your code here int t,n,k; cin>>t; while(t){ cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } int max=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if(a[i]>a[j]){ k=a[i]-a[j]+j-i; } else{ k=a[j]-a[i]+j-i; } if(k>max){ max=k; } } } cout<<max<<endl; t--; }

27th Dec 2020, 5:30 PM
Pulluri Sai Lalith
Pulluri Sai Lalith - avatar
1 Answer
0
Give input sample for testing please ...
2nd Jan 2021, 4:13 AM
Ipang