Open in App
Not now

# Find the Maximum sum of the Array by performing the given operations

• Difficulty Level : Medium
• Last Updated : 08 Feb, 2023

Given an Array A[] of size N with repeated elements and all array elements are positive, the task is to find the maximum sum by applying the given operations:

• Select any 2 indexes and select 2 integers(say x and y) such that the product of the elements(x and y) is equal to the product of the elements at the chosen index.
• At last, replace the array element with x and y

Examples:

Input: N = 3, A[] = {2, 3, 2}
Output: 14
Explanation:

• i = 1,  j = 2, x = 6,  y = 1 After applying the following operation we get sum = 9(6 + 1 + 2) and array now become {6, 1, 2} now we again chose i and j and will try to find i and j
• i = 1, j = 3 array become {12, 1, 1} sum become 14, thus the maximum sum will remain 14 and we cannot increase further.

Input: N = 2, A = {1, 3}
Output: 4
Explanation

• i = 1, j = 2, x = 3, y = 1 After applying the following operation we get sum = 4 array now become {3, 1}. If we again apply the operation then the array will remain the same so the maximum sum comes out to be 4.

Approach: Implement the idea below to solve the problem:

Notice the operations then we can observe that we are multiplying 2 array elements so at each time we will select one integer as product and other is 1. why? Because this will satisfy our condition for the operation that is given in the question that is product of x and y should be equal to the product of array element at the index we have Chosen. So, we will follow this operation n -1 times and after following the operation n -1 times we will get our final array which consist of n – 1 ones and one element which is equal to product of all the array elements.

Follow the below steps to implement the above idea:

• Calculate the product of the array.
• Add n – 1 to the final product of the array.
• The resulting variable will be the maximum sum possible.

Below is the implementation for the above approach:

## C++

 `// C++ implementation of the above approach` `#include ` `using` `namespace` `std;`   `// Function to calculate maximum sum` `void` `maximumsum(``int` `A[], ``int` `n)` `{`   `    ``int` `k = 1;`   `    ``// Calculating the product of array` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``k *= A[i];` `    ``}`   `    ``// Adding n-1 ones` `    ``k += n - 1;`   `    ``// Printing the final answer` `    ``cout << k << endl;` `}`   `// Driver Code` `int` `main()` `{`   `    ``int` `n = 3;` `    ``int` `A[] = { 2, 3, 2 };`   `    ``// Function call` `    ``maximumsum(A, n);`   `    ``return` `0;` `}`

## Java

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `// Function to calculate maximum sum` `static` `void` `maximumsum(``int` `A[], ``int` `n)` `{`   `    ``int` `k = ``1``;`   `    ``// Calculating the product of array` `    ``for` `(``int` `i = ``0``; i < n; i++) {` `        ``k *= A[i];` `    ``}`   `    ``// Adding n-1 ones` `    ``k += n - ``1``;`   `    ``// Printing the final answer` `    ``System.out.println(k);` `}` `    `  `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``3``;` `    ``int` `A[] = { ``2``, ``3``, ``2` `};`   `    ``// Function call` `    ``maximumsum(A, n);` `}` `}`

## Python3

 `# Python3 implementation of the above approach` `def` `maximumsum(A, n):` `    ``k ``=` `1` `    `  `    ``# Calculating the product of array` `    ``for` `i ``in` `range``(n):` `        ``k ``*``=` `A[i]` `        `  `    ``# Adding n-1 ones` `    ``k ``+``=` `n ``-` `1` `    `  `    ``# Printing the final answer` `    ``print``(k)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    ``n ``=` `3` `    ``A ``=` `[``2``, ``3``, ``2``]` `    `  `    ``# Function call` `    ``maximumsum(A, n)`   `    ``# This code is contributed by phasing17.`

## C#

 `// C# implementation of the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `Gfg` `{` `  ``// Function to calculate maximum sum` `  ``static` `void` `maximumsum(``int``[] A, ``int` `n)` `  ``{`   `    ``int` `k = 1;`   `    ``// Calculating the product of array` `    ``for` `(``int` `i = 0; i < n; i++) {` `      ``k *= A[i];` `    ``}`   `    ``// Adding n-1 ones` `    ``k += n - 1;`   `    ``// Printing the final answer` `    ``Console.WriteLine(k);` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(``string``[] args)` `  ``{`   `    ``int` `n = 3;` `    ``int``[] A = { 2, 3, 2 };`   `    ``// Function call` `    ``maximumsum(A, n);`   `  ``}` `}`   `// This code is contributed by imruhrbf8.`

## Javascript

 `// Javascript implementation of the above approach`   `// Function to calculate maximum sum` `function` `maximumsum(A, n)` `{`   `    ``let k = 1;`   `    ``// Calculating the product of array` `    ``for` `(let i = 0; i < n; i++) {` `        ``k *= A[i];` `    ``}`   `    ``// Adding n-1 ones` `    ``k += n - 1;`   `    ``// Printing the final answer` `    ``console.log(k);` `}`   `// Driver Code` `let n = 3;` `let A = [ 2, 3, 2 ];`   `// Function call` `maximumsum(A, n);`

Output

`14`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles