
- 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 path sum for each position with jumps under divisibility condition in C++
In this tutorial, we will be discussing a program to find maximum path sum for each position with jumps under divisibility condition
For this we will be provided with an array of n random integers. Our task is to jump from one position to another if it divides it and finally provide the maximum sum path for every given position.
Example
#include <bits/stdc++.h> using namespace std; //finding maximum sum path void printMaxSum(int arr[], int n) { int dp[n]; memset(dp, 0, sizeof dp); for (int i = 0; i < n; i++) { dp[i] = arr[i]; int maxi = 0; for (int j = 1; j <= sqrt(i + 1); j++) { if (((i + 1) % j == 0) && (i + 1) != j) { if (dp[j - 1] > maxi) maxi = dp[j - 1]; if (dp[(i + 1) / j - 1] > maxi && j != 1) maxi = dp[(i + 1) / j - 1]; } } dp[i] += maxi; } for (int i = 0; i < n; i++) cout << dp[i] << " "; } int main() { int arr[] = { 2, 3, 1, 4, 6, 5 }; int n = sizeof(arr) / sizeof(arr[0]); printMaxSum(arr, n); return 0; }
Output
2 5 3 9 8 10
- Related Articles
- Maximum path sum in matrix in C++
- Maximum path sum in a triangle in C++
- Maximum Sum Path in Two Arrays in C++
- Path with Maximum Gold in C++
- Maximum Path Sum in a Binary Tree in C++
- Maximum path sum in an Inverted triangle in C++
- C++ Path with Maximum Average Value
- Binary Tree Maximum Path Sum in Python
- Maximum sum of a path in a Right Number Triangle in C++
- Maximum sum path in a matrix from top to bottom in C++
- Efficiency of DC Generator & Condition for Maximum Efficiency with Examples
- Maximum sum path in a matrix from top to bottom in C++ Program
- Path Sum III in C++
- Minimum Path Sum in C++
- Path Sum IV in C++

Advertisements