# Java Urgent Help

• February 16th, 2014, 04:22 PM
satyen
Java Urgent Help
I have a java question. Can anyone help me solve it please. Much appreciated

A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is called a triangle if 0 ≤ P < Q < R < N and:
A[P] + A[Q] > A[R],
A[Q] + A[R] > A[P],
A[R] + A[P] > A[Q].
The perimeter of such a triangle equals A[P] + A[Q] + A[R]. For example, consider the following array A:
A[0] = 10
A[1] = 2
A[2] = 5
A[3] = 1
A[4] = 8
A[5] = 20
Triplet (0, 2, 4) is a triangle and its perimeter equals 10 + 5 + 8 = 23. There is no other triangle in this array with a larger perimeter.
Write a function:
class Solution { public int solution(int[] A); }
that, given a zero-indexed array A of N integers, returns the maximum perimeter of any triangle in this array. The function should return −1 if there are no triangles.
For example, given:
A[0] = 10
A[1] = 2
A[2] = 5
A[3] = 1
A[4] = 8
A[5] = 20
the function should return 23, as explained above.
Given array A such that:
A[0] = 5
A[1] = 10
A[2] = 18
A[3] = 7
A[4] = 8
A[5] = 3
the function should return 25: the triangle with the maximum perimeter is (1, 3, 4).
While for an array A:
A[0] = 10
A[1] = 20
A[2] = 30
the function should return −1, as it is impossible to create a triangle.
Assume that:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [1..100,000,000].
Complexity:
expected worst-case time complexity is O(N*log(N));
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
• February 16th, 2014, 04:26 PM
Norm
Re: Java Urgent Help
Quote:

I have a java question
Be sure to wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• February 16th, 2014, 04:28 PM
satyen
Re: Java Urgent Help
All I have is the question above with no other guidance. Do you think you could kindly form a rough code. Thank you
• February 16th, 2014, 04:32 PM
Norm
Re: Java Urgent Help
Sorry, I don't write code for students.
What have you got so far? Where are you having problems?
• February 16th, 2014, 04:34 PM
satyen
Re: Java Urgent Help
That's a shame because I am not a Student. I am helping my brother out whose going for a finance job and they have asked for him to do some code in java which is pointless for him as this is very IT technical.
• February 16th, 2014, 04:41 PM
GoodbyeWorld
Re: Java Urgent Help
This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• February 16th, 2014, 05:12 PM
KucerakJM
Re: Java Urgent Help
So...do you or your brother have any experience with java?
Also, regarding this bit:
A[P] + A[Q] > A[R],
A[Q] + A[R] > A[P],
A[R] + A[P] > A[Q].
The first rule is the only relevant one as the other two would be true by nature of 0 ≤ P < Q < R < N. One approach would be to sort array high to low, check the first 3 integers against the rules then print perimeter if they pass or move one index over. (If A[0],A[1],A[2] don't work then try A[1],A[2],A[3])

GoodbyeWorld, that is not cross posting, just creating duplicate threads. If they posted the same thing on another forum (not jpf) altogether then that would be cross posting.