PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4
Setter: Utkarsh Gupta
Tester: Harris Leung
Editorialist: Trung Dang
DIFFICULTY:
932
PREREQUISITES:
None
PROBLEM:
You are given an array A=[A_1,A_2,…,A_N] of length N.
You can right rotate it any number of times (possibly, zero). What is the maximum value of A_1+A_N you can get?
Note: Right rotating the array [A_1,A_2,…,A_N] once gives the array [A_N,A_1,A_2,…,A_{N−1}]. For example, right rotating [1,2,3] once gives [3,1,2], and right rotating it again gives [2,3,1].
EXPLANATION:
Note that any two consecutive elements in the “circular” array A can be right rotated such that one becomes A_1 and the other becomes A_N (circular here means A_N is adjacent to A_1 as well). Therefore, the solution is to simply find the max sum of two consecutive elements on the circular array A.
TIME COMPLEXITY:
Time complexity is O(N) per test case.
SOLUTION:
Setter's Solution
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);
assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}
if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}
return x;
} else {
assert(false);
}
}
}
string readString(int l,int r,char endd){
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
return readString(l,r,'\n');
}
string readStringSp(int l,int r){
return readString(l,r,' ');
}
int sumN=0;
void solve()
{
int n=readInt(2,100000,'\n');
sumN+=n;
assert(sumN<=100000);
int A[n+1]={0};
for(int i=1;i<=n;i++)
{
if(i==n)
A[i]=readInt(1,1000000000,'\n');
else
A[i]=readInt(1,1000000000,' ');
}
ll ans=0;
for(int i=1;i<n;i++)
ans=max(ans,(ll)A[i]+A[i+1]);
ans=max(ans,(ll)A[1]+A[n]);
cout<<ans<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
int T=readInt(1,1000,'\n');
while(T--)
solve();
assert(getchar()==-1);
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
Tester's Solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const ll mod=998244353;
const int N=2e6+1;
ll a[N];
int main(){
ios::sync_with_stdio(false);
int t;cin >> t;
while(t--){
int n;cin >> n;
ll ans=0;
for(int i=1; i<=n ;i++){
cin >> a[i];
if(i>=2) ans=max(ans,a[i]+a[i-1]);
}
ans=max(ans,a[1]+a[n]);
cout << ans << '\n';
}
}
Editorialist's Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, a[i] + a[(i + 1) % n]);
}
cout << ans << '\n';
}
}