
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Path Sum 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
Advertisements