Nitin
BAN USERI think the following should work:
1) find the 2 smallest (u,v) and 3 largest numbers (x < y < z) from the array (they can overlap if numbers are less)
2) find the product of the two smallest numbers ( say A = u * v) and two largest numbers (say B = y * z)
if( A > B)
Answer = A * z;
else
Answer = B * x;
Comments?
I am trying to solve the problem :P and description shouldn't be hard to write but might be hard to understand given how I will write.
I used the greedy approach. Starting with index 0, I find the position which has the maximum value where I can jump. But I consider the fact that {3, 2} lying in this order are equivalent by decreasing the value at any current index by 1 while checking for the max value. For example, {3, 3, 2, 2, 3, 4} I chose index 3 and not index 1 on first jump from index 0.
I do it iteratively until I reach the end.
HTH.
How about this?
Starting from right for each digit, if possible, find the digit which has a smaller on its left (possibly not immediate). If such a digit can be found, then proceed otherwise exit.
Now say this digit's position is i, and digit smaller than it has position j such that j < i. Now swap i & j and sort all digits from j+1 to n (where n is the total number of digits).
For example (zero based indexing):
1024 => i = 3, j = 2 => 1042
10244 => i = 4, j = 2 => 10424
865 => i = ?? => no solution
524308 => i = 5, j = 4 => 524380
567123 => i = 5, j = 4 => 567132
567821 => i = 4, j = 3 => 568127
52430 => i = 3, j = 1 => 53024
How about it? Complexity: O(n^2)
RepBimerLaura, Area Sales Manager at Bloomberg LP
I am a Job training specialist conducting training programs that will boost employees workplace performance in alliance with the company ...
Learn to run code, try here: codepad[dot]org/Br0scfvF
- Nitin March 26, 2012