Find the Maximum sum of the Array by performing the given operations
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++
#include <bits/stdc++.h>
using namespace std;
void maximumsum( int A[], int n)
{
int k = 1;
for ( int i = 0; i < n; i++) {
k *= A[i];
}
k += n - 1;
cout << k << endl;
}
int main()
{
int n = 3;
int A[] = { 2, 3, 2 };
maximumsum(A, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void maximumsum( int A[], int n)
{
int k = 1 ;
for ( int i = 0 ; i < n; i++) {
k *= A[i];
}
k += n - 1 ;
System.out.println(k);
}
public static void main(String[] args)
{
int n = 3 ;
int A[] = { 2 , 3 , 2 };
maximumsum(A, n);
}
}
|
Python3
def maximumsum(A, n):
k = 1
for i in range (n):
k * = A[i]
k + = n - 1
print (k)
if __name__ = = '__main__' :
n = 3
A = [ 2 , 3 , 2 ]
maximumsum(A, n)
|
C#
using System;
using System.Collections.Generic;
class Gfg
{
static void maximumsum( int [] A, int n)
{
int k = 1;
for ( int i = 0; i < n; i++) {
k *= A[i];
}
k += n - 1;
Console.WriteLine(k);
}
public static void Main( string [] args)
{
int n = 3;
int [] A = { 2, 3, 2 };
maximumsum(A, n);
}
}
|
Javascript
function maximumsum(A, n)
{
let k = 1;
for (let i = 0; i < n; i++) {
k *= A[i];
}
k += n - 1;
console.log(k);
}
let n = 3;
let A = [ 2, 3, 2 ];
maximumsum(A, n);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...