Given an Array A[] of measurement N with repeated parts and all array parts are constructive, the duty is to seek out the most sum by making use of the given operations:
- Choose any 2 indexes and choose 2 integers(say x and y) such that the product of the weather(x and y) is the same as the product of the weather on the chosen index.
- Eventually, substitute the array component with x and y
Examples:
Enter: N = 3, A[] = {2, 3, 2}
Output: 14
Rationalization:
- i = 1, j = 2, x = 6, y = 1 After making use of the next operation we get sum = 9(6 + 1 + 2) and array now turn into {6, 1, 2} now we once more selected i and j and can attempt to discover i and j
- i = 1, j = 3 array turn into {12, 1, 1} sum turn into 14, thus the utmost sum will stay 14 and we can’t improve additional.
Enter: N = 2, A = {1, 3}
Output: 4
Rationalization:
- i = 1, j = 2, x = 3, y = 1 After making use of the next operation we get sum = 4 array now turn into {3, 1}. If we once more apply the operation then the array will stay the identical so the utmost sum comes out to be 4.
Method: Implement the concept beneath to resolve the issue:
Discover the operations then we are able to observe that we’re multiplying 2 array parts so at every time we are going to choose one integer as product and different is 1. why? As a result of it will fulfill our situation for the operation that’s given within the query that’s product of x and y ought to be equal to the product of array component on the index we’ve Chosen. So, we are going to observe this operation n -1 instances and after following the operation n -1 instances we are going to get our ultimate array which include n – 1 ones and one component which is the same as product of all of the array parts.
Comply with the beneath steps to implement the above thought:
- Calculate the product of the array.
- Add n – 1 to the ultimate product of the array.
- The ensuing variable would be the most sum doable.
Under is the implementation for the above method:
C++
|
|
Time Complexity: O(N)
Auxiliary Area: O(1)