Find the Maximum sum of the Array by performing the given operations Last Updated : 08 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: 14Explanation: 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 ji = 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: 4Explanation: 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 <bits/stdc++.h> 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); Output14 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find the Maximum sum of the Array by performing the given operations shivamaggarwal5570 Follow Improve Article Tags : Greedy Competitive Programming DSA Arrays Arrays Maths +2 More Practice Tags : ArraysArraysGreedy Similar Reads Maximize the Sum of the given array using given operations Given two arrays A[] and B[] consisting of N integers and an integer K, the task is to maximize the sum calculated from the array A[] by the following operations: For every index in B[] containing 0, the corresponding index in A[] is added to the sum.For every index in B[] containing 1, add the valu 7 min read Maximum possible array sum after performing the given operation Given an array arr[] of size N, the task is to find the maximum sum of the elements of the array after applying the given operation any number of times. In a single operation, choose an index 1 ? i < N and multiply both arr[i] and arr[i - 1] by -1.Examples: Input: arr[] = {-10, 5, -4} Output: 19 9 min read Maximum sum of all elements of array after performing given operations Given an array of integers. The task is to find the maximum sum of all the elements of the array after performing the given two operations once each. The operations are: 1. Select some(possibly none) continuous elements from the beginning of the array and multiply by -1. 2. Select some(possibly none 7 min read Maximum score possible after performing given operations on an Array Given an array A of size N, the task is to find the maximum score possible of this array. The score of an array is calculated by performing the following operations on the array N times: If the operation is odd-numbered, the score is incremented by the sum of all elements of the current array. If th 15+ min read Maximum possible Array sum after performing given operations Given array arr[] of positive integers, an integer Q, and arrays X[] and Y[] of size Q. For each element in arrays X[] and Y[], we can perform the below operations: For each query from array X[] and Y[], select at most X[i] elements from array arr[] and replace all the selected elements with integer 9 min read Maximize the sum of sum of the Array by removing end elements Given an array arr of size N, the task is to maximize the sum of sum, of the remaining elements in the array, by removing the end elements.Example: Input: arr[] = {2, 3} Output: 3 Explanation: At first we will delete 2, then sum of remaining elements = 3. Then delete 3. Therefore sum of sum = 3 + 0 6 min read Find maximum in an array without using Relational Operators Given an array A[] of non-negative integers, find the maximum in the array without using Relational Operator. Examples: Input : A[] = {2, 3, 1, 4, 5}Output : 5Input : A[] = {23, 17, 93}Output : 93We use repeated subtraction to find out the maximum. To find maximum between two numbers, we take a vari 9 min read Maximize sum of pairwise products generated from the given Arrays Given three arrays arr1[], arr2[] and arr3[] of length N1, N2, and N3 respectively, the task is to find the maximum sum possible by adding the products of pairs taken from different arrays. Note: Each array element can be a part of a single pair. Examples: Input: arr1[] = {3, 5}, arr2[] = {2, 1}, ar 11 min read Maximizing array sum with given operation There is an array consisting of (2 * n - 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array. Examples : Input : arr[] = 50 50 50 Output : 150 There is no need t 6 min read Find maximum sum by replacing the Subarray in given range Given an array arr[], the task is to find the maximum sum possible such that any subarray of the indices from [l, r] i.e all subarray elements from arr[l] to arr[r] can be replaced with |arr[l] - arr[r]| any number of times. Examples: Input: arr[] = { 9, 1}Output: 16Explanation: The subarray [l, r] 9 min read Like