My issue
in this que. why we cannot use sort(a,a+n)
My code
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for( int i=0;i<n;i++){
cin>>a[i];
}
// sort(a,a+n);
cout<<a[n-2]+a[n-1]<<endl;
}
return 0;
}
Problem Link: FIRSTANDLAST Problem - CodeChef
@deepakjdh31
Because u are rotating the whole array so sort function will disturb the elements position and thus will give to wrong output .
i think according to que. we given an array & we rotate it to find max val a[0]+a[n-1]
but my que. is why we rotate i don’t understand that part where ans also coming from shorting
@deepakjdh31
We rotate the array to place maximum two consecutive values at a[n-1] and a[0] .
like 1 2 4 5 3 when u right rotate it two time it will become 5 3 1 2 4 and thus u will get 5+4 as maximum a[n-1]+a[0] value.