304 North Cardinal St.
Dorchester Center, MA 02124

# First and Last CodeChef Solution

## Problem – First and Last CodeChef Solution

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].

### Input Format

• The first line of input will contain a single integer T, denoting the number of test cases. The description of the test cases follows.
• The first line of each test case contains a single integer N, denoting the length of array A.
• The second line of each test case contains N space-separated integers A1​,A2​,…,AN​ — denoting the array A.

### Output Format

For each test case, output on a new line the maximum value of A1​+AN​ you can get after several right rotations.

### Constraints

• 1≤T≤1000
• 2≤N≤10^5
• 1≤Ai​≤10^9
• The sum of N across all test cases does not exceed 10^5

### Sample 1:

``````Input:
3
2
5 8
3
5 10 15
4
4 4 4 4
Output:
13
25
8
``````

### Explanation:

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.

## First and Last CodeChef Solution in C++17

``````#include <bits/stdc++.h>
using namespace std;

int main() {
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+a[N-1])<<endl;

}
return 0;
}
``````
##### First and Last CodeChef Solution Review:

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

Find on CodeChef

##### Conclusion:

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 >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions