CPP program based on Vikram Venkatesan's Algo:
#include <iostream>
#include <conio.h>
using namespace std;
class arr
{
int *a;
int n; /*No. of elements in an array*/
public:
arr()
{
a=0;
n=0;
}
arr(int num_of_elements)
{
n = num_of_elements;
a = new int[n];
}
void get_arr()
{
cout<<"Enter Elements\n";
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<". ::";
cin>>a[i];
}
}
void put_arr()
{
cout<<"No. of Array Elements:: "<<n<<"\n";
cout<<"Array Elements\n";
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<". ::"<<a[i];
}
}
void find_max(int &grt_val,int &low,int &high);
~arr()
{
printf("\nDestructor is called....\n");
delete a;
}
};
void arr::find_max(int &grt_val,int &low,int &high)
{
grt_val=0;
int i,j,pivot,sum;
int l=-1;
int r=-1;
for(i=0;i<n;i++)
{/*Main Loop*/
sum=0;
pivot=a[i];
/*set l and r*/
j=i;
while(j>0)
{
if(a[j-1]>pivot)
j--;
else break;
}
l=j;
j=i;
while(j<=(n-2))
{
if(a[j+1]>pivot)
j++;
else break;
}
r=j;
/*find sum*/
for(j=l;j<=r;j++)
{
sum += a[j];
}
sum *= pivot;
/* cout<<"\nPivot:: "<<pivot;
cout<<"\nSum:: "<<sum;
cout<<"\nRange:: "<<l<<"\t"<<r<<"\n";*/
/*compare it with max and set max=sum if required sum>max*/
if(sum>grt_val)
{
grt_val = sum;
low = l;
high = r;
}
}
}
int main()
{
int n;
char ch;
int grt_val; /*Greatest value of test case*/
int low,high;/*Range of greatest value of test case*/
cout<<"Enter no. of elements in an array \n";
cin>>n;
arr t1(n);
t1.get_arr();
// t1.put_arr();
t1.find_max(grt_val,low,high);
cout<<"\nMax Happiness:: "<<grt_val;
cout<<"\nRange of Happiness:: "<<low+1<<"\t"<<high+1<<"\n";
getch();
return 0;
}
Regards,
Manoj
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---
- [algogeeks] Nice question!! Pradeep Muthukrishnan
- [algogeeks] Re: Nice question!! Guillermo Garcia
- [algogeeks] Re: Nice question!! Pradeep Muthukrishnan
- [algogeeks] Re: Nice question!! Guillermo Garcia
- [algogeeks] Re: Nice question!! Gene
- [algogeeks] Re: Nice question!! Prunthaban Kanthakumar
- [algogeeks] Re: Nice question!! Vikram Venkatesan
- [algogeeks] Re: Nice question!! Arunachalam
- [algogeeks] Re: Nice question... Googmeister
- [algogeeks] Re: Nice ques... Prunthaban Kanthakumar
- [algogeeks] Re: Nice ... Manoj Gupta
- [algogeeks] Re: Nice question!! Googmeister
- [algogeeks] Re: Nice question!! Arunachalam
- [algogeeks] Re: Nice question!! Googmeister
- [algogeeks] Re: Nice question!! Arunachalam
- [algogeeks] Re: Nice question!! Gene
- [algogeeks] Re: Nice question!! Googmeister
- [algogeeks] Re: Nice question!! Prunthaban Kanthakumar
- [algogeeks] Re: Nice question!! Gene
- [algogeeks] Re: Nice question... mg
- [algogeeks] Re: Nice ques... Prunthaban Kanthakumar