Open In App

Maximum sum of a path in a Right Number Triangle

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a right triangle of numbers, find the largest of the sum of numbers that appear on the paths starting from the top towards the base, so that on each path the next number is located directly below or below and one place to the right.

Examples : 

Input : 1
        1 2
        4 1 2
        2 3 1 1        
Output : 9
Explanation : 1 + 1 + 4 + 3

Input : 2
        4 1
        1 2 7
Output : 10
Explanation : 2 + 1 + 7

Method 1: Recursion

The idea is to find the largest sum ending at every cell of the last row and return a maximum of these sums. We can recursively compute these sums by recursively considering the above two cells. 

C++




// C++ program for
// Recursion implementation of
// Min Sum Path in a Triangle
#include <bits/stdc++.h>
using namespace std;
 
// Util function to find minimum sum for a path
int helper(vector<vector<int>>& tri, int i, int j){
    // Base Case 
    if(i == tri.size() ){
      return 0 ;
    }
   
    int mx ;
    // Add current to the maximum  of the next paths
    mx = tri[i][j] + max(helper(tri, i+1,j), helper(tri,i+1, j+1)) ;
    //return maximum
    return mx ;
}
 
 
int maxSumPath(vector<vector<int>>& tri) {
    return helper(tri, 0, 0) ;
}
 
/* Driver program to test above functions */
int main()
{
    vector<vector<int> > tri{ { 1 },
                            { 2, 1 },
                            { 3, 3, 2 } };
    cout << maxSumPath(tri);
    return 0;
}


Java




// Java program for
// Recursion implementation of
// Min Sum Path in a Triangle
import java.io.*;
 
class GFG
{
 
  // Util function to find minimum sum for a path
  public static int helper(int tri[][], int i, int j)
  {
 
    // Base Case
    if (i == tri.length) {
      return 0;
    }
 
    int mx;
     
    // Add current to the maximum  of the next paths
    mx = tri[i][j]
      + Math.max(helper(tri, i + 1, j),
                 helper(tri, i + 1, j + 1));
     
    // return maximum
    return mx;
  }
 
  public static int maxSumPath(int tri[][])
  {
    return helper(tri, 0, 0);
  }
 
  /* Driver program to test above functions */
  public static void main(String[] args)
  {
    int tri[][] = { { 1 }, { 2, 1 }, { 3, 3, 2 } };
    System.out.print(maxSumPath(tri));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python program for Recursion implementation of Min Sum Path in a Triangle
 
# Util function to find minimum sum for a path
def helper(tri, i, j):
      # Base Case
    if (i == len(tri)):
        return 0
    mx = 0
     
    # Add current to the maximum  of the next paths
    mx = tri[i][j] + max(helper(tri, i + 1, j), helper(tri, i + 1, j + 1))
     
    # return maximum
    return mx
 
def maxSumPath(tri):
    return helper(tri, 0, 0)
 
# Driver program to test above functions
if __name__ == "__main__":
    tri = [[1], [2, 1], [3, 3, 2]]
    print(maxSumPath(tri))
     
    # This code is contributed by ajaymakavana.


C#




// C# program for
// Recursion implementation of
// Min Sum Path in a Triangle
using System;
 
public class GFG {
 
  // Util function to find minimum sum for a path
  static int helper(int[][] tri, int i, int j)
  {
 
    // Base Case
    if (i == tri.GetLength(0)) {
      return 0;
    }
 
    int mx;
 
    // Add current to the maximum  of the next paths
    mx = tri[i][j]
      + Math.Max(helper(tri, i + 1, j),
                 helper(tri, i + 1, j + 1));
 
    // return maximum
    return mx;
  }
 
  static int maxSumPath(int[][] tri)
  {
    return helper(tri, 0, 0);
  }
 
  static public void Main()
  {
 
    // Code
    int[][] tri = { new int[] { 1 }, new int[] { 2, 1 },
                   new int[] { 3, 3, 2 } };
    Console.Write(maxSumPath(tri));
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




// javascript program for
// Recursion implementation of
// Min Sum Path in a Triangle
 
// Util function to find minimum sum for a path
function helper(tri, i, j)
{
 
    // Base Case 
    if(i == tri.length ){
      return 0 ;
    }
   
    let mx ;
     
    // Add current to the maximum  of the next paths
    mx = tri[i][j] + Math.max(helper(tri, i+1,j), helper(tri,i+1, j+1)) ;
     
    //return maximum
    return mx ;
}
 
function maxSumPath( tri) {
    return helper(tri, 0, 0) ;
}
 
/* Driver program to test above functions */
    let tri = [[ 1 ], [ 2, 1 ],[ 3, 3, 2 ]];
    console.log(maxSumPath(tri));
  
// This code is contributed by garg28harsh.


Output

6

Complexity Analysis:

  • Time Complexity: O(2N*N) where N = number of rows and M = number of columns
  • Auxiliary Space: O(N)

Method 2: Dynamic Programming – Top-Down Approach

Since there are overlapping subproblems, we use dynamic programming to find the maximum sum ending at a particular cell of the last row.
Below is the implementation of the above idea.

C++




// C++ program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
#include <bits/stdc++.h>
using namespace std;
 
// Util function to find minimum sum for a path
int helper(vector<vector<int>>& tri, int i, int j, vector<vector<int>>& dp){
    // Base Case 
    if(i == tri.size() ){
      return 0 ;
    }
   
   // To avoid solving overlapping subproblem
   if(dp[i][j] != -1){
     return dp[i][j] ;
   }
   
 
    // Add current to the minimum  of the next paths
    // and store it in dp matrix
    return dp[i][j] = tri[i][j] + max(helper(tri, i+1,j, dp), helper(tri,i+1, j+1, dp)) ;
     
     
}
 
 
int maxSumPath(vector<vector<int>>& tri) {
    int n = tri.size() ;
    // Initializating of dp matrix
    vector<vector<int>> dp(n, vector<int>(n, -1) ) ;
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
int main()
{
    vector<vector<int> > tri{ { 1 },
                            { 2, 1 },
                            { 3, 3, 2 } };
    cout << maxSumPath(tri);
    return 0;
}


Java




// Java program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
import java.io.*;
 
class GFG {
 
    // Util function to find minimum sum for a path
    static int helper(int[][] tri, int i, int j, int[][] dp)
    {
        // Base Case
        if (i == tri.length) {
            return 0;
        }
 
        // To avoid solving overlapping subproblem
        if (dp[i][j] != -1) {
            return dp[i][j];
        }
 
        // Add current to the minimum  of the next paths
        // and store it in dp matrix
        return dp[i][j]
            = tri[i][j]
              + Math.max(helper(tri, i + 1, j, dp),
                         helper(tri, i + 1, j + 1, dp));
    }
 
    static int maxSumPath(int[][] tri)
    {
        int n = tri.length;
 
        // Initializating of dp matrix
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = -1;
            }
        }
 
        // calling helper function
        return helper(tri, 0, 0, dp);
    }
 
    public static void main(String[] args)
    {
        int[][] tri = { { 1 }, { 2, 1 }, { 3, 3, 2 } };
 
        System.out.print(maxSumPath(tri));
    }
}
 
// This code is contributed by lokesh


Python3




# Python program for Dynamic
# Programming implementation of
# Min Sum Path in a Triangle
 
# Util function to find minimum sum for a path
def helper(tri, i, j, dp):
    # Base Case 
    if(i == len(tri)):
        return 0
   
   # To avoid solving overlapping subproblem
    if(dp[i][j] != -1):
        return dp[i][j]
    
   
    # Add current to the minimum  of the next paths
    # and store it in dp matrix
    dp[i][j]=tri[i][j]+max(helper(tri, i+1,j, dp), helper(tri, i+1, j+1, dp))
    return dp[i][j];
 
 
def maxSumPath(tri):
    n = len(tri) ;
     
    # Initializating of dp matrix
    #vector<vector<int>> dp(n, vector<int>(n, -1) ) ;4
    dp= [[-1]*n for _ in range(n)]
     
    # calling helper function
    return helper(tri, 0, 0, dp)
 
 
# Driver program to test above functions
if __name__ == "__main__":
    tri= [[ 1 ],[2, 1 ],[3, 3, 2 ] ];
    print(maxSumPath(tri));
    
# This code is contributed by lokeshpotta20.


C#




// C# program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
using System;
class GFG
{
   
    // Util function to find minimum sum for a path
    static int helper(int[][] tri, int i, int j, int[, ] dp)
    {
        // Base Case
 
        if (i == tri.GetLength(0))
            return 0;
 
        // To avoid solving overlapping subproblem
        if (dp[i, j] != -1) {
            return dp[i, j];
        }
 
        // Add current to the minimum  of the next paths
        // and store it in dp matrix
        return dp[i, j]
            = tri[i][j]
              + Math.Max(helper(tri, i + 1, j, dp),
                         helper(tri, i + 1, j + 1, dp));
    }
    static int maxSumPath(int[][] tri)
    {
 
        // Initializating of dp matrix
        int n = tri.Length;
 
        int[, ] dp = new int[n, n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                dp[i, j] = -1;
 
        // calling helper function
        return helper(tri, 0, 0, dp);
    }
    static void Main()
    {
        int[][] tri = new int[3][];
        tri[0] = new int[] { 1 };
        tri[1] = new int[] { 2, 1 };
        tri[2] = new int[] { 3, 3, 2 };
        Console.Write(maxSumPath(tri));
    }
}
 
// This code is contributed by garg28harsh.


Javascript




// javascript program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
// Util function to find minimum sum for a path
function helper( tri, i,  j,  dp)
{
 
    // Base Case 
    if(i == tri.length ){
      return 0 ;
    }
   
   // To avoid solving overlapping subproblem
   if(dp[i][j] != -1){
     return dp[i][j] ;
   }
   
    // Add current to the minimum  of the next paths
    // and store it in dp matrix
    return dp[i][j] = tri[i][j] + Math.max(helper(tri, i+1,j, dp), helper(tri,i+1, j+1, dp)) ;
}
 
function maxSumPath( tri) {
    let n = tri.length ;
    // Initializating of dp matrix
    let dp= new Array(n);
    for (let i = 0; i < n; i++)
        dp[i] = new Array(n);
    for(let i = 0; i < n; i++)
        for(let j = 0; j < n; j++)
            dp[i][j] = -1;
 
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
 
    let tri = [[ 1 ], [ 2, 1 ], [ 3, 3, 2 ]];
    console.log(maxSumPath(tri));
    
// This code is contributed by garg28hrsh.


Output

6

Complexity Analysis:

  • Time Complexity: O(N*M) where N = number of rows and M = number of columns
  • Auxiliary Space: O(N2)

Method 3: Dynamic Programming: Bottom-Up Approach

Since there are overlapping subproblems, we use dynamic programming to find the maximum sum ending at a particular cell of the last row. Below is the implementation of the above idea.

C++




// C++ program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
#include <bits/stdc++.h>
using namespace std;
 
// Util function to find minimum sum for a path
int helper(vector<vector<int>>& tri, int i, int j, vector<vector<int>>& dp){
    int n = tri.size() ;
      // loop for bottom-up calculation
     for(int j = 0; j < n; j++ ){
            dp[n-1][j] = tri[n-1][j] ;
        }
         
        for(int i = n-2; i >= 0; i--){
            for(int j = i; j >= 0; j-- ){
                dp[i][j] = tri[i][j] + max(dp[i+1][j] , dp[i+1][j+1]) ;
            }
        }
         
        return dp[0][0] ;
     
     
}
 
 
int maxSumPath(vector<vector<int>>& tri) {
    int n = tri.size() ;
    // Initializating of dp matrix
    vector<vector<int>> dp(n, vector<int>(n, -1) ) ;
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
int main()
{
    vector<vector<int> > tri{ { 1 },
                            { 2, 1 },
                            { 3, 3, 2 } };
    cout << maxSumPath(tri);
    return 0;
}


Java




// Java program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
import java.util.*;
 
class GFG {
    // Util function to find minimum sum for a path
    static int helper(int[][] tri, int i, int j, int[][] dp)
    {
        int n = tri.length;
        // loop for bottom-up calculation
        for (j = 0; j < n; j++) {
            dp[n - 1][j] = tri[n - 1][j];
        }
 
        for (i = n - 2; i >= 0; i--) {
            for (j = i; j >= 0; j--) {
                dp[i][j] = tri[i][j]
                           + Math.max(dp[i + 1][j],
                                      dp[i + 1][j + 1]);
            }
        }
 
        return dp[0][0];
    }
 
    static int maxSumPath(int[][] tri)
    {
        int n = tri.length;
        // Initializating of dp matrix
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = -1;
            }
        }
        // calling helper function
        return helper(tri, 0, 0, dp);
    }
    public static void main(String[] args)
    {
        int[][] tri = { { 1 }, { 2, 1 }, { 3, 3, 2 } };
        System.out.println(maxSumPath(tri));
    }
}
 
// This code is contributed by karandeep1234


Python3




# Python program for Dynamic
# Programming implementation of
# Min Sum Path in a Triangle
import math
# Util function to find minimum sum for a path
def helper(tri, i, j, dp):
    n = len(tri) ;
      # loop for bottom-up calculation
    for j in range(0,n):
            dp[n-1][j] = tri[n-1][j] ;
         
         
    for i in range(n-2, -1, -1):
        for j in range(i,-1,-1):
            dp[i][j] = tri[i][j] + max(dp[i+1][j] , dp[i+1][j+1]) ;
         
    return dp[0][0] ;
 
 
 
def maxSumPath(tri):
    n = len(tri) ;
    # Initializating of dp matrix
    #vector<vector<int>> dp(n, vector<int>(n, -1) ) ;
    dp=[[-1]*n for _ in range(n)];
    # calling helper function
    return helper(tri, 0, 0, dp) ;
 
# Driver program to test above functions */
tri=[[ 1 ],[2, 1],[3, 3, 2 ]];
print(maxSumPath(tri));


C#




// C# program for Dynamic Programming implementation of Min
// Sum Path in a Triangle
 
using System;
 
public class GFG {
 
    // Util function to find minimum sum for a path
    static int helper(int[][] tri, int i, int j, int[, ] dp)
    {
        int n = tri.GetLength(0);
        // loop for bottom-up calculation
        for (j = 0; j < n; j++) {
            dp[n - 1, j] = tri[n - 1][j];
        }
 
        for (i = n - 2; i >= 0; i--) {
            for (j = i; j >= 0; j--) {
                dp[i, j] = tri[i][j]
                           + Math.Max(dp[i + 1, j],
                                      dp[i + 1, j + 1]);
            }
        }
 
        return dp[0, 0];
    }
 
    static int maxSumPath(int[][] tri)
    {
        int n = tri.GetLength(0);
        // Initializating of dp matrix
        int[, ] dp = new int[n, n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i, j] = -1;
            }
        }
        // calling helper function
        return helper(tri, 0, 0, dp);
    }
 
    static public void Main()
    {
 
        int[][] tri = new int[3][];
 
        // Initialize the elements
        tri[0] = new int[] { 1 };
        tri[1] = new int[] { 2, 1 };
        tri[2] = new int[] { 3, 3, 2 };
        Console.WriteLine(maxSumPath(tri));
    }
}
 
// This code is contributed by lokeshmvs21.


Javascript




// JavaScript program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
// Util function to find minimum sum for a path
function helper(tri, i, j, dp){
    let n = tri.length;
    // loop for bottom-up calculation
    for (j = 0; j < n; j++) {
        dp[n - 1][j] = tri[n - 1][j];
    }
 
    for (i = n - 2; i >= 0; i--) {
        for (j = i; j >= 0; j--) {
            dp[i][j] = tri[i][j]
                       + Math.max(dp[i + 1][j],
                                  dp[i + 1][j + 1]);
        }
    }
 
    return dp[0][0];
}
 
function maxSumPath(tri){
    let n = tri.length;
    // Initializating of dp matrix
    let dp = new Array();
    for(let i=0;i<n;i++){
        dp[i] = new Array();
        for(let j=0;j<n;j++){
            dp[i][j] = -1;
        }
    }
    // calling helper function
    return helper(tri, 0, 0, dp);
}
 
let tri = [ [1], [2, 1], [3, 3, 2] ];
console.log(maxSumPath(tri));
 
// This code is contributed by lokesh


Output

6

Complexity Analysis:

  • Time Complexity: O(m*n) where m = no of rows and n = no of columns
  • Auxiliary Space: O(n2)

Method 4: Space Optimization(Without changing the input matrix)

We don’t need a matrix of m*n size. It will store all the results.
We just need the triangle row minimum of the immediate bottom row
So using this approach we can optimize the space from O(m*n) to O(n).
The approach remains the same as that of Method 3.

C++




// C++ program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
#include <bits/stdc++.h>
using namespace std;
 
// Util function to find minimum sum for a path
int helper(vector<vector<int>>& tri, int i, int j, vector<vector<int>>& dp){
    int n = tri.size() ;
    vector<int> front(n, -1) , curr(n, -1) ;
 
    for(int j = 0; j < n; j++ ){
      front[j] = tri[n-1][j] ;
    }
 
    for(int i = n-2; i >= 0; i--){
      for(int j = i; j >= 0; j-- ){
        curr[j] = tri[i][j] + max(front[j] , front[j+1]) ;
      }
      front = curr ;
    }
 
    return front[0] ;
     
}
 
 
int maxSumPath(vector<vector<int>>& tri) {
    int n = tri.size() ;
    // Initializating of dp matrix
    vector<vector<int>> dp(n, vector<int>(n, -1) ) ;
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
int main()
{
    vector<vector<int> > tri{ { 1 },
                            { 2, 1 },
                            { 3, 3, 2 } };
    cout << maxSumPath(tri);
    return 0;
}


Java




// Java program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
import java.util.*;
  
class GFG {
     
// Util function to find minimum sum for a path
static int helper(int[][] tri, int i, int j, int[][] dp){
    int n = tri.length;
     
    int[] front=new int[n];
    int[] curr=new int[n];
    for(int t=0;t<n;t++)
    {
        front[t]=-1;
        curr[t]=-1;
    }
     
    for(j = 0; j < n; j++ ){
    front[j] = tri[n-1][j] ;
    }
 
    for(i = n-2; i >= 0; i--){
    for(j = i; j >= 0; j-- ){
        curr[j] = tri[i][j] + Math.max(front[j] , front[j+1]) ;
    }
      front = curr ;
    }
 
    return front[0] ;
     
}
 
 
static int maxSumPath(int[][] tri) {
    int n = tri.length;
    // Initializating of dp matrix
    int[][] dp = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
                dp[i][j] = -1;
            }
        }
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
public static void main(String[] args)
{
    int[][] tri = { { 1 }, { 2, 1 }, { 3, 3, 2 } };
    System.out.println(maxSumPath(tri));
}
 
}
// This code is contributed by Pushpesh Raj.


Python3




#Python code for the above approach
# Util function to find minimum sum for a path
def helper(tri, i, j, dp):
    n = len(tri)
    front = [-1] * n
    curr = [-1] * n
 
    for j in range(n):
        front[j] = tri[n-1][j]
 
    for i in range(n-2, -1, -1):
        for j in range(i, -1, -1):
            curr[j] = tri[i][j] + max(front[j], front[j+1])
        front = curr
 
    return front[0]
 
def maxSumPath(tri):
    n = len(tri)
    # Initializating of dp matrix
    dp = [[-1] * n for _ in range(n)]
    # calling helper function
    return helper(tri, 0, 0, dp)
 
# test the code with a triangle
tri = [ [1],
        [2, 1],
        [3, 3, 2] ]
print(maxSumPath(tri))


C#




// C# program for Dynamic Programming implementation of Min
// Sum Path in a Triangle
using System;
 
class GFG
{
   
  // Util function to find minimum sum for a path
  public static int helper(int[][] tri, int i, int j,
                           int[][] dp)
  {
    int n = tri.Length;
 
    int[] front = new int[n];
    int[] curr = new int[n];
    for (int t = 0; t < n; t++) {
      front[t] = -1;
      curr[t] = -1;
    }
 
    for (j = 0; j < n; j++) {
      front[j] = tri[n - 1][j];
    }
 
    for (i = n - 2; i >= 0; i--) {
      for (j = i; j >= 0; j--) {
        curr[j]
          = tri[i][j]
          + Math.Max(front[j], front[j + 1]);
      }
      front = curr;
    }
 
    return front[0];
  }
 
  public static int maxSumPath(int[][] tri)
  {
    int n = tri.Length;
     
    // Initializating of dp matrix
    int[][] dp = new int[n][];
    for (int i = 0; i < n; i++) {
      dp[i] = new int[n];
      for (int j = 0; j < n; j++) {
        dp[i][j] = -1;
      }
    }
     
    // calling helper function
    return helper(tri, 0, 0, dp);
  }
 
  /* Driver program to test above functions */
  public static void Main(string[] args)
  {
    int[][] tri = { new[] { 1 }, new[] { 2, 1 },
                   new[] { 3, 3, 2 } };
    Console.WriteLine(maxSumPath(tri));
  }
}
 
// This code is contributed by ik_9


Javascript




// Javascript program for Dynamic
// Programming implementation of
// Min Sum Path in a Triangle
 
// Util function to find minimum sum for a path
function helper(tri, i,  j,  dp){
    let n = tri.length ;
    let front=new Array(n, -1) , curr=new Array(n, -1) ;
 
    for(let j = 0; j < n; j++ ){
      front[j] = tri[n-1][j] ;
    }
 
    for(let i = n-2; i >= 0; i--){
      for(let j = i; j >= 0; j-- ){
        curr[j] = tri[i][j] + Math.max(front[j] , front[j+1]) ;
      }
      front = curr ;
    }
 
    return front[0] ;
     
}
 
 
function maxSumPath(tri) {
    let n = tri.length ;
    // Initializating of dp matrix
    let dp=new Array(n);
    for(let i=0; i<n; i++)
        dp[i]=new Array(n).fill(-1);
    // calling helper function
    return helper(tri, 0, 0, dp) ;
}
 
/* Driver program to test above functions */
let tri=[[ 1 ],[ 2, 1 ],[ 3, 3, 2 ]];
console.log(maxSumPath(tri));


Output

6

Complexity Analysis:

  • Time Complexity: O(m*n) where m = no of rows and n = no of columns
  • Space Complexity: O(n)

Method 5: Space Optimization (Changing the input matrix)

C++




// C++ program to print maximum sum
// in a right triangle of numbers
#include<bits/stdc++.h>
using namespace std;
 
// function to find maximum sum path
int maxSum(int tri[][3], int n)
{
    // Adding the element of row 1 to both the
    // elements of row 2 to reduce a step from
    // the loop
    if (n > 1)
        tri[1][1] = tri[1][1] + tri[0][0];
        tri[1][0] = tri[1][0] + tri[0][0];
 
    // Traverse remaining rows
    for(int i = 2; i < n; i++) {
        tri[i][0] = tri[i][0] + tri[i-1][0];
        tri[i][i] = tri[i][i] + tri[i-1][i-1];
 
        //Loop to traverse columns
        for (int j = 1; j < i; j++){
 
            // Checking the two conditions,
            // directly below and below right.
            // Considering the greater one
             
            // tri[i] would store the possible
            // combinations of sum of the paths
            if (tri[i][j] + tri[i-1][j-1] >=
                            tri[i][j] + tri[i-1][j])
                 
                tri[i][j] = tri[i][j] + tri[i-1][j-1];
            else
                tri[i][j] = tri[i][j]+tri[i-1][j];
        }
    }
     
    // array at n-1 index (tri[i]) stores
    // all possible adding combination, finding
    // the maximum one out of them
    int max=tri[n-1][0];
     
    for(int i=1;i<n;i++)
    {
        if(max<tri[n-1][i])
            max=tri[n-1][i];
    }
     
    return max;
}
 
// driver program
int main(){
     
    int tri[3][3] = {{1}, {2,1}, {3,3,2}};
 
    cout<<maxSum(tri, 3);
     
    return 0;
}


Java




// Java program to print maximum sum
// in a right triangle of numbers
import java.io.*;
class GFG
{
     
    // function to find maximum sum path
    static int maxSum(int tri[][], int n)
    {
         
        // Adding the element of row 1 to both the
        // elements of row 2 to reduce a step from
        // the loop
        if (n > 1)
            tri[1][1] = tri[1][1] + tri[0][0];
            tri[1][0] = tri[1][0] + tri[0][0];
     
        // Traverse remaining rows
        for(int i = 2; i < n; i++) {
            tri[i][0] = tri[i][0] + tri[i-1][0];
            tri[i][i] = tri[i][i] + tri[i-1][i-1];
     
            //Loop to traverse columns
            for (int j = 1; j < i; j++){
     
                // Checking the two conditions,
                // directly below and below right.
                // Considering the greater one
                 
                // tri[i] would store the possible
                // combinations of sum of the paths
                if (tri[i][j] + tri[i-1][j-1] >=
                           tri[i][j] + tri[i-1][j])
                     
                    tri[i][j] = tri[i][j]
                                  + tri[i-1][j-1];
                     
                else
                    tri[i][j] = tri[i][j]
                                    + tri[i-1][j];
            }
        }
         
        // array at n-1 index (tri[i]) stores
        // all possible adding combination,
        // finding the maximum one out of them
        int max = tri[n-1][0];
         
        for(int i = 1; i < n; i++)
        {
            if(max < tri[n-1][i])
                max = tri[n-1][i];
        }
         
        return max;
    }
         
    // Driver code
    public static void main (String[] args)
    {
        int tri[][] = {{1}, {2,1}, {3,3,2}};
         
        System.out.println(maxSum(tri, 3));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to print maximum sum
# in a right triangle of numbers.
 
# tri[][] is a 2D array that stores the
# triangle, n is number of lines or rows.
def maxSum(tri, n):
 
    # Adding the element of row 1 to both the
    # elements of row 2 to reduce a step from
    # the loop
    if n > 1:
        tri[1][1] = tri[1][1]+tri[0][0]
        tri[1][0] = tri[1][0]+tri[0][0]
 
    # Traverse remaining rows
    for i in range(2, n):
        tri[i][0] = tri[i][0] + tri[i-1][0]
        tri[i][i] = tri[i][i] + tri[i-1][i-1]
 
        # Loop to traverse columns
        for j in range(1, i):
 
            # Checking the two conditions, directly below
            # and below right. Considering the greater one
 
            # tri[i] would store the possible combinations
            # of sum of the paths
            if tri[i][j]+tri[i-1][j-1] >= tri[i][j]+tri[i-1][j]:
                tri[i][j] = tri[i][j] + tri[i-1][j-1]
            else:
                tri[i][j] = tri[i][j]+tri[i-1][j]
 
    # array at n-1 index (tri[i]) stores all possible
    # adding combination, finding the maximum one
    # out of them
    print (max(tri[n-1]))
 
# driver program
tri = [[1], [2,1], [3,3,2]]
maxSum(tri, 3)


C#




// C# program to print
// maximum sum in a right
// triangle of numbers
using System;
 
class GFG
{
     
    // function to find
    // maximum sum path
    static int maxSum(int [,]tri,
                      int n)
    {
         
        // Adding the element of row 1
        // to both the elements of row 2
        // to reduce a step from the loop
        if (n > 1)
            tri[1, 1] = tri[1, 1] +
                        tri[0, 0];
            tri[1, 0] = tri[1, 0] +
                        tri[0, 0];
     
        // Traverse remaining rows
        for(int i = 2; i < n; i++)
        {
            tri[i, 0] = tri[i, 0] +
                        tri[i - 1, 0];
            tri[i, i] = tri[i, i] +
                        tri[i - 1, i - 1];
      
            //Loop to traverse columns
            for (int j = 1; j < i; j++)
            {
     
                // Checking the two conditions,
                // directly below and below right.
                // Considering the greater one
                 
                // tri[i] would store the possible
                // combinations of sum of the paths
                if (tri[i, j] + tri[i - 1, j - 1] >=
                    tri[i, j] + tri[i - 1, j])
                 
                    tri[i, j] = tri[i, j] +
                                tri[i - 1, j - 1];
                     
                else
                    tri[i, j] = tri[i, j] +
                                tri[i - 1, j];
            }
        }
         
        // array at n-1 index (tri[i])
        // stores all possible adding
        // combination, finding the
        // maximum one out of them
        int max = tri[n - 1, 0];
         
        for(int i = 1; i < n; i++)
        {
            if(max < tri[n - 1, i])
                max = tri[n - 1, i];
        }
         
        return max;
    }
         
// Driver Code
public static void Main ()
{
     
        int [,]tri = {{1,0,0},
                      {2,1,0},
                      {3,3,2}};
         
        Console.Write(maxSum(tri, 3));
}
}
 
// This code is contributed by ajit.


PHP




<?php
// PHP program to print maximum sum
// in a right triangle of numbers
 
// function to find maximum sum path
function maxSum($tri, $n)
{
    // Adding the element of row 1
    // to both the elements of row
    // 2 to reduce a step from the loop
    if ($n > 1)
        $tri[1][1] = $tri[1][1] + $tri[0][0];
        $tri[1][0] = $tri[1][0] + $tri[0][0];
 
    // Traverse remaining rows
    for($i = 2; $i < $n; $i++)
    {
        $tri[$i][0] = $tri[$i][0] +
                      $tri[$i - 1][0];
        $tri[$i][$i] = $tri[$i][$i] +
                       $tri[$i - 1][$i - 1];
 
        //Loop to traverse columns
        for ($j = 1; $j < $i; $j++)
        {
 
            // Checking the two conditions,
            // directly below and below right.
            // Considering the greater one
             
            // tri[i] would store the possible
            // combinations of sum of the paths
            if ($tri[$i][$j] + $tri[$i - 1][$j - 1] >=
                 $tri[$i][$j] + $tri[$i - 1][$j])
                 
                $tri[$i][$j] = $tri[$i][$j] +
                               $tri[$i - 1][$j - 1];
            else
                $tri[$i][$j] = $tri[$i][$j] +
                               $tri[$i - 1][$j];
        }
    }
     
    // array at n-1 index (tri[i])
    // stores all possible adding
    // combination, finding the
    // maximum one out of them
     
    $max = $tri[$n - 1][0];
     
    for($i = 1; $i < $n; $i++)
    {
        if($max < $tri[$n - 1][$i])
            $max = $tri[$n - 1][$i];
    }
     
    return $max;
}
 
// Driver Code
$tri = array(array(1),
             array(2,1),
             array(3,3,2));
 
echo maxSum($tri, 3);
 
// This code is contributed by ajit
?>


Javascript




<script>
 
    // Javascript program to print maximum sum
    // in a right triangle of numbers
     
    // function to find maximum sum path
    function maxSum(tri, n)
    {
           
        // Adding the element of row 1 to both the
        // elements of row 2 to reduce a step from
        // the loop
        if (n > 1)
            tri[1][1] = tri[1][1] + tri[0][0];
            tri[1][0] = tri[1][0] + tri[0][0];
       
        // Traverse remaining rows
        for(let i = 2; i < n; i++) {
            tri[i][0] = tri[i][0] + tri[i-1][0];
            tri[i][i] = tri[i][i] + tri[i-1][i-1];
       
            // Loop to traverse columns
            for (let j = 1; j < i; j++){
       
                // Checking the two conditions,
                // directly below and below right.
                // Considering the greater one
                   
                // tri[i] would store the possible
                // combinations of sum of the paths
                if (tri[i][j] + tri[i-1][j-1] >=
                           tri[i][j] + tri[i-1][j])
                       
                    tri[i][j] = tri[i][j]
                                  + tri[i-1][j-1];
                       
                else
                    tri[i][j] = tri[i][j]
                                    + tri[i-1][j];
            }
        }
           
        // array at n-1 index (tri[i]) stores
        // all possible adding combination,
        // finding the maximum one out of them
        let max = tri[n-1][0];
           
        for(let i = 1; i < n; i++)
        {
            if(max < tri[n-1][i])
                max = tri[n-1][i];
        }
           
        return max;
    }
     
    let tri = [[1], [2,1], [3,3,2]];
           
      document.write(maxSum(tri, 3));
         
</script>


Output

6

Complexity Analysis:

  • Time Complexity: O(m*n) where m = no of rows and n = no of columns
  • Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads