[Solution] Maximum Subarray CodeChef Solution
Problem
Given two arrays and of sizes and respectively. You can apply the following operation until the array is non-empty:
- Choose either the first or the last element of array .
- Insert the chosen element to either the front or the back of array .
- Delete the chosen element from array .
For example, let and . In one operation, we can choose either or (first or last element of array ). We can insert in array and make it either or . The chosen is deleted from array . Thus, it will become either (when chosen is ) or (when chosen is ).
Find the maximum sum of any subarray of the array that you can achieve after performing exactly operations.
Note: A subarray of an array is formed by deleting some (possibly zero) elements from the beginning of the array and some (possible zero) elements from the end of the array. A subarray can be empty as well.
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of lines of input.
- The first line of each test contains a single integer , the size of array .
- The next line contains space-separated integers, denoting elements of array .
- The third line of each test contains a single integer , the size of array .
- The next line contains space-separated integers, denoting elements of array .
Output Format
For each test case, output on a new line the maximum sum of any subarray of the array that you can achieve after performing exactly operations.
Explanation:
Test case :
- Operation : Add the first element of array to the back of array . Thus, and .
- Operation : Add the first element of array to the back of array . Thus, and .
The, maximum sum subarray of array is having sum .
Test case :
- Operation : Add the first element of array to the front of array . Thus, and .
The, maximum sum subarray of array is having sum .
Test case :
- Operation : Add the last element of array to the back of array . Thus, and .
- Operation : Add the last element of array to the front of array . Thus, and .
- Operation : Add the first element of array to the front of array . Thus, and .
The, maximum sum subarray of array is having sum .
No comments:
Post a Comment