I am trying to get the maximum value out of this array.
There is a long rope of length totalLength
at its each unit length some ropes are tied pointing in upper and lower directions with their length given. I am saving them in upper and lower arrays. Question is if we light up the front end how much time it will take to burn the whole rope. The fire spreads at 1m/s.
These two arrays upper and lower they have some value and the I am adding the max of them in each iteration and at the end I am finding the max value.
My idea is the total time taken will be equal to burning the longest part of the rope. I am finding the longest part and displaying it. It seems like this code is not good enough and is not clearing the time limits.
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();//total test cases;
int totalLength = 0;
int maxSum = 0, maxUL = 0;
int[] upper;
int[] lower;
int[] result;
while(N-- > 0) {
maxSum = 0;
totalLength = scan.nextInt();// total length of a base graph
upper = new int[totalLength - 1];
lower = new int[totalLength - 1];
result = new int[totalLength - 1];
for(int i = 0; i < totalLength - 1; i++){
upper[i] = scan.nextInt();
}
for(int i = 0; i < totalLength - 1; i++){
lower[i] = scan.nextInt();
}
for(int i = 0; i <= totalLength - 2; i++){
maxUL = (upper[i] > lower[i]) ? upper[i]: lower[i];
maxSum = i+1 + maxUL;
result[i] = maxSum;
}
int maxVal = 0;
for(int i = 0; i < result.length; i++){
if(maxVal < result[i]){
maxVal = result[i];
}
}
System.out.println(maxVal);
}
}
}
Sample Input
1 // for the test cases
5 // the totallength of the main rope
1 0 2 2
1 1 1 0
Output
6