in Education by
Given an array of integers, which can contain both +ve and -ve numbers. I've to maximize the product of any 3 elements of the array. The elements can be non-contiguous. Some examples: int[] arr = {-5, -7, 4, 2, 1, 9}; // Max Product of 3 numbers = -5 * -7 * 9 int[] arr2 = {4, 5, -19, 3}; // Max Product of 3 numbers = 4 * 5 * 3 I've tried solving it using Dynamic Programming, but I'm not getting the expected result. It is returning the result often involving the same number twice in the multiplication. So, for the array - {4, 2, 1, 9}, it is returning - 32, which is 4 * 4 * 2. Here's my code: public static int maxProduct(int[] arr, int count) { return maxProduct(arr, 0, arr.length - 1, count); } private static int maxProduct(int[] arr, int fromIndex, int toIndex, int count) { if (count == 1) { return maximum(arr, fromIndex, toIndex); } else if (toIndex - fromIndex + 1 < count) { return 1; } else { return MathUtil.max(maxProduct(arr, fromIndex, toIndex - 1, count - 1) * arr[toIndex - 1], maxProduct(arr, fromIndex, toIndex - 1, count)); } } MathUtil.max(int a, int b) is a method that gives maximum of a and b. The two values I pass to max method there are: maxProduct, when we consider last element as a part of product. maxProduct, when we don't consider it as a part of product. count contains the number of element we want to consider. Here 3. For count == 1, we have to find maximum of 1 element from array. That means, we have to use maximum array element. If toIndex - fromIndex + 1 < count, means, there are not enough elements in the array between those indices. I've an intuition that, the first if condition is one of the reason of failure. Because, it is only considering maximum element from an array, while the maximum product may comprise of negative numbers too. But I don't know how to take care of that. The reason I'm using Dynamic Programming is that I can then generalize this solution to work for any value of count. Of course, if someone have any better approach, even for count = 3, I welcome the suggestion (I would want to avoid sorting the array, as that will be another O(nlogn) at the least). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Sort the given array in ascending order and you have to take the maximum of these cases to get the answer.. product of last 3 numbers in sorted array Product of first two and last number in the sorted array

Related questions

0 votes
    I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 ... { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have an array that has multiple numbers in it. I want the function to return another array of maximum 3 ... { dartsToFinishArrTotal += dartsToFinishArr[n]; } } if (output...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
0 votes
    Two numbers are selected at random from 1,2,3,....100 and are multiplied, then the probability correct to two places ... 67 75 D. 8 75 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
0 votes
    Two different dice are thrown together. Find the probability that (i) the sum of the numbers appeared is less than ... less than 18. Select the correct answer from above options...
asked Nov 17, 2021 in Education by JackTerrance
0 votes
    Two dice are rolled together. Find the probability of getting such numbers on two dice whose product is a perfect square. Select the correct answer from above options...
asked Nov 17, 2021 in Education by JackTerrance
0 votes
0 votes
    Two dice are rolled once. Find the probability of getting such numbers on two dice whose product is a perfect square. Select the correct answer from above options...
asked Nov 17, 2021 in Education by JackTerrance
0 votes
    This question exist only because of pure curiosity. Not a homework. Find the fastest way to find two ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
...