Anil
BAN USER#include <iostream>
using namespace std;
class LongestSum {
private:
long _findSum ( int* array, int startIndex, int endIndex ) {
if ( startIndex == endIndex ) {
return array[startIndex];
}
int half = ( startIndex + endIndex ) / 2;
long leftSum = _findSum( array, startIndex, half );
long rightSum = _findSum( array, half + 1, endIndex );
long middleSum = 0L;
int left = half;
int right = half + 1;
long currentSum = middleSum;
while ( left >= startIndex && right <= endIndex ) {
if ( array[left] >= array[right] ) {
currentSum = currentSum + array[left];
}
else {
currentSum = currentSum + array[right++];
}
if ( currentSum >= middleSum ) {
middleSum = currentSum;
}
}
while ( left >= startIndex ) {
currentSum = currentSum + array[left];
if ( currentSum >= middleSum ) {
middleSum = currentSum;
}
}
while ( right <= startIndex ) {
currentSum = currentSum + array[right++];
if ( currentSum >= middleSum ) {
middleSum = currentSum;
}
}
long halfMax = ( leftSum > rightSum ) ? leftSum : rightSum;
long max = ( halfMax > middleSum ) ? halfMax : middleSum;
return max;
}
public:
long findSum( int *input, int size ) {
if ( size == 0 ) {
return 0;
}
int halves = size / 2;
int* odd = new int[halves];
int* even = new int[size  halves ];
for ( int i = 0; i< size; i++ ) {
if ( i%2 == 0 ) {
even[i/2]= input[i];
}
else {
odd[i/2] = input[i];
}
}
long maxOdd = _findSum(odd, 0, halves  1 );
long maxEven = _findSum(even, 0, size  halves 1 );
return ( maxOdd > maxEven) ? maxOdd : maxEven;
};
};
int main(int argc, const char * argv[])
{
LongestSum* sum = new LongestSum();
int size = 7;
int input[7] = { 3,8,4,8,4,2,3 };
cout << "Sum is " << sum>findSum(input,size) << endl;
int test2[8] = {1,3,5,1,12,6,7,11};
cout << "Sum is " << sum>findSum(test2,8) << endl;
int test1[7] = {50,7,11,2,1,3,4};
cout << "Sum is " << sum>findSum(test1,size) << endl;
int test[5] = {1,  1, 3, 8 ,4 };
cout << "Sum is " << sum>findSum(test,5) << endl;
delete sum;
return 0;
}

Anil
June 06, 2013 int maxDiff( int* numbers, int size ) {
int max_diff = 0;
int min_ind = 0;
for( int i = 0 ; i < size; i++ ) {
if( numbers[i] < numbers[min_ind] ) {
min_ind = i;
}
else {
if ( ( numbers[i]  numbers[min_ind] ) > max_diff ) {
max_diff = numbers[i]  numbers[min_ind] ;
}
}
}
return max_diff;
}
int maxDiff( int* numbers, int size ) {
int max_diff = 0;
int min_ind = 0;
for( int i = 0 ; i < size; i++ ) {
if( numbers[i] < numbers[min_ind] ) {
min_ind = i;
}
else {
if ( ( numbers[i]  numbers[min_ind] ) > max_diff ) {
max_diff = numbers[i]  numbers[min_ind] ;
}
}
}
return max_diff;
}
Reprichardcstrong, Accountant at AppPerfect
I am a modern magician, except I transform complicated technical ideas into userfriendly images before the eyes of your company ...
RepLisaBAllen, Accountant at ABC TECH SUPPORT
I am Lisa a certified Personal Trainer with more than 7 years experience in the local and international fitness scene ...
RepI'm from India, 26 years old. I want to travel, I want a job where I can earn money ...
Repsherrymrex, Computer Scientist at CGIAMS
I am Sherry from West Palm Beach USA, I started my journey in 2016 as a yoga teacher. I like ...
Repdubinalinda4, Animator at Apache Design
Hello, I am a school librarian from Lewistown USA. I work in public or private schools at elementary, middle and ...
Repamritatorr, Paramedic at Capital Medical Billing
By profession, I am a paramedic in Chicago. I want to know about Indian ayurvedic treatment. I like to explore ...
Open Chat in New Window
Maintain an array/hashtable as a counter.
 Anil June 08, 2013e.g. for [2,5] ( Assuming the animal was born at time 2 and died at 5 )
increment count of 2,3,4 and 5 by one
repeat the step for each set.
at the end parse the array/hash to get the max and min and respective year.