Post date: Oct 26, 2013 1:11:15 AM
Problem
/*
4 individual numbers which could be permuted in 4 factorial ways.
permutation of these 4 integers is an 0indexedarray consisting of 4 digits in some order when integers are different.
the best permute of the 4 integers is by the following funciton
func(summ) = abs(summ[0] - summ[1]) + abs(summ[1] - summ[2] + abs(summ[2] - summ[3])) that would give maximum value.
method signature
public int answer(int w, int x, int y, int z){
}
w = 5
x = 3
y = -1
z = 5
the sample permute wiht given numbers in the given function that would give maximum value is as follows.
for the
summ[0] = 5
summ[1] = -1
summ[2] = 5
summ[3] = 3
This should be done in O(1)time ans space complexity.
My questions wordings may be confusing, but the function and sample data are perfectly correct.
*/
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Compute min and max
Created Date : 26-10-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
using namespace std;
class ArrangeArray{
private:
int summ[4];
public:
int answer(int w, int x, int y, int z){
summ[0] = w;
summ[1] = x;
summ[2] = y;
summ[3] = z;
if(summ[0] < summ[1]) swap(summ[0], summ[1]);
if(summ[2] < summ[3]) swap(summ[2], summ[3]);
if(summ[0] < summ[2]) swap(summ[0], summ[2]);
if(summ[1] > summ[3]) swap(summ[1], summ[3]);
if(summ[2] < summ[3]) swap(summ[2], summ[3]);
return 0;
}
void Print()
{
for(auto i: summ){
cout << i << endl;
}
}
};
int main(int argc, char* argv[])
{
ArrangeArray aa;
aa.answer(5, 3, -1, 5);
aa.Print();
cout << "---------------------" << endl;
aa.answer(2, 3, 1, 4);
aa.Print();
return 0;
}
Output
5
-1
5
3
---------------------
4
1
3
2
Press any key to continue . . .