Max Candies(Contest)

Max Candies easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Given a 2*N matrix in which each cell contains some candies in it. You are at the top left corner of the matrix and want to reach the bottom right corner of the matrix i. e from (1, 1) to (2, N). You can only move right or down. You have to find the maximum number of candies you can collect in your journey. Input The first line of input contains a single integer N. The second line of input contains N spaces separated integers. The last line of input contains N space-separated integers.

Constraints:- 2 <= N <= 10000 1 <= Matrix[i][j] <= 100000 Output Print the maximum amount of candies you can have at the end of your journey. Example Sample Input 1:- 5 1 3 5 6 2 2 4 8 4 2

Sample Output 1:- 23

Sample Input 2:- 4 1 1 1 1 1 1 1 1

Sample Output 2:- 5

link:https://my.newtonschool.co/playground/code/6553tcubeji1/

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) {
        // Your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int [][]arr = new int[2][n];
        for(int i=0;i<2;i++){
            for(int j=0;j<n;j++){
            arr[i][j]= sc.nextInt();
            }
        }
       int dp[][] = new int[3][n+1];
       for(int i=1;i<=2;i++){
           for(int j=1;j<=n;j++){
               dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+arr[i-1][j-1];
           }
       }
       
        System.out.println(dp[2][n]);
    }
 
}

Last updated