βœ…First & Last (Contest)

First & Last (Contest) easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Ram is given an array A of length N, Ram can right-rotate the array any number of times (possibly zero times also). His task is to find out the maximum value of A1 + AN. Note: Right rotation of array [A1, A2,. , AN] is [AN, A1,. , AN-1]. Input The first line of the input contains a single integer T, denoting the number of test cases. The first line of each test case contains a single integer N, denoting the size of an array A. The second line of each test case contains N space- separated integers denoting array A.

Constraints 1 ≀ T ≀ 1000 1 ≀ N ≀ 105 1 ≀ Ai ≀ 109 Output For each test case output on a new line denoting the maximum value of A1 + AN. Example Sample Input 3 2 5 8 3 5 10 15 4 4 4 4 4 Sample Output 13 25 8

link:https://my.newtonschool.co/playground/code/o7ty0qf8d3n9/

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    int tc=sc.nextInt();
    while(tc-->0){
    int n=sc.nextInt();
    int arr[]=new int[n];
    for(int i=0;i<n;i++){
        arr[i]=sc.nextInt();
    }
    long sum=0;
    for(int i=1;i<n;i++){
        sum=Math.max(sum,(arr[i-1]+arr[i]));
    }
    sum=Math.max((arr[0]+arr[n-1]),sum);
    System.out.println(sum);
    }
    }
}

Last updated