#include <iostream>
using namespace std;
int *remanage( int * p_array, const int & position, const int & length ) {
int *ary = new int [length];
int local_pos = 0;
for ( int i=0; i<length; i++ ) {
if ( ( i + position - 1 ) == length ) local_pos = length;
ary[i] = *( p_array + i + position-1 - local_pos );
}
return ary;
}
int main(int argc, char *argv[]) {
int ary[] = { 11, 19, 9, 12, 5, 20, 1, 18, 4, 16, 6, 10, 15, 2, 17, 3, 14, 7, 13, 8 };
const int len = sizeof(ary) / sizeof(int);
int pos;
cout << "Input an initial position of array in [5, 20] \n";
cin >> pos;
int *p_ary = remanage( &ary[0], pos, len );
cout << "original array: \n";
for ( int i=0; i<len; i++ ){
cout << ary[i] << " ";
}
cout << endl;
cout << "re-arranged array: \n";
for ( int i=0; i<len; i++ ){
cout << p_ary[i] << " ";
}
cout << endl;
int max=0;
int sum=0;
int init_pos=0;
int max_pos=0;
while ( p_ary[init_pos+3] != 0 ) {
for ( int j=0; j<4; j++ ) {
//cout << p_ary[init_pos+j] << " ";
sum += p_ary[init_pos+j];
}
//cout << " = " << sum << " " << endl;
if ( max < sum ) {
max = sum;
max_pos = init_pos+1;
}
init_pos++;
sum = 0;
}
cout << endl;
cout << "loc" << max_pos << ": ";
for ( int k=0; k<4; k++ ) {
cout << p_ary[max_pos+k-1] << " ";
}
cout << "= <MAX> " << max << endl;
delete [] p_ary;
}