Answer to Question #249663 in Java | JSP | JSF for Tarurendra Kushwah

Question #249663
You are given an array of N non-negative integers: A1, A2, ..., AN. An alternating subsequence is a subsequence in which the indices of any two consecutive elements differ by exactly two in the original array. That is, if Ai1, Ai2, ..., Aik is some subsequence, then for it to be an alternating subsequence, (i2 - i1 = 2), (i3 - i2 = 2), and so on should all hold true. Among all alternating subsequences, find the one which has maximum sum of elements, and output that sum.

Input
The first line of the input contains an integer T denoting the number of test cases.

The first line of each test case contains an integer N denoting the number of elements in the array.

The second line contains N space-separated integers A1, A2, ..., AN denoting the array A.

Output
For each test case, output a single line containing the answer.

Note
A subsequence with only a single integer is also an alternating subsequence.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ Ai ≤ 105
1
Expert's answer
2021-10-12T00:37:30-0400
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    int N;
	    int temp = 0; 
	    int[] A=new int [10];
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of test cases: ");
		int T=input.nextInt();
		for(int x=0; x<T; x++){
		System.out.print("Enter the number of elements in the array:" );
		N=input.nextInt();
		System.out.print("Enter the elements of the array: ");
		for(int i=0;i<N;i++){
		    A[i]=input.nextInt();
	
		}
			    
	int sum=A[N-1]+A[N-2];
	System.out.println("Sum is: "+sum);
	}
	 
}}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS