Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given an array A=[A1,A2,…,AN] of length N.
You can right rotate it any number of times (possibly, zero). What is the maximum value of A1+AN you can get?
Note: Right rotating the array [A1,A2,…,AN] once gives the array [AN,A1,A2,…,AN−1]. For example, right rotating [1,2,3] once gives [3,1,2], and right rotating it again gives [2,3,1].
For each test case, output on a new line the maximum value of A1+AN you can get after several right rotations.
Input:
3
2
5 8
3
5 10 15
4
4 4 4 4
Output:
13
25
8
Test case 1: Whether you right rotate the array or not, you will always end up with A1+AN=13.
Test case 2: It is optimal to right rotate the array once after which the array becomes [15,5,10] with A1+AN=25.
Test case 3: No matter how much you right rotate the array, you will always obtain A1+AN=8.
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin >> t;
while(t--)
{
int N,maxx=INT_MIN,sum;
cin >> N;
int a[N];
for(int i=0;i<N;i++)
{
cin >> a[i];
}
for(int i=0;i<N-1;i++)
{
sum=a[i] +a[i+1];
maxx=(sum>maxx? sum:maxx);
}
cout<<max(maxx,a[0]+a[N-1])<<endl;
}
return 0;
}
In our experience, we suggest you solve this First and Last CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the First and Last CodeChef Solution
I hope this First and Last CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>