
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum sum of a path in a Right Number Triangle in C++
Problem statement
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
Example
If given input is: 3 4 5 1 10 7 Then maximum sum is 18 as (3 + 5 + 10).
Algorithm
The idea is to find largest sum ending at every cell of last row and return maximum of these sums.
We can recursively compute these sums by recursively considering above two cells
Since there are overlapping sub-problems, we use dynamic programming to find the maximum sum ending at particular cell of last row
Example
#include<bits/stdc++.h> using namespace std; int maxSum(int tringle[][3], int n){ if (n > 1) { tringle[1][1] = tringle[1][1] + tringle[0][0]; tringle[1][0] = tringle[1][0] + tringle[0][0]; } for(int i = 2; i < n; i++) { tringle[i][0] = tringle[i][0] + tringle[i-1][0]; tringle[i][i] = tringle[i][i] + tringle[i-1][i-1]; for (int j = 1; j < i; j++){ if (tringle[i][j] + tringle[i-1][j-1] >=tringle[i][j] + tringle[i-1][j]) { tringle[i][j] = tringle[i][j] + tringle[i-1][j-1]; } else { tringle[i][j] = tringle[i][j]+tringle[i-1][j]; } } } int max = tringle[n - 1][0]; for(int i = 1;i < n; i++) { if(max < tringle[n-1][i]) { max=tringle[n-1][i]; } } return max; } int main(){ int tringle[3][3] = { {3}, {4,5}, {1,10,7} }; cout << "Maximum sum = " << maxSum(tringle, 3) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Maximum sum = 18
- Related Articles
- Maximum path sum in a triangle in C++
- Maximum path sum in an Inverted triangle in C++
- Minimum Sum Path in a Triangle in C++
- Maximum Path Sum in a Binary Tree in C++
- Maximum number of squares that can fit in a right angle isosceles triangle in C++
- Maximum path sum in matrix in C++
- Binary Tree Maximum Path Sum in Python
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum sum path in a matrix from top to bottom in C++
- Maximum Sum Path in Two Arrays in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Maximum sum path in a matrix from top to bottom and back in C++ Program
- Find sum of a number and its maximum prime factor in C++
- C++ Path Length having Maximum Number of Bends

Advertisements