Maximum sum increasing subsequence
PROBLEM :
Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the given array such that the integers in the subsequence are sorted in increasing order.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input A[].
Output:
Print the sum of maximum sum sequence of the given array.
Constraints:
1 = T = 50
1 = N = 100
1 = A[] = 1000
Example:
Input:
2
7
1 101 2 3 100 4 5
4
10 5 4 3
Output:
106
10
Explanation:
For input:
7
1 101 2 3 100 4 5
All the increasing subsequences : (1,101); (1,2,3,100); (1,2,3,4,5), out of this (1,2,3,100) has maximum sum,i.e., 106. Hence the output is stated as 106.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Maximum_sum_increasing_subsequence(int[] ,int ) ;
int main()
{
int t,no,arr[1000],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Maximum_sum_increasing_subsequence(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Maximum_sum_increasing_subsequence(int arr[],int no)
{
int sum[no] ;
int i,j ;
for(i=0;i<no;i++)
sum[i]=arr[i] ;
for(i=1;i<no;i++)
{
for(j=0;j<i;j++)
{
if(arr[j]<arr[i]&&sum[i]<arr[i]+sum[j])
sum[i]=arr[i]+sum[j] ;
}
}
int max=sum[0] ;
for(i=0;i<no;i++)
if(sum[i]>max)
max=sum[i] ;
return max ;
}
---------------------------------------------------------------------------------
Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the given array such that the integers in the subsequence are sorted in increasing order.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the size of array.
The second line of each test case contains N input A[].
Output:
Print the sum of maximum sum sequence of the given array.
Constraints:
1 = T = 50
1 = N = 100
1 = A[] = 1000
Example:
Input:
2
7
1 101 2 3 100 4 5
4
10 5 4 3
Output:
106
10
Explanation:
For input:
7
1 101 2 3 100 4 5
All the increasing subsequences : (1,101); (1,2,3,100); (1,2,3,4,5), out of this (1,2,3,100) has maximum sum,i.e., 106. Hence the output is stated as 106.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Maximum_sum_increasing_subsequence(int[] ,int ) ;
int main()
{
int t,no,arr[1000],i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
no=Maximum_sum_increasing_subsequence(arr,no) ;
cout<<no<<endl ;
}
return 0;
}
int Maximum_sum_increasing_subsequence(int arr[],int no)
{
int sum[no] ;
int i,j ;
for(i=0;i<no;i++)
sum[i]=arr[i] ;
for(i=1;i<no;i++)
{
for(j=0;j<i;j++)
{
if(arr[j]<arr[i]&&sum[i]<arr[i]+sum[j])
sum[i]=arr[i]+sum[j] ;
}
}
int max=sum[0] ;
for(i=0;i<no;i++)
if(sum[i]>max)
max=sum[i] ;
return max ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment