Carey Brown wrote:Is 'name' in Transaction the same as Customer.name, e.g. "Joe Smith"? If so then Transaction.name is redundant because you already have Transaction.custid that can be used to retrieve the name.
Transaction is not a sub-class of anything so you can't call 'super()' as you've done on line 26.
In the Customer constructor you pass in a List of Transactions which is inappropriate. Id, name, and citizenshpNo are fixed attributes of a Customer but Transactions are different in that (supposedly) you can add them over time with a properly named method.
Customer
id
name
citizenshipNo
Trans
id
custid
name
amount
Piet Souris wrote:hi Tangara,
I would not use that method at all. In the case of a triple product, I would split the array in two parts, the negative numbers and the rest, sort the two subarrays, and then the max product is either the product of the two smallest negative numbers and the largest non-negative number, or the product of the three largest non-negative numbers. This can be generalized to the max product of K elements
A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3
A[1] = 1
A[2] = 2
A[3] = -2
A[4] = 5
A[5] = 6
contains the following example triplets:
(0, 1, 2), product is −3 * 1 * 2 = −6
(1, 2, 4), product is 1 * 2 * 5 = 10
(2, 4, 5), product is 2 * 5 * 6 = 60
Your goal is to find the maximal product of any triplet.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A, returns the value of the maximal product of any triplet.
For example, given array A such that:
A[0] = -3
A[1] = 1
A[2] = 2
A[3] = -2
A[4] = 5
A[5] = 6
the function should return 60, as the product of triplet (2, 4, 5) is maximal.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [3..100,000];
each element of array A is an integer within the range [−1,000..1,000].
Stephan van Hulst wrote:No. The problem is that arraySum may overflow when you're adding a to it. So that variable needs to be a long. When you cast totalSum, it's already too late.
Here is how I would implement Piet's solution:
Carey Brown wrote:Sorry, guess there isn't any code. Just the problem statement.
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
We can split this tape in four places:
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
If you divide the array into sumOfLeft and sumOfRight
Where P == 1
Left = 3
Right = 1 + 2 + 4 + 3 = 10
diff = Math.abs( left - right ) = 7
Piet Souris wrote:The exercise is about splitting an array into two parts. In your example the split is after the first element. Then we determine the sum of both parts. The first part contains just 3 and the other part sums up to 10. The question is where the split must be in order that both sums are as close as possible.
Did you figure out an algorithm yet?
Piet Souris wrote:Well I used the sum of the array, and with the elements going from 1 to 100.000, the sum is about 5 billion. So internally I would use longs, and cast the answer to int. But with your method, this is not necessary. I just mentioned my method because it is immediately clear that it is O(N).
Carey Brown wrote:The link in your title doesn't work for me. Would you cut and paste the code here.
Piet Souris wrote:That my formula fails is due to overflow. If the array contained longs then there would be no problem. For instance, if the array is {1, 3, 4, 5}, then the missing element is 5*6/2 - 13 = 2.
A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
We can split this tape in four places:
P = 1, difference = |3 − 10| = 7
P = 2, difference = |4 − 9| = 5
P = 3, difference = |6 − 7| = 1
P = 4, difference = |10 − 3| = 7
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
For example, given:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [2..100,000];
each element of array A is an integer within the range [−1,000..1,000].
P = 1, difference = |3 − 10| = 7
Piet Souris wrote:Assuming that the creation of the set and the checking of the array-elements are both O(N), the whole operation is O(N). A bit more clear is returning (N + 1) * (N + 2) / 2 - sum of the array elements.