jayati

Maximum sum increasing subsequence

Nov 30th, 2023
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // } Driver Code Ends
  6. class Solution{
  7.        
  8.  
  9.     public:
  10.     int maxSumIS(int arr[], int n)  
  11.     {  
  12.         // Your code goes here
  13.         vector<int> maxSum(n,0);
  14.        
  15.         for(int i=0;i<n;i++)
  16.         {
  17.             maxSum[i]=arr[i];
  18.             for(int j=0;j<i;j++)
  19.             {
  20.                 if(arr[i]>arr[j] && maxSum[i]<maxSum[j]+arr[i])
  21.                 {
  22.                     maxSum[i]=maxSum[j]+arr[i];
  23.                 }
  24.             }
  25.         }
  26.         return *max_element(maxSum.begin(),maxSum.end());
  27.     }  
  28. };
  29.  
  30. //{ Driver Code Starts.
  31. int main()
  32. {
  33.    
  34.    
  35.     int t;
  36.     cin >> t;
  37.     while (t--)
  38.     {
  39.         int n;
  40.         cin >> n;
  41.  
  42.         int a[n];
  43.  
  44.         for(int i = 0; i < n; i++)
  45.             cin >> a[i];
  46.  
  47.      
  48.  
  49.         Solution ob;
  50.         cout << ob.maxSumIS(a, n) << "\n";
  51.          
  52.     }
  53.     return 0;
  54. }
  55.  
  56.  
  57. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment