Advertisement
saurav_kalsoor

Help Meera - JAVA

Aug 12th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Problem {
  7.  
  8.     public static void main(String[] args){
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         int[][] coins = new int[2][n];
  12.  
  13.         for(int i=0; i<2; i++){
  14.             for(int j=0; j < n; j++)
  15.                 coins[i][j] = sc.nextInt();
  16.         }
  17.         System.out.println(helpMeera(coins, n));
  18.  
  19.     }
  20.  
  21.     public static int helpMeera(int[][] coins, int n){
  22.  
  23.         int[] top = new int[n];
  24.         int[] bottom = new int[n];
  25.  
  26.         top[0] = coins[0][0];
  27.         bottom[n-1] = coins[1][n-1];
  28.  
  29.         for(int i=1; i < n ; i++)
  30.             top[i] = top[i-1] + coins[0][i];
  31.  
  32.         for(int i=n-2; i >= 0; i--)
  33.             bottom[i] = bottom[i+1] + coins[1][i];
  34.  
  35.         int result = 0;
  36.  
  37.         for(int i=0; i < n; i++){
  38.             result = Math.max(result, top[i] + bottom[i]);
  39.         }
  40.  
  41.         return result;
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement